I see this topic come very often, but keep in mind Godot does not enforce any concept of “current scene and no other is open”. It’s just a scene tree, and you can put scene instances or nodes on any branch you want. TL;DR,
change_scene() is just a shorthand for replacing one of the child nodes of the tree root.
If you want to have two scenes available at a time, just instance them both as child of the root node. Or as child of whatever node you consider as “current world”.
To prepare your map in the background, you could also have a singleton node (which is just a shorthand for adding other nodes as child of the root) and put your loading scene as child of that singleton, hidden, so that it can be accessed while you present the other scene. One drawback of adding to the tree is that you need to make sure the logic in that next scene does not run while it loads (if you have scripts in it for example).
You could also not add your other scene to the tree at all, just instance it and keep it in a variable. You should be able to prepare it this way, as long as no logic in that scene needs to access the full tree (if it does, either make it so it doesn’t need to, or use the other options I explained).
Hey I really appreciate your answer being so concise while also covering a lot of my questions. How would I go about setting a node in another scene? What would be the syntax with the Singleton method? I know how to instance them but I’ve always just used them as global variables.
It really depends what you want to do. Did you try
change_scene() to begin with? If your loading doesn’t need to be asynchronous, it would be equivalent to this:
# Load and instance next scene
var next_scene = load("res://other_scene.tscn")
# Call a function on the root node of the next scene,
# so it can load its stuff. You can add argument if you wish.
# Schedule removal of the current scene
# Add next scene
get_tree().get_root().current_scene = next_scene
But that’s not very different from using
get_tree().change_scene("res://other_scene.tscn"), except initialization of the next scene would have to be written in
If loading is asynchronous, you’d probably use
_process while the next scene is hidden before swapping the scenes.
Zylann | 2019-09-11 17:47
Ahh thanks! I didn’t know it was possible to get a scene like getting a node. For some reason I thought scenes were different from nodes in that respect. I really appreciate the patience and help!
Hey sorry to bother you again but lets say that I want to change the Position of a player’s piece in a previous scene. What would be the syntax to inherit the player object from the last scene? I have it so that when the player steps on a certain square it takes them to the next level next to a square that should return them to the earlier scene. But when they step onto the return square they are already on the the next level square. My thinking is that I will move them before I change scenes. How would you go about doing this?
when they step onto the return square they are already on the the next level square
How can your player be at two places at a time?
Either separate your levels from the player so you can move it from previous level to the next without bothering about how you left the previous level. Or just remember the position?
Zylann | 2019-09-11 20:42