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

Adds the Settings entry to the tab menu.
Updates the settings menu, if it is open, when the value of a setting has changed.
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.
Runs the RegisterClientSettings hook so that plugins and the schema can register their settings.
Writes the settings to the data store when the game shuts down while a change is still waiting to be saved.

Methods

all()shared
Returns every registered setting.
find(id)shared
Returns the definition of a registered setting.
Returns the definition of a type of settings.
Returns the value of a setting: what the player has set it to, or its default.
Groups the settings that are listed in the settings menu by category.
Checks whether a setting is at its default value.
Checks whether a setting is listed in the settings menu: it is unless it has a visible function that returns a falsy value.
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.
Registers a setting.
Registers a type of settings: how the values of such settings are checked and which control edits them in the settings menu.
Removes a setting, so that it is no longer listed in the menu, read or networked.
reset(id)client
Sets a setting back to its default value by forgetting what the player has set it to.
Checks a value against the definition of a setting.
save()client
Writes the values of the settings to the 'settings' file of the client data store.
Sets the value of a setting.
sync()client
Sends the networked settings that have changed since the last time to the server.
Sends the value of every networked setting to the server.

Hooks

ClientSettings:AddTabMenuItems(menu)

client plugins/settings/plugin/cl_hooks.lua:7

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:34

Implements 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

ClientSettings:InitPostEntity()

client plugins/settings/plugin/cl_hooks.lua:18

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.

ClientSettings:OnSchemaLoaded()

shared plugins/settings/plugin/sh_plugin.lua:31

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.

ClientSettings:ShutDown()

client plugins/settings/plugin/cl_hooks.lua:24

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

ClientSettings:find(id)

shared plugins/settings/plugin/lib/sh_settings.lua:194

Returns the definition of a registered setting.

Parameters

  • id String setting id

Returns

  • Map setting definition, or nil if it is not registered

ClientSettings:find_type(id)

shared plugins/settings/plugin/lib/sh_settings.lua:58

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

ClientSettings:get(id, default)

client plugins/settings/plugin/cl_plugin.lua:89

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 nil returned if the setting is not registered

Returns

  • Any value of the setting

ClientSettings:get_categories()

client plugins/settings/plugin/cl_plugin.lua:207

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)

ClientSettings:is_default(id)

client plugins/settings/plugin/cl_plugin.lua:108

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

ClientSettings:is_visible(id)

client plugins/settings/plugin/cl_plugin.lua:188

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

ClientSettings:load_value(id)

client plugins/settings/plugin/cl_plugin.lua:70

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

ClientSettings:register_setting(id, data)

shared plugins/settings/plugin/lib/sh_settings.lua:116

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

ClientSettings:register_type(id, data)

shared plugins/settings/plugin/lib/sh_settings.lua:47

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)

ClientSettings:remove_setting(id)

shared plugins/settings/plugin/lib/sh_settings.lua:177

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

ClientSettings:reset(id)

client plugins/settings/plugin/cl_plugin.lua:159

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

ClientSettings:sanitize(id, value)

shared plugins/settings/plugin/lib/sh_settings.lua:203

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

ClientSettings:save()

client plugins/settings/plugin/cl_plugin.lua:245

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.

ClientSettings:set(id, value)

client plugins/settings/plugin/cl_plugin.lua:125

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

ClientSettings:sync()

client plugins/settings/plugin/cl_plugin.lua:253

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.

ClientSettings:sync_all()

client plugins/settings/plugin/cl_plugin.lua:278

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.

Defined in