0 votes

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 :/ Hope there is enough info

in Engine by (82 points)
edited by

2 Answers

0 votes
Best answer

I will simply tell you to take a look again in the docs:
https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html
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 posx and posy 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)

by (1,193 points)
selected by

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

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

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

what is your load function?

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

what does the button_data print?

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,"Crystaltour":600,"Gold":600,"Goldtour":600}}

print is printing this exact lineenter image description here
Stats are randomly generated atm

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

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

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

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

0 votes

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.

by (856 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.