yourSettings = FS22PrefSaver:new(modName, fileName, perSaveSlot, defaults, loadHookFunction, saveHookFunction, logClass)
- modName : Name of your mod
- fileName : Filename to save to (something.xml)
- perSaveSlot : True to instance per savegame, false for global to installation
- defaults : Table of default settings (see below)
- loadHookFunction : A function to call post-load of settings
- saveHookFunction : A function to call post-save of settings
- logClass : A FS22Log logging class. If this is nil, this library will not log anything.
Note for the logClass, this library provides a lot of development debug logging. At a minimum you
may want to configure the logger to filter out getValue
and setValue
. All logging from this library
is done at the FS22Log.LOG_LEVEL.VERBOSE or FS22Log.LOG_LEVEL.DEVEL level.
The defaults table takes options in two formats
local defaults = {
optionName = "optionValue"
}
local defaults = {
optionName = {"optionValue", "valueType"}
}
valueType is:
- string : A string, typically auto-detected
- bool : A boolean, typically auto-detected
- float : A floating point number, all numbers are detected as floats
- int : An integer, cannot be auto detected
- color : A color table {r, g, b, a}, cannot be auto detected
You may mix and match between types. Best practice is to only define the type when it cannot be auto detected. Note that you cannot used mixed types with the giants XML library (e.g. someOption = "string" or false) "nil" Values will also cause failures.
yourSettings:addDefaults(tableOfDefaults)
This adds the contents of tableOfDefaults
to the list of default options. In the case of a name
collision, the value of the option will be the newest one you assign.
yourSettings:getValue(optionName)
Retrieve the value of optionName
yourSettings:setValue(optionName, newValue)
Set the value of optionName
to newValue
yourSettings:saveSettings()
Save the settings to disk. Note that this is not automatic, you must hook this function to the game's save function. The save file will be created if it does not already exist.
yourSettings:loadSettings()
Load the settings from disk. This function will gracefully fail if they have not yet been saved.
These functions may prove useful in debugging errors with the library or your mod.
yourLogger:dumpDefaults()
Dumps the defaults to the log, using the logClass, at FS22Log.LOG_LEVEL.DEVEL
yourLogger:dumpSettings()
Dumps the settings to the log, using the logClass, at FS22Log.LOG_LEVEL.DEVEL
Note that UserPrefSaver sparsely populates the settings table the first time it is run, and returns the default value until the setting is explicitly set, or the settings are loaded from disk.
yourLogger:getXMLFileName()
Returns the full path and name of the XML file used for saving if you need to refer to it.
(c)JTSage Modding & FSG Modding. You may reuse or alter this code to your needs as necessary with no prior permission. No warranty implied or otherwise.