1) Why is load[_checkpoints]
not working? (I'm trying to run it from _ready
, and it swipes clear the config file.)
First of, you don't call config.save(SAVE_PATH)
. Secondly, it seems the key "Visited Checkpoints" is accessed as "VisitedCheckpoints" (without a space!) even though it is written to the config file as "Visited Checkpoints". You can see that for yourself by using config.get_section_keys("SAVEDATA")
, which will print you the array of keys in that section. Lastly, when you visit a checkpoint, you insert the information about that checkpoint in a dict called visited_checkpoints
and seem to assume that this will also change the value of _checkpoints["SAVEDATA"]["VisitedCheckpoints"]
. However, that's not the case! You're working with a copy, not a reference.
So here's a version that should work:
extends Node
const SAVE_PATH = "res://config.cfg"
var level: Node2D = null
var config = ConfigFile.new()
var _checkpoints = {
"SAVEDATA": {
"VisitedCheckpoints": { }
}
}
func _ready():
load_checkpoints()
Events.connect("checkpoint_visited", self, "_on_Events_checkpoint_visited")
func _on_Events_checkpoint_visited(checkpoint_name: String) -> void:
var visited_checkpoints = _checkpoints["SAVEDATA"]["VisitedCheckpoints"]
visited_checkpoints[level.name] = visited_checkpoints.get(level.name, [])
visited_checkpoints[level.name].push_back(checkpoint_name)
save_checkpoints()
func save_checkpoints():
for section in _checkpoints.keys():
for key in _checkpoints[section].keys():
config.set_value(section, key, _checkpoints[section][key])
config.save(SAVE_PATH)
func load_checkpoints():
config.load(SAVE_PATH)
for section in _checkpoints.keys():
for key in _checkpoints[section].keys():
_checkpoints[section][key] = config.get_value(section,key)
2) Why is save[_checkpoints]
not adding to the list, but swiping any previous data clear and then saving only new data when the game is run?
Now that the loading works this shouldn't be the case anymore. Note that checkpoints will be added multiple times to the array, if they player visits them multiple times. In case you don't want this, you'll have to change your callback:
func _on_Events_checkpoint_visited(checkpoint_name: String) -> void:
var visited_checkpoints = _checkpoints["SAVEDATA"]["VisitedCheckpoints"]
visited_checkpoints[level.name] = visited_checkpoints.get(level.name, [])
if not visited_checkpoints[level_name].has(checkpoint_name):
visited_checkpoints[level.name].push_back(checkpoint_name)
save_checkpoints()
Bonus question: how can I later check whether a checkpoint has been visited or not?
Just check the dictionary for the name of the checkpoint:
var visited_checkpoints = _checkpoints["SAVEDATA"]["VisitedCheckpoints"]
if visited_checkpoints[level_name].has(checkpoint_name):
...