Skip to main content

GetConfigNumber

This function was added in DSL 1

Description

Return the number associated with key if it can be converted. Otherwise, defaultValue will be returned (if given).

The number can be expressed using hexadecimal if it is preceded by 0x in the config.

Returns the number associated with key in the given config if it can be converted to a number. If the key does not exist or cannot be converted, it returns defaultValue if provided, or nil otherwise.

function GetConfigNumber(config, key, defaultValue) --[[ ... ]] end

Parameters

  • config: userdata - The configuration object from which to retrieve the value.
  • key: string - The key for which the number value is to be retrieved.
  • defaultValue?: any - (Optional) The value to return if the key does not exist or cannot be converted to a number. If not provided, defaults to nil.

Return Values

  • value: number? - The number associated with the key, or the default value if the key does not exist or cannot be converted.

Example

local config = LoadConfigFile('my_config.ini')
local maxPlayers = GetConfigNumber(config, 'max_players', 16)
if maxPlayers then
print('Max players allowed: ' .. maxPlayers)
else
print('Max players not set or invalid, using default value.')
end