Connecting signal to every instance of a scene in another scene

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


Let’s say I have scene A, B and C, where scene A can emit a signal. When I isntantiate A from the code, I want to connect its signal to every instance of scene B, and the instances of scene B and A are inside scene C, so C is kind of a wrapper scene, and A and B are “siblings”. Both A and B are instantiated only from the code, therefore I can’t drag and drop anything from the GUI (at least I didn’t find a way).
I could potentially track every instance of B in some list in an autoloaded singleton, but it feels like a bad design. Also, when I create a new instance of scene B at any time in the future, I would need to make sure that signal connected to the instance as well, which feels like a hassle. I would just like this to happen “out of the box”, so that whenever a new B is created, the signal is connected automatically.
Is there a proper way to to this?

Thank you

:bust_in_silhouette: Reply From: Gluon

You can add a connect call in the ready function of the scene B. Something like the below

func _ready():
    var thing = get_node("Your node with signal")
    thing.connect("signal name", self, "function to connect")

The problem is that the “thing” in your example, it’s instantiated from the code somewhere in another scene, maybe even many instances. I’m just a beginner with Godot, but I think to use getnode, I need to know the path to the instances of the objects. But the object is instantiated in runtime, in a different scene, it’s not instantiated from editor onto a fixed path.
Looks like I will need to maintain a list of A or B, and then do the call with a for loop…

Lajbert | 2023-01-25 19:46

Okay well there is a find_node() function in godot so you could use that but be aware that it can be quite a lot slower than get_node() because it will recursively search through your node tree until it finds the node. As long as it isnt used too often it should be fine but something to bear in mind. The documentation for find node is here

Node — Godot Engine (stable) documentation in English

Gluon | 2023-01-25 20:05