Is it possible to emit signals to a different tree?

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

I am currently making a simple tree which looks like this :

MainNode (Parent) → Character (Child)

MainNode can add child or free one from any files such as maps (i.e. terrain.tscn) or events (i.e. event.tscn). All events and maps will be stored in a dictionary for easy usage. The codes would look like this :

func loadNewScene(key):
   # load the corresponding tscn file from dictionary

func freeScene(key):
   # find the corresponding target node using key

func _ready():
   # load Mainmenu.tscn

My problem begins when I press start in the Mainmenu.tscn. If I had Mainmenu.tscn as a child (in default), I can always emit a signal for confirming the player press the button to fire a signal to proceed to the next scene, such as :

func _on_Button_pressed():
    var target_scene = "Opening Scene"

But since the node isn’t actually in the MainNode (It will be added as a child after _ready() ), I can’t find a way to emit a signal from “Mainmenu.tscn” to MainNode, because initially it’s not in the MainNode’s tree.

Is there a way to make a signal to a different tree? Or is there a alternative solve for this?

[note : This question is also on stackoverflow. I’m posting on both platforms :wink: ]

:bust_in_silhouette: Reply From: RisingThumb

From what I understand, you want to connect a signal after you’ve added a child node to its parent.

This can be done with node.connect("signalName", nodeToBeConnectedTo, "functionName") in your code. This is useful when you instance a node, so you can’t connect it in the editor.

You also ask about making a signal to a different tree. You can use the get_parent(), get_node() and other similar functions for finding nodes, or if you want the root node, you can use get_tree().get_root(), or if you want an array of child nodes, get_children(). There’s a lot of different ways of getting nodes, so I will recommend using the documentation to find and read more if you need a more particular way of getting a node in your tree.

The documentation lists further information about signals and connecting them. You can find that here.