C# problem with save/load (node positions set to 0,0)

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

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?


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

Inces | 2022-07-28 17:23

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.

broslovski | 2022-07-28 17:30

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 ?

Inces | 2022-07-28 18:23

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”.

broslovski | 2022-07-29 09:27

:bust_in_silhouette: Reply From: zenbobilly

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

Silly me. Works perfectly with “position”. Many thanks

broslovski | 2022-07-29 12:01