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 a table of tables
return {
-- Let's define a boolean setting
{
-- The name of the setting
name = 'Bool setting',
-- The current value of this setting
value = settings.someBool,
-- onChange is a function that will be called when the values has changed
onChange = function(newState) settings.someBool = newState end
},
-- Let's define an integer setting
{
name = 'Integer setting',
value = settings.someInt,
-- We provide a list of possible values
values = {1, 2, 3},
onChange = function(newState) settings.someInt = newState end
},
-- Let's define an integer setting with custom names for the values
{
name = 'Named integer setting',
value = settings.someNamedInt,
values = {1, 2, 3},
-- We provide a list names for the of possible values that we've specified
valueNames = {'A', 'B', 'C'},
onChange = function(newState) settings.someNamedInt = newState end
},
-- Let's define a setting with multiple string options
{
name = 'Some strings',
value = settings.someString,
values = {'Abc', 'Xyz', '123'},
onChange = function(newState) settings.someString = newState end
},
-- Let's define a setting for generic text that can be edited
{
name = 'Generic text',
-- This description will be shown when user enters a new text
desc = 'Here you can enter a text:',
value = settings.someGenericText,
onChange = function(newState) settings.someGenericText = newState end,
-- onReset is a function that will be called when users presses the settings reset button
onReset = function() settings.someGenericText = 'Reset was called' end
}
}
endStarting 1.12.98 the onChange callback function takes a second argument called rebuild. It's a function that can be called to manually issue a UI rebuild. It can be useful when changes happen later, eg when the action triggers a color picker whose color should be applied to UI immediately after closing the dialog.