0 votes

So when I was using GameMaker, GM have this "Persistent" option that basically makes your object("Player") persistent through scenes. It would retain the position and state of the object when it leaves the scene, and when it goes back.

My question is, does Godot have this function?
If not, how would you go about doing this?

in Engine by (36 points)

1 Answer

+1 vote
Best answer

I see a few options for persistent nodes (not just a player):

1) Use an auto-load singleton, which will make your node present in all scenes of your game (even the main menu though, if you have one). Documentation here: http://docs.godotengine.org/en/stable/learning/step_by_step/singletons_autoload.html

Next solutions require a bit of scripting:

2) Dynamically instance the player, remove it from the tree (remove_child) before changing scene (it won't delete it), and add it back in the scene once it is loaded (add_child). That requires a bit of code in a singleton (like in 1) )and the setup of "spawn points" using Position2D for example, so that you know where to put the node.

3) Don't use change_scene, because as you noticed it replace everything by a new scene. But because Godot games are scene trees, an alternative is to have one scene that removes and loads the others under a child node. This way, the "container" can have common nodes that will be preserved because they are in a parent or another branch. I use this approach in my own game to preserve UI and global effects because they are common to all levels and won't need to be reloaded everytime. One drawback is that scenes are less likely to be tested independently because they require to be in their "container" at runtime, but I worked around it somehow.

by (28,791 points)
selected by

Thank you. This is very helpful.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.