How do I save the positions of instances in a scene and come back to them later?

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

I made two scenes, one with a door and some tiles - lets call this scene Main. That door, when activated, changes the scene and brings the character into another scene with another door and some tiles - lets call this scene House. In Main, when I first start, I spawn far from the door, which is how I set the scene up. I go through the door in Main, and appear on the door in House which is where I placed the character in the scene.

Here’s the problem.

When I go back through, I want to reappear where the door is in Main, but instead I appear in the starting position, far from the door.

I’ve seen many things about PackedScene having functionality similar to what I want here, but even after reading the docs I can’t figure a way to implement them.

I need an easy way to save the state of every instance under Main so I don’t want to save the position of everything in a global variable, because that would make for some very messy code and I may need to save more things for every instance, such as ammo or chest opened states and such. If PackedScene is the way to do this, I would very much appreciate a detailed explanation on how to use them because I don’t really understand them.

Thank you so much in advance!

I found an answer to this by having the doors teleport me within one scene. It works well, but it doesn’t feel like I actually solved the problem. Any help would still greatly be appreciated!

Panda_Scientist | 2020-07-08 15:47

You could consider adding alternative spawning locations to your scenes…
In other words scene internal coordinate variables which you can set your ‘players’ coordinates to when loading the scene.

Something like:
entryPointA = Vector3(<some coords>)
entryPointB = Vector3(<some other coords)

And call the one you want in the func ready(): function.

But when dealing with more variables or loads of entry points packed scenes or global variables may be more useful.

Arandual | 2020-07-08 19:07

That does seem like a very viable option, thank you for that! If I change it back to separate scenes for each area then I will definitely try that.

One thing that I would like to happen is the entire scene get saved, so all of the states for all of the instances stay the same, so even placements of enemies and other objects stay in the world where I left them. Your suggestion is an interesting concept and certainly solves the problem of spawning at the right door, though.

Panda_Scientist | 2020-07-08 19:43

My suggestion works well for a small amount of specific things, but if you really dynamically want to save enemy positions and the like then this would very quickly become a chaotic mess.

Still I am happy I could contribute at least a little!

Unfortunately I do not really have experience with packed scenes or saving such things dynamically, but I wish you the best still.

Arandual | 2020-07-08 20:18

Oh that’s OK! Talking with you was a pleasure. Hopefully we can find an answer to this problem soon because it seems like something that could be very useful in future projects. It seems like something that would be pretty easy to do but it’s just kind of not.

Panda_Scientist | 2020-07-08 20:44

Thank you for your kind words, good luck on your quest to find a solution!

Arandual | 2020-07-09 11:48

:bust_in_silhouette: Reply From: Xenm

I would use a singleton. A master that is persistent between scenes. So you can pass stuff to him when changing scenes, that you want to happen in the next scene.

When the player teleport you would go:

Master.next_player_loc = #where this door leaves him

As the Master beeing your singleton.
And when everything is loaded, for example the player, he emits a signal that the Master is listening.

Master script:

func _ready():
    player.connect("ready", self, "realocate_player")
func realocate_player():
    player.global_position = next_player_loc

This “next_player_loc” was setted by the door when it was activated.

So in the function called by this signal you would realocate the player in the right place, note that if the camera has smoothness, and its loaded first, I think that the player may “teleport”, and the camera will not. It will smooth its way to where you put the player.

I think there is some bugs here, for example, “next_player_loc” needs firstly to have the starting position, as when you load the scene for the first time, you want him to appear there. And everytime that you move scenes you will need to be carefull to update this variable, as if you don’t do it, he will appear at the last place you setted.

Although not a bad idea, this becomes messy if you want to save the states of say 100 different scenes.

Arandual | 2020-07-09 11:50