save_settings()
looks correct, it saves the current values stored in _settings
.
However load_settings
won't work if settings were never saved beforehand. It doesn't check if a file exists, and doesn't load one either. Indeed, it only reads values from _config_file
(wether it has data in it or not) and returns them as an array, which you don't even use.
Then in _ready
, you call load_settings()
first (which does nothing then), and save_settings()
, which saves the current settings at that time, which are most likely the default ones because _settings
hasn't been updated at all by load_settings()
.
Now if you change FPSview = true
, you change the variable, but not the value stored in _settings
, which is something else. Indeed, when you declared the contents of _settings
earlier, GDScript copies the values into the dictionary, because primitive types (float, int, Vector2, Vector3) are always handled by value. If you want that value to change, you need to change the contents of _settings
, not another variable.
Finally, when saving you delete the file, wait a second and save it. That works, but deleting and waiting aren't necessary. You can just call save_settings()
.
Now, to fix all this:
To modify _settings
directly, do this:
func _on_fpsOn_pressed():
# No need to use extra variables
_settings.test.fps = true
To load them correctly:
func load_settings():
var err = _config_file.load(SAVE_PATH)
if err != OK:
# Likely no settings were saved yet
return err
for section in _settings.keys():
for key in _settings[section].keys():
var val = _settings[section][key]
_settings[section][key] = _config_file.get_value(section, key, val)
return OK