Signals - just a question

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

hello

i’ve been using godot for a while now, i love it! best engine ever!
but i’ve never really got my head around signals, i kinda know how to use them
but i don’t get the point

a signal is a soft coupling so it doesn’t break the game when the connecting object gets destroyed, but… in the receiver code you have to reference the emitter node by

emitter.connect(“custom_signal”, self, “adjust_and_print”)

but that’s a coupling cause you need the find and reference the emitter with

var emitter = get_parent().get_node(“signal_emit”)
if this line fails then the game breaks anyway!

unless the receiver is a child, or a parent of the emitter, in which case i could do this
instead

$receiver.adjust_and_print() or …/ect…

and if not a child or parent then i might as well do this

var receiver= get_parent().get_node(“receiver”)
receiver.adjust_and_print()

why can’t i just do this?
connect(“custom_signal”, self, “adjust_and_print”)

if one node is emitting then surely receivers listening can react?
or have i got it all wrong?

any help or enlightenment would be great!
thanks

oh and ps. i do know that you can connect via the editor it does solve a shed load of the above but i would like the option of doing in code. or i could use the get node, or find by group.

:bust_in_silhouette: Reply From: jgodfrey

Generally, a signal connect() should be made by something that already has direct knowledge of the nodes in question (such as a common parent). In that case, signals are very easy and elegant to set up.

As soon as you find yourself doing some kind of contorted scene tree gymnastics to find a node for the connection, that should be a red flag.

That said, unfortunately, not everything fits in a neat little box. When you have a need to connect seemingly unrelated nodes via signals, an Event Bus is a common, clean paradigm that solves the issue quite nicely.

Here are a few references to some of the above, but there are lots more.

thanks for the answer bud, thanks for confirming i wasn’t missing something

still not sure why this isn’t a thing?

connect(“customsignal”, self, “adjustand_print”)

it would make like so much easier

i’ll carry on with finding by group, seems to be the lesser evil.

cheers

sparrow666 | 2023-03-09 08:55

connect(“customsignal”, self, “adjustand_print”)

That’s very nearly what an Event Bus connected signal might look like (as linked above).

It’s just be something like:

Events.connect("custom_signal", self, "_on_custom_signal")

jgodfrey | 2023-03-09 14:06

yes, i agree, thanks for the info

sparrow666 | 2023-03-09 18:42