Changing levels (scenes) with player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dejvid_bejlej
:warning: Old Version Published before Godot 3 was released.

How can I change current scene and “take” player node with me?
Let’s say, when player scene instanced in lvl1 enters Area2D, current scene changes to lvl2.
I know how to change the scene, but I wonder if instead of instancing player in new lvl (this way I’ll loose all his stats like hp that are stored in script attached to player) I can transport it from last level (something like reparent player scene)
Or should I store all player stats in a global script and load it every time I change level?

:bust_in_silhouette: Reply From: avencherus

It depends on how you organize it, and how much data is involved, but you can do it either way.

While you’re doing some scene switching, you can store a reference to the character in a variable. Then make sure you use remove_child() for the character node from the scene before freeing the scene it was a child inside. This will keep the character from getting freed as part of the scene.

Then when the new scene is loaded you can add back that character from that variable using add_child()

As long as you hold some reference to your character, and remove it from a scene, it should remain intact.

Not sure why you were downvoted. Upvoted.

wombatTurkey | 2017-12-28 03:54

:bust_in_silhouette: Reply From: MrMonk

the way I do it, is to store player data in a global variable and actually save the data in a user file from time to time. I do this because I don’t want the user to lose his gains due to an unexpected game close event (on mobile I think is especially important). I also find it very easy to save as… the previous level into a new level and already have all the elements in there. What you’re thinking of is doable, I have a custom change scene script, that will transport data from old scene to the new scene, so definitely can be done, but it does not seem safe to do it that way…