Editting scene B while in Scene A

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

Is there any way to play scene A while calling function like set_cellv in a different scene? I’m hoping I can generate the map for scene B while Scene A is running.

Thanks,
Xofox

:bust_in_silhouette: Reply From: Zylann

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.

Thanks again,
Xofox

xofox | 2019-09-11 13:54

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:

func load_next_scene():
	
	# 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.
	next_scene.load_things()

	# Schedule removal of the current scene
	get_tree().get_root().current_scene.queue_free()

	# Add next scene
	get_tree().get_root().add_child(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 _ready().

If loading is asynchronous, you’d probably use yield or _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!

xofox | 2019-09-11 19:11

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?

xofox | 2019-09-11 20:38

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