Emit and receive signals from instantiated scenes

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

Hi guys.

I am trying to emit a custom signal from a instantiated scene to be received/processed by another instantiated scene but cant understand correctly what to do.

My structure is something like this

> World
>       |
>       -> Emitters
>       |    |->InstantiateEmitterA
>       |    |->InstantiatedEmitterB
>       |    |->InstantiatedEmitterC
>       |
>       |-> Receivers
>            |->InstantiatedReceiverA
>            |->InstantiatedReceiverB
>            |->InstantiatedReceiverC

In World.gd i do:

const EMITTER = preload("res://emitter.tscn")
const RECEIVER = preload("res://receiver.tscn")

func _ready():
	for x in range(3):
		var emitter = EMITTER.instance()
		get_node("Emitters").add_child(emitter)

		var receiver = RECEIVER.instance()
		get_node("Receivers").add_child(receiver)

In Emitter.gd i can create and emit the signal:

signal my_signal

func emitTheSignal():
    emit_signal("my_signal")

But, how could i connect a method in Receiver to my_signal in Emissor

:bust_in_silhouette: Reply From: njamster

Here you go:

const EMITTER = preload("res://emitter.tscn")
const RECEIVER = preload("res://receiver.tscn")

func _ready():
    for x in range(3):
        var emitter = EMITTER.instance()
        get_node("Emitters").add_child(emitter)

        var receiver = RECEIVER.instance()
        get_node("Receivers").add_child(receiver)

        emitter.connect("my_signal", receiver, "_on_my_signal")

This is assuming the receiver-script contains a function _on_my_signal.

Hi njamster.

So, this way, receiverA will only respond to emitterA signal correct?

What i want is that if any emitter “emits” a signal, all the receivers respond to it.

I ended up using the groups option where i put all receivers in a group, then:

get_tree().call_group("group_name", "method_name")

eduardovmendes | 2020-04-29 23:37

So, this way, receiverA will only respond to emitterA signal correct?

Correct.

What i want is that if any emitter “emits” a signal, all the receivers respond to it.

In that case, your solution seems best indeed. :slight_smile:

njamster | 2020-04-30 10:58

:bust_in_silhouette: Reply From: 2ndGarrison

There are probably a few ways you could go about it. I’m not sure if there’s a particular order you want to follow when connecting your emitters, but you could do this if you were intending to connect emitterA to receiverA, emitterB to receiverB and so forth:

After your code for instancing the emitters and receivers…

for x in get_node("Emitters").get_children():
    x.connect("SIGNAL", get_node("Receivers").get_child(x.get_index()), "method_name_in_receiver")

Like I say, there are other ways to do this as well. In this case, it only works if the amount of emitters and receivers are the same. Hopefully this can give you some guidance to find what you need.

Hello 2ndGarrison.

What i want is like a Interface in C#.

When any emitter call a signal, all the receivers respond to it.

In this particular case, i used groups.

Added all receiver in a group and in emitters used:

get_tree().call_group("group_name", "method_name")

Anyway, thank you

eduardovmendes | 2020-04-29 23:32