I made a save and load method for every node that has a value I want to save.
The save method sends the value and the load method receives it and overwrites the current.
func save():
return self.value
func load(value):
self.value = value
(is more complicated in some cases)
There is also a "main" node that goes through all those nodes every time a value changes in one of them (signal), calls their save method and saves the returns in a config file.
onready var nodesToSave = get_tree().get_nodes_in_group("nodesToSave")
func _on_number_value_changed(value):
save_settings()
func save_settings():
for node in nodesToSave:
configFile.set_value("GAME", node.get_name(), node.save())
configFile.save(savePath)
This all works well. The values get saved to the config file in real time.
The main node also calls all load methods in it's _ready method to give the nodes the saved config values back every time the game starts.
func _ready():
load_settings()
func load_settings():
configFile.load(savePath)
for node in nodesToSave:
node.load(configFile.get_value("GAME", node.get_name()))
The problem is that load_settings only changes some of them (or they get rechanged to the default settings).
However when I make sure save_settings doesn't do anything but a valid config file exists,
func save_settings():
pass
load_settings DOES work for everything.
I think it might be that save_settings is called while load_settings is still working because load_settings makes the nodes change their values and with that triggers save_settings (signals).
But that doesn't make sense because when a value hasn't been changed yet, it will just save the default value and get overwritten late, and when it has, it will just save the right value...