Hi - this is my first post (experienced developer but new to Godot)!

I have a nearly empty root scene where I switch between other scenes using:

var currentScene = null
func set_scene(scene):
	currentScene.queue_free()
	var s = ResourceLoader.load(scene)
	currentScene = s.instance()
	get_tree().get_root().call_deferred("add_child",currentScene)

However, some of my scenes have signals declared for the root node script. How do I programmatically connect these signals after switching scenes? I have tried a few different variations with connect, but I cannot figure it out - nothing works.

Thank you!

I have some questions to make, before I could actually answear your question

Why don't you use: var currentScene = get_tree().get_current_scene()

Also the SceneTree class has the function you're coding already:

SceneTree.set_current_scene (Node child_node)

You can pass a PackedScene as argument as well, since a PackedScene is a saved Node.

and the you can simplify your code:

onready var scene = load (Resource.get_path()) #you can set a exported variable to use as argument for that
#if you want to, take a look in the video I make yesterday, like in the end of the comment

var tree = get_tree()
tree.set_current_scene(scene)
var currentScene  = tree.get_current_scene()

Also I wouldn't recommend by any means that you add a child directly to the root, but you can add it to the first root child, the trunk node (the first node of the scene) of the current scene, that's because having 2 trunk nodes can create everykind of strange behaviours.

So you can make it this way: tree.get_root().get_child(0).add_child(currentScene)

And to connect the signal you can connect it as simply as possible:

var nodeThatWillBeConnected = get_node(NodePath)

nodeThatWillBeConnected.connect(..........................the arguments for connect the signal................)

Now to solve the overall process and to actually answear your question:

You can use a Singleton to hold a function that will receive the signals from the nodes you're instancing.

The Singleton won't change, either be reproccessed, until the game actually is closed.

So you can have a structure like that:

SingletonNode root
|currentScene
|
otherNodes

You can watch the video here, with EN-US subtitles

6 years later