Registry of the Settings plugin. A setting is registered with
ClientSettings:register_setting from the RegisterClientSettings hook. It has a type
('boolean', 'number', 'choice' or 'string' out of the box, more can be added with
ClientSettings:register_type), a default value, a category, a name and a description, and
optionally a function that decides whether the player sees it in the menu and a function
that is called when its value changes.
Summary
Hooks
Methods
visible function that returns a falsy value.Hooks
Implements hook AddTabMenuItems
Adds the Settings entry to the tab menu.
Parameters
- menu Panel the tab menu
ClientSettings:ClientSettingChanged(id, _value, _old_value)
client plugins/settings/plugin/cl_hooks.lua:34Implements hook ClientSettingChanged
Updates the settings menu, if it is open, when the value of a setting has changed.
Parameters
- id String setting id
- _value Any new value of the setting
- _old_value Any previous value of the setting
Sends the networked settings to the server as soon as the client is able to, so that the server knows them before it runs PlayerInitialized for the local player.
Implements hook OnSchemaLoaded
Runs the RegisterClientSettings hook so that plugins and the schema can register their settings. On the client it then sends the networked settings to the server again if they have been sent before, which is the case after a code refresh.
Writes the settings to the data store when the game shuts down while a change is still waiting to be saved.
Methods
Returns every registered setting.
Returns
- Map setting definitions keyed by setting id
Returns the definition of a registered setting.
Parameters
- id String setting id
Returns
- Map setting definition, or nil if it is not registered
Returns the definition of a type of settings.
Parameters
- id String type id
Returns
- Map type definition, or nil if there is no such type
Returns the value of a setting: what the player has set it to, or its default.
function MyPlugin:HUDPaint()
if ClientSettings:get('my_plugin_clock') then
draw.SimpleText(os.date('%H:%M'), Theme.get_font('text_small'), 16, 16, color_white)
end
end
Parameters
- id String setting id
- default Any optional, defaults to
nilreturned if the setting is not registered
Returns
- Any value of the setting
Groups the settings that are listed in the settings menu by category. The categories are sorted by their translated name, the settings of a category by the order in which they have been registered.
Returns
- List<Map> categories: id (String category phrase), name (String translated name) and settings (List<Map> setting definitions)
Checks whether a setting is at its default value.
Parameters
- id String setting id
Returns
- Boolean true if it is, also if the setting is not registered
Checks whether a setting is listed in the settings menu: it is unless it has a
visible function that returns a falsy value. A setting that is not listed keeps its
value and can still be read and set from code.
Parameters
- id String setting id
Returns
- Boolean false if the setting is hidden or not registered
Reads the saved value of a setting again and checks it against the definition of the setting; a saved value that is not valid for the setting is ignored. Called when a setting is registered or removed.
Parameters
- id String setting id
Registers a setting. Meant to be called from the RegisterClientSettings hook.
Registering an ID again replaces the setting but keeps its place in the menu.
function MyPlugin:RegisterClientSettings()
ClientSettings:register_setting('my_plugin_clock', {
type = 'boolean',
default = true,
category = 'settings.categories.interface',
name = 'settings.my_plugin_clock.name',
description = 'settings.my_plugin_clock.desc'
})
ClientSettings:register_setting('my_plugin_clock_scale', {
type = 'number',
default = 1,
min = 0.5,
max = 2,
decimals = 1,
category = 'settings.categories.interface',
name = 'settings.my_plugin_clock_scale.name',
-- Only listed in the menu while the clock is turned on.
visible = function(setting)
return ClientSettings:get('my_plugin_clock')
end,
on_change = function(value, old_value, setting)
MyPlugin:rebuild_clock(value)
end
})
ClientSettings:register_setting('my_plugin_clock_format', {
type = 'choice',
default = '24h',
choices = {
{ value = '12h', name = 'settings.my_plugin_clock_format.twelve' },
{ value = '24h', name = 'settings.my_plugin_clock_format.twenty_four' }
},
category = 'settings.categories.interface',
name = 'settings.my_plugin_clock_format.name',
-- The server can read it with target:get_setting('my_plugin_clock_format').
networked = true
})
end
Parameters
- id String unique setting id, also the key its value is saved under
- data Map setting definition. type (String) is required. Optional: default (Any, the type decides if left out), category (String phrase, 'settings.categories.general' by default), name (String phrase, the id by default), description (String phrase, shown as a tooltip), networked (Boolean send the value to the server), visible (Function(setting) return a falsy value to leave the setting out of the menu; clientside), on_change (Function(value, old_value, setting) called on the client when the value changes). By type: min (0), max (100) and decimals (0) for 'number'; choices (List of values or of { value, name } tables) for 'choice'; max_length (128) for 'string'
Returns
- Map the registered setting, or nil if it could not be registered
Registers a type of settings: how the values of such settings are checked and which control edits them in the settings menu.
ClientSettings:register_type('boolean', {
-- Optional. Fills in the fields of a setting of this type when it is registered;
-- return false to refuse the setting.
setup = function(setting) end,
-- Optional. Returns the default of a setting that does not define a valid one.
get_default = function(setting)
return false
end,
-- Returns the value if it is valid for the setting (it may correct it first),
-- or nil if it is not.
sanitize = function(setting, value)
if isbool(value) then
return value
end
end,
-- Clientside. Creates the control of the setting for the settings menu. The control
-- has to show the given value and call on_change with the new one when the player
-- changes it.
create_control = function(setting, parent, value, on_change)
-- ...
end
})
The values of a setting have to be booleans, numbers or strings.
Parameters
- id String unique type id
- data Map type definition: sanitize(setting, value) and the optional setup(setting), get_default(setting) and create_control(setting, parent, value, on_change)
Removes a setting, so that it is no longer listed in the menu, read or networked. The value the player has saved for it is kept in case the setting comes back.
Parameters
- id String setting id
Sets a setting back to its default value by forgetting what the player has set it to. Runs the same callbacks as ClientSettings:set if that changes the value.
Parameters
- id String setting id
Returns
- Boolean true if the setting has been reset, false if it is not registered
Checks a value against the definition of a setting.
Parameters
- id String setting id
- value Any value to check
Returns
- Any the value as the setting would store it (a number is clamped and rounded, for example), or nil if the setting is not registered or the value is not valid for it
Writes the values of the settings to the 'settings' file of the client data store. Called a second after a value has changed and when the game shuts down.
Sets the value of a setting. The value is checked against the definition of the setting
first. If it differs from the current one, the on_change function of the setting and
the ClientSettingChanged hook run, and a networked setting is sent to the server.
Parameters
- id String setting id
- value Any new value
Returns
- Boolean true if the value has been set, false if the setting is not registered or the value is not valid for it
Sends the networked settings that have changed since the last time to the server. Called a quarter of a second after a networked setting has changed.
Sends the value of every networked setting to the server. Called when the map's entities have been created, which is when the client is first able to tell the server, and when the settings are registered again after a code refresh.