Skip to content

Latest commit

 

History

History
148 lines (104 loc) · 4.15 KB

README_ModPrefSaver.md

File metadata and controls

148 lines (104 loc) · 4.15 KB

FS22Log LUA User Preference Saver for FS22

Class Initialization

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.

Defaults Table

The defaults table takes options in two formats

Format 1

local defaults = {
  optionName = "optionValue"
}

Format 2

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.

Class Functions

Add Defaults

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.

Get Setting

yourSettings:getValue(optionName)

Retrieve the value of optionName

Set Setting

yourSettings:setValue(optionName, newValue)

Set the value of optionName to newValue

Save Settings

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.

Load Settings

yourSettings:loadSettings()

Load the settings from disk. This function will gracefully fail if they have not yet been saved.

Developer functions

These functions may prove useful in debugging errors with the library or your mod.

Print defaults

yourLogger:dumpDefaults()

Dumps the defaults to the log, using the logClass, at FS22Log.LOG_LEVEL.DEVEL

Print settings

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.

Get XML file name

yourLogger:getXMLFileName()

Returns the full path and name of the XML file used for saving if you need to refer to it.

Distribution & Licensing Terms

(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.