Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | anssiko |
Hi,
I’m trying to save and load visited checkpoints with config file. I’m trying to create a scalable solution, so that I can save other data as well in future. My game is a very simple platformer, so I thought config is a working solution for saving.
At the moment, saving works (for path res://, but no file appears anywhere for path user://) to some extent. Load does not. I am developing on 3.2 and osx.
Considering the code below:
-
Why is load not working? (I’m trying to run it from _ready, and it swipes clear the config file.)
-
Why is save not adding to the list, but swiping any previous data clear and then saving only new data when the game is run?
extends Node
const SAVE_PATH = "res://config.cfg"
var level: Node2D = null
var config = ConfigFile.new()
var visited_checkpoints := {}
var _checkpoints = {
"SAVEDATA": {
"Visited Checkpoints": visited_checkpoints
}
}
func _ready():
Events.connect("checkpoint_visited", self, "_on_Events_checkpoint_visited")
load_checkpoints()
func _on_Events_checkpoint_visited(checkpoint_name: String) -> void:
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():
for section in _checkpoints.keys():
for key in _checkpoints[section].keys():
_checkpoints[section][key] = config.get_value(section,key)
Bonus question: how can I later check whether a checkpoint has been visited or not?
anssiko | 2020-03-27 09:12
You can edit your question by clicking at the three little dots in the bottom right corner and choosing “Edit”. Providing an answer to your own question that doesn’t solve the issue will decrease your question’s visibility as it won’t appear here any longer.
njamster | 2020-03-27 11:01