0 votes

I'm trying to save a node and its children, but only the Parent is saved:

func save():
    var save = PackedScene.new()
    save.pack(self);

    ResourceSaver.save(save, "res://lista de clientes/JDC.tscn");
Godot version 4.0
in Engine by (12 points)

1 Answer

+1 vote

There is one step you are missing here to include a nodes children, that would be to set the owner. So using your example code here:

func save():
    var save = PackedScene.new()
    for c in self.get_children():
        c.set_owner(self)
    save.pack(self);

    ResourceSaver.save(save, "res://lista de clientes/JDC.tscn");

Remember to set the owner before you pack it, this will now save itself and it's immediate children. As a side note, if c in this case has children, you will also need to loop through these and set owner to self so the for loop would then become

for c in self.get_children():
            c.set_owner(self)
            for a in c.get_children():
                a.set_owner(self)
by (262 points)
edited by

Okay, it kinda worked, but when it loads, all the variables were reset to the default

if your variable is exported, it will have the appropriate flags set to be saved

Okay, i used the @export thingy on the variables i wanted and it kinda works

But everytime i use it in a array that stores the Nodes inside the parent variable it crashes

It might me because i'm declaring the array wrong

@export var clientes:=[];

Nope, you are declaring it correctly. When I export and use an array, the same one you posted, it seems to save just fine with its children and a random value appended to it just fine. I tried with the node paths and it was fine, as well as getting a node with $ and storing it. Getting the node of course caused an empty entry, but it did not crash.

Could I see the code as you are writing it, so I can try to replicate this crash and if so, help try to find the reason why.

I noticed i was doing an infinite loop inside the object, sorry XD.

But thanks, you were of immense help!!

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 Frequently asked questions and 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 [email protected] with your username.