Problems about Saves

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rensae

Hi ! ~ I’ve some troubles figuring out why godot is actually doing this. Here’s my code for saving :

 `var save_game = File.new()
	save_game.open_encrypted_with_pass(save_path, File.WRITE, "Trololo")
	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()`

with a var node_data = {}

Now, it saves without any troubles, besides that… it;ll save like that :
Line 1 : [Path] + data
Line 2 : [Path] + data + [Path other node] + data

And it’ll do that with every node, just repeating them every line and adding a new one.

Each node has a func save() returning their variables, looking like that :

    func save():
var d = {
"Var1": var1,
"Var2": var2
}
return d

Because of that, I don’t really know how to load my variables and add them to each of the objects :confused: Hope there is enough info

:bust_in_silhouette: Reply From: rustyStriker

I will simply tell you to take a look again in the docs:

Do note a couple things:
first, we are saving variables not a whole scene
second, you cant save complex stuff like vectors(that is why the pos_x and pos_y in the demo)
third and MOST IMPORTANT: you need something to craft the nodes back as we are saving properties…

besides, your problem is because you create a dictionary and saves the full dict every time instead of the new line only, you wanna save the node.save() output not the full dict(or save only the full dict)

But, if the nodes are already there when I’m loading the scene : I should be able to just load out variables in them right ?

Thanks for the answer, I’ll try that right now !
EDIT : It worked out, it stores them on a single line now

Rensae | 2020-06-21 21:15

If your nodes are already in the scene you can load it directly to them, yes

rustyStriker | 2020-06-22 06:32

Okay, thanks ! Now, can I bother you on explaining how to load those variables ?

I’m trying to get them with the get_as_text() function, and when I print it there is no problem
But, for exemple, there is “Gold”: xx inside of this dictionnary, and if I want to print(data["Gold"]) it’ll tell me there is an index error on dictionnary.
I tried to make it str, doesn’t work, am I missing something ? I’m kinda dumb on that, it’s my first time ever using a save / load function and I really don’t understand it

Rensae | 2020-06-22 13:10

what is your load function?

rustyStriker | 2020-06-22 13:37

func loadgame():
var save_game = File.new()
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()

I deleted the pass since it was for testing purposes on the save function, so it isn’t here anymore

Rensae | 2020-06-22 13:57

what does the button_data print?

rustyStriker | 2020-06-22 14:25

func save():
save_ress = {
	"Gold": gold,
	"Crystal": crystal,
	"Gold_tour": gold_tour,
	"Crystal_tour": crystal_tour
}

return save_ress

Here is what I store on my save, so my save contains

{“/root/World/BackInterface”:{“Crystal”:1300,“Crystal_tour”:600,“Gold”:600,“Gold_tour”:600}}

print is printing this exact line


Stats are randomly generated atm

Rensae | 2020-06-22 14:30

So, the problem you have is, you saved a json string as a value… what you want to do is take the data and do something like this(for the way you save at lease):

 data["/root/World/BackInterface"]["Gold"]

since you saved a json as a value of a key

rustyStriker | 2020-06-22 14:56

It doesn’t seem to work, or I’m misstyping something there:

save_game.open(save_path, File.READ)
var button_data = parse_json(save_game.get_as_text())
var Gold = button_data["/root/World/BackInterface"]["Gold"]
print(Gold)

It print the exact same thing :x

Rensae | 2020-06-22 15:39

try parsing the json inside the button_data[“/root/World/BackInterface”] as well

rustyStriker | 2020-06-22 17:03

save_game.open(save_path, File.READ)
var button_data = parse_json(save_game.get_as_text())
var Gold = parse_json(button_data["/root/World/BackInterface"])
print(Gold)
save_game.close()

Still printing the same entire line. I really hope I’m not misstyping and that is actually something I just don’t understand

Rensae | 2020-06-22 20:28

:bust_in_silhouette: Reply From: JimArtificer

In your for loop you are constructing node_data by assigning to it for each node. It looks like you are also calling store_line for each node. I don’t think you need to do both.

Try either pulling store_line out of the loop or just store node.save() directly without building up a data structure first.

Thanks a lot for your answer, it helped me comprehend a bit better the uses of each of those lines :slight_smile:

Rensae | 2020-06-21 21:24