Make reading config values for minetest easy!
If you have a config file with settings names like this:
my_mod_name.my_setting_name = some value
my_mod_name.setting_category.another_setting_name = 345
...
You can parse them by declaring a settings model like this:
local settings = config.settings_model('my_mod_name', {
my_setting_name = config.types.string("default_value"),
setting_category = {
another_setting_name = config.types.integer(123, { min=100, max=1000 })
}
...
})
print(dump(settings))
-- Output:
--{
-- my_setting_name = "some value",
-- setting_category = {
-- another_setting_name = 345
-- }
--}
config.types.some_type(default_value[, setting_options, setting_transformer])
Create a type instance, some_type
should be the name of a real type, like string
or number
, see "Default Types" for a full list
any
:default_value
| the fallback value if none is configuredtable
:setting_options
| the customisation options, specific for each type, see "Default Types" for a full listany
:setting_transformer
| a transformation map that the value will be passed through before returning
config.settings_model(root_name, settings_config)
Parse all the settings in the structure of the settings_config given
string
:root_name
| The base name for the settings, usually the mod name. Like:root_name.sub_group.my_setting_name
table
:settings_config
| The structure of the settings for this root_name. Like:{ sub_group = { my_setting_name = config.types.number(9) } }
config.register_type(names[, parser, getter])
Register a new data type, see "Default Types" (below) for examples of what this can be used for
string|string[]
:names
| The name, or list of names for this type. Each type can have multiple aliases that all map to the same thing, e.g. bool, booleanfunction
:parser
| The function that will parse the data retrieved from the settings, can be used to parse lists and other data types from stringsfunction
:getter
| The function used to retrieve data from the settings, should have a fallback for older versions
config.register_transformer_type(type_string, type_transformer)
Register a custom transformer type, see "Transformer Types" (below) for more information
string
:type_string
| The name of the type this transformer will accept as its first argument (user_given_mapping), there can only be one for each typefunction
:type_transformer
| The type_transformer should be a function that takes the arguments `(user_given_mapping, setting_value, setting_name, setting_opts)`` and returns the transformed value
You can create your own data types using config.register_type
(see above), but there are many that come built-in:
The simplest type, performs no parsing and just reads in the setting
string
str
None
A true or false value
boolean
bool
None
Any number
number
num
float
decimal
number
:min
| The minimum value the number can takenumber
:max
| The maximum value the number can take
A number with no decimal places / fractional part
integer
int
number
:min
| The minimum value the integer can takenumber
:max
| The maximum value the integer can take
Choose one string out of a list of possible values
enumerable
enum
table
:options
| the list of possible values it can take
A spatial vector, with x, y and z coordinates
vector
vec3
vec
position
pos
v3f
number
:min
| The minimum value each number in the vector can takenumber
:max
| The maximum value each number in the vector can take
A color value, just a string for now
color
colour
None
A list of values, all with the same type. Separated by a comma by default, but can be configured
list
array
multiple
string
:separator
| The character the list is separated bynumber
:length
| The fixed length of the list. If provided any extra values will be discarded and any missing values will be filled in by the defaulttype
:type
| The data type of the members of the list, defined throughconfig.types...
A literal lua table, will be parsed from the serialised string
table
luatable
None
Transformer types are for mappings from human input values to computer values - so you input human values in the settings and have them mapped into the correspoding computer values in the final settings table
A use for this would be mapping "south", "west", "north", "east"
into numbers representing the param2 for facedir, 0, 1, 2, 3
.
This would be accomplished by adding a transformer for the table (this should already be available in the default types):
config.register_transformer_type("table",
function (user_given_mapping, setting_value, setting_name, setting_opts)
return user_given_mapping[setting_value]
end)
Then creating a type with the table mapping as the third argument
initial_orientation = config.types.string("north", nil, { south=0, west=1, north=2, east=3 })
If the setting this reads from is set to "east", then the final result will be 3 If the setting this reads from is not set, then the final result will be 2 (because "north" is the default, and maps to 2)
Note that type here is "string", the type of the data read in from the settings, NOT the type of the data it will be transformed into. This type can be anything and is of no concern to this system.
A table transformer will look the config value up in the table you provide, as the key in the table, it will return the corresponding value.
A function transformer will pass the config value into the function you provide, along with the name of the config value and the settings object passed into the data type definition