Set position of object in changed scene

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

I’m making a portal in my game that brings the player to a different world/scene. When it activates I just change scenes. Problem is I need to change the players position in the new scene. I can’t figure out how to do it without just storing the position on an autoloaded script and then just getting the position in the new scene and setting the position. Is there a better way to do it in the same script with the change_scene code.

:bust_in_silhouette: Reply From: njamster

Is there a better way to do it in the same script with the change_scene code.

No. The script is attached to a node which (if it’s not a Singleton) is part of the current scene, i.e. if you change that scene for something else, the script will be gone.

Instead of changing the full scene, you could swap out parts of it using add_child and remove_child, which will keep the script attached to their parent-nodes in place. That isn’t “better” than using a Singleton though. It’s exactly the same principle.

So its just better to store data in a singleton to share data between scenes?

jujumumu | 2020-04-10 00:46

So its just better to store data in a singleton to share data between scenes?

Better than what? You cannot store any data you want to keep in the node you’re going to swap out or anything attached to it - that’s all. It’s like writing your credit card pin on a five-dollar-bill and then trading that five-dollar-bill for a bunch of Popsicles. You’ll end up with a bunch of Popsicles, but your credit card pin will be gone.

njamster | 2020-04-10 09:27