+1 vote

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
   add_child(instanced_scene)

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

func _ready():
   # load Mainmenu.tscn
   loadNewScene("Mainmenu")

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"
    freeScene(current_scene)
    loadNewScene(target_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 ;) ]

in Engine by (13 points)

1 Answer

0 votes

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.

by (14 points)
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.