Code: Select all
-- We define the local variable for our settings table here so that our functions can see it
local settings
function script:init()
-- Let's get a storage table and initialize values that are nil
settings = Util.optStorage(TheoTown.getStorage(), self:getDraft():getId()..':settings')
settings.someBool = settings.someBool == nil and true or settings.someBool
settings.someInt = settings.someInt or 1
settings.someNamedInt = settings.someNamedInt or 3
settings.someString = settings.someString or 'Xyz'
settings.someGenericText = settings.someGenericText or 'Hello World'
end
-- This function will be called whenever the game enters or refreshes the settings view.
-- The result is expected to be a table of tables that contain settings.
function script:settings()
return {
-- Boolean setting
{
name = 'Bool setting',
value = settings.someBool,
onChange = function(newState) settings.someBool = newState end
},
-- Integer setting
{
name = 'Integer setting',
value = settings.someInt,
values = {1, 2, 3},
onChange = function(newState) settings.someInt = newState end
},
-- Integer setting with custom names
{
name = 'Named integer setting',
value = settings.someNamedInt,
values = {1, 2, 3},
valueNames = {'A', 'B', 'C'},
onChange = function(newState) settings.someNamedInt = newState end
},
-- Setting with multiple string options
{
name = 'Some strings',
value = settings.someString,
values = {'Abc', 'Xyz', '123'},
onChange = function(newState) settings.someString = newState end
},
-- Editable text setting
{
name = 'Generic text',
desc = 'Here you can enter a text:',
value = settings.someGenericText,
onChange = function(newState) settings.someGenericText = newState end,
onReset = function() settings.someGenericText = 'Reset was called' end
},
-- Hint text: no onChange callback
{
name = 'You can customize these settings.'
},
-- Colored link: no value or values
{
name = 'Reset generic text',
color = 'FFAA33',
onChange = function(_, rebuild)
settings.someGenericText = 'Hello World'
rebuild() -- Refresh the settings UI
end
}
}
endUpdate for version 1.12.98
The onChange callback now receives a second argument, rebuild, which can be called to refresh the settings UI. This is useful when a setting opens another dialog, such as a color picker, and the UI needs to reflect changes made there. Existing onChange callbacks continue to work without modification.
Additional features:
- Settings without an onChange callback are displayed as hint text.
- Settings without value and values are displayed as clickable links that trigger onChange.
- Links can be assigned a custom color using the color property, specified as a hex color string.
- If the script owning the draft defines an "icon frame", it is used as the icon for the settings category.
