Topic was automatically imported from the old Question2Answer platform.
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.
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
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: