0 votes

Howdy. I've been following the saving games docs page and when loading, any members of the "Persist" group get instanced at position (0,0).
I read somewhere about setting positions prior to calling AddChild, and so swapped the lines around in the load function but it doesn't seem to make any difference. I have also tried using GlobalPosition instead of Position but again, no dice.

The relevant section of my "load" function (unchanged from the docs page example) looks like this:

while (saveGame.GetPosition() < saveGame.GetLen())
    // Get the saved dictionary from the next line in the save file
    var nodeData = new Godot.Collections.Dictionary<string, object>((Godot.Collections.Dictionary)JSON.Parse(saveGame.GetLine()).Result);

    // Firstly, we need to create the object and add it to the tree and set its position.
    var newObjectScene = (PackedScene)ResourceLoader.Load(nodeData["Filename"].ToString());
    var newObject = (Node)newObjectScene.Instance();
    newObject.Set("Position", new Vector2((float)nodeData["PosX"], (float)nodeData["PosY"]));

All variables seem to be fine if I GD.print them and the savegame file looks fine:


Where might I be going wrong here?


Godot version 3.4.4 stable.mono.official
in Engine by (15 points)

Setting position must come after adding child.
Did You try to print get("position") ?
Or print after load function to ensure it is called ?
Perhaps it is only visual effect

Thanks for the response. As I said, I did try switching the addchild and position lines and it didn't make any difference.
The position is correct in the save file, as shown above.
A print from the load function shows the correct value for position.
The section of load function above is taken verbatim from the docs page, hence my confusion.

If printed loaded position is correct, that the problem must happen inbetween loading and instantiating. Are You sure You don't set position elsewehere in code ? Most likely in loaded objects ready() function ?
Also, ate You sure You are following the correct node on screen ? Or can there be an old node, that never changed position from 0,0 ?

Hey. Thanks for the help so far. Turns out I was lying - unintentionally, I assure you!
The position returns as null.
I've since noticed that my instanced scene is oddly named:
weird, eh?
I've tried to set the name using

newObject.Set("Name", nodeData["Name"]);

of course, ensuring the savefile contains that data, but it still turns out like above - I assume this is an effect of the instancing - prior to the save/load calls, it simply shows up as "object".

1 Answer

+1 vote
Best answer

I think your problem is that you are referring to "Position" and not "position". Case sensitivity is important.

by (141 points)
selected by

Silly me. Works perfectly with "position". Many thanks

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.