-
Notifications
You must be signed in to change notification settings - Fork 4
Creating The Config File
In the _ready() function
- Create variable and store the ConfigFile class in it:
var _config = ConfigFile.new() - Start setting your config values within your config variable:
_config.set_value(...)- There is a very specific way these values need to be stored. We will go over this shortly
- Using
FileAccesscheck to see if the config file exists:if !FileAccess.file_exists(FILE_PATH + "/config.ini"):- If it doesn't exist, using
DirAccess, create the folder that the config files will be stored:DirAccess.open("user://").make_dir(FILE_PATH)and then save the config file to the system:_config.save(FILE_PATH + "/config.ini") - If it does exist, load the configuration file
- If it doesn't exist, using
ExampleModConfig.gd
extends Node
const MOD_ID = "ExampleMod"
const FILE_PATH = "user://MCM/ExampleMod"
func _ready():
var _config = ConfigFile.new()
_config.set_value("String", "testString", {
"name" = "Test String",
"tooltip" = "A test string",
"default" = "Hello World",
"value" = "Hello World"
})
_config.set_value("Int", "testInt", {
"name" = "Test Int",
"tooltip" = "A test int",
"default" = 5,
"value" = 5,
"minRange" = 0,
"maxRange" = 20
})
_config.set_value("Float", "testFloat", {
"name" = "Test Float",
"tooltip" = "A test float",
"default" = 10.3,
"value" = 10.3,
"minRange" = 0,
"maxRange" = 50.5
})
_config.set_value("Bool", "testBool", {
"name" = "Test Bool",
"tooltip" = "A test bool",
"default" = false,
"value" = false
})
_config.set_value("Keycode", "testKeycode", {
"name" = "Test Keycode",
"tooltip" = "A test keycode",
"default" = KEY_ALT,
"value" = KEY_ALT
})
if !FileAccess.file_exists(FILE_PATH + "/config.ini"):
DirAccess.open("user://").make_dir(FILE_PATH)
_config.save(FILE_PATH + "/config.ini")
else:
_config.load(FILE_PATH + "/config.ini")
By default MCM will sort all your values alphabetically based on the set name. So for this example it will currently display the values in this order:
> Test Bool
> Test Float
> Test Int
> Test Keycode
> Test String
However, if you wish to set the order they're sorted yourself all you need to do is add menu_pos when creating the config value. If menu_pos isn't set for a value it will place it under all values that has menu_pos and then alphabetically with everything else that doesn't have menu_pos.
For example if we wish to make sure Test Keycode is always shown at the top of the menu and Test Float below it we would change them to:
...
_config.set_value("Float", "testFloat", {
"name" = "Test Float",
"tooltip" = "A test float",
"default" = 10.3,
"value" = 10.3,
"minRange" = 0,
"maxRange" = 50.5,
"menu_pos" = 2
})
...
_config.set_value("Keycode", "testKeycode", {
"name" = "Test Keycode",
"tooltip" = "A test keycode",
"default" = KEY_ALT,
"value" = KEY_ALT,
"menu_pos" = 1
})
...
Now in the menu it will show up as:
> Test Keycode
> Test Float
> Test Bool
> Test Int
> Test String
MCM Supports 5 different types of values
- String
- Integer
- Float
- Boolean
- Keycodes
Array's of these types are not currently supported.
Instead of just storing a value in a normal variable we create a Dictionary type of structure for every value. This allows MCM to store control variables that is used in the GUI aspect of the mod and allows the author to set default values to be reset if the player wishes.
The way MCM determines what type a value is is based on the Section that is passed in _config.set_value Take a look at the documentation to see what sections are
Because of this it is VERY important that the sections are written exactly as I have them in the examples. If they aren't then they will not be displayed in the MCM menu.
- String
- Int
- Float
- Bool
- Keycode
Let's go over the different types and how they should be set up.