Hi ! I'm trying to load some variables, but can't seem to be able to understand how to do it.
I'm saving like that :
const save_path = "user://savegame.json"
var node_data = {}
func savegame():
var save_game = File.new()
save_game.open(save_path, File.WRITE)
var save_buttons = get_tree().get_nodes_in_group("Persist")
for node in save_buttons:
node_data[node.get_path()] = node.save()
save_game.store_line(to_json(node_data))
save_game.close()
On a singleton, where the func save :
func save():
save_ress = {
"Gold": gold,
"Crystal": crystal,
"Gold_tour": gold_tour,
"Crystal_tour": crystal_tour
}
return save_ress
Is called. So, my save works fine, and my json file got something like that in it (values are atm randomly generated) :
{"/root/World/BackInterface":{"Crystal":200,"Crystaltour":200,"Gold":3000,"Goldtour":1200}}
Now, here is my actual load code, on the same singleton as the save func :
func loadgame():
var save_game = File.new()
#check if there is a file
if not save_game.file_exists("user://savegame.json"):
return
save_game.open(save_path, File.READ)
var button_data = parse_json(save_game.get_as_text())
print(button_data)
save_game.close()
And it'll print the exact text in the json as expected. But now, I'd like to get the values I stored inside it of course ! But when I'm doing something like:
print(button_data["Gold']
It'll print me... the entire line again. So, just in case, I tried to retrieve it directly and assign it to the gold variable on my game, but it said "Invalid get index "Gold" on base : Dictionary", so I assume that it doesn't retrive the value. What am I doing wrong there please ? Tried to look for some youtube videos, but I can't seem to understand the problem, will gladly take any help !