-
Notifications
You must be signed in to change notification settings - Fork 2
Mod Settings
Mod Utils includes a new tab in the standard Settings window, labelled "Mods". If your mod has any settings you'd like to make easily configurable, they can show up here!
This menu is currently completely data-driven, so instead of putting code in the init_content function, we'll be making a table and variables above it. When changes are applied in the menu, the variables in your mod script will be changed to match, and they'll even be saved automatically to a special mod_settings.cfg file and restored the next time your mod loads!
Example:
var setting_toggle_test: bool = false
var setting_options_test: int = 0
var setting_slider_test: float = 0.5
var setting_volume_test: float = 1.0
const MODUTILS: Dictionary = {
"settings": [
{
"property": "setting_toggle_test",
"type": "toggle",
"label": "Toggle test",
},
{
"property": "setting_options_test",
"type": "options",
"label": "Multiple choice",
"values": [
0,
1,
2,
],
"value_labels": [
"Default",
"Alternative 1",
"Alternative 2",
],
},
{
"property": "setting_slider_test",
"type": "slider",
"label": "Slider",
"min_value": 0.0,
"max_value": 1.0,
"step": 0.01,
},
{
"property": "setting_volume_test",
"type": "percent_slider",
"label": "Volume",
},
],
}Your mod may use any valid variable names, and any valid ConfigFile value types (string, int, float, bool). I only prefix my setting variables with setting_ for clarity. The menu will automatically update the values only after the user confirms changes should be applied, save them to a mod_settings.cfg file in the user's save directory, and restore the values the next time the game starts.
To access the value from elsewhere, you'll need a reference to your metadata.tres Resource.
This may be obtained through DLC.mods_by_id or by loading the resource yourself from the file.
These should all be equivalent, returning a reference to the same Resource:
var mod_from_dlc: ContentInfo = DLC.mods_by_id["your_mod_id"]
var mod_from_preload: ContentInfo = preload("metadata.tres")
var mod_from_load: ContentInfo = load("res://mods/your_mod_id/metadata.tres")Note that using preload may not be possible if a circular script dependency is created.
If your setting needs to immediately run a script when changed, you can use Godot's standard setget syntax:
var setting_with_callback: int = 0 setget _set_setting_with_callback
# This setter function gets called whenever the value of setting_with_callback is changed externally.
# It will NOT be called if the value is set within the same script file.
func _set_setting_with_callback(value: int) -> void:
# The variable must be explicitly changed in the setter function when one is defined.
setting_with_callback = value
# Now you may execute whatever other changes you'd like to make as a result of the new value.Reference Godot's documentation on this feature of GDScript for more information.