Connect programmatically instantied nodes only when they are available

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


I’ve got a node in my scene tree which generate some instances of my scene programmaticaly (the Player for example) .

I also have a camera node in my scene tree and it needs to connect some specific instances to this camera by using get_nodes_in_group method. But when my camera script gets the nodes in group “shake screen” for example, there is nothing, as the instances are not yet instancied. So the nodes are not connected and then i can’t use signals properly.

I don’t know if this is a problem in the way i structure my projet or if i’m missing something in the way to instantiate/load nodes.

:bust_in_silhouette: Reply From: Jowan-Spooner

Hey Llenucs,
I don’t know when you are create the instances. You should keep in mind that the _ready() function is done from bottom to top in your tree, from child to parent. Don’t know how your structure looks, but this could be one problem. On the other hand you could just call the cameras connect_to_nodes()function each time you add a new node. Like this:

# Camera-Script
func  connect_to_nodes():
    for node in get_tree().get_nodes_in_group("Players"):
        connect("shake", node, "do_shake")
# Another script
func instance_scene():
   var new_scene = Myscene.instance()

The problem is that this would reconnect all nodes over and over again. So it’s not that useful and I would recommend to just connect the nodes when they are instanced:

func instance_scene():
    var new_scene = Myscene.instance()
    get_node("SomeNode/MyCamera").connect("shake",new_scene, "do_shake") 

Is this what you were looking for or did I understand you wrong?
Good luck,


You understand well :wink:

And yes i ended up with your solution which is the best I think.

In fact i first tried to avoid calling node explicitely with get_node(“my_node”) in case the structure will change, because i thought it was a best solution to use groups to connect nodes, which allow to redefine node’s hierarchy.

But yes your solution works fine, thank you :slight_smile:

Lienucs | 2019-06-17 13:50