+2 votes

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!

asked Jul 8 in Engine by Panda_Scientist (25 points)

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!

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.

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.

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.

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.

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

1 Answer

0 votes

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 "nextplayerloc" 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, "nextplayerloc" 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.

answered Jul 8 by Xenm (18 points)

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

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.