Global Signals in Godot 4.0

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

Hello, I was searching for a way to emit and listen to signals without the need to get a node Reference and found this video discribing the use of signals registered globally with an aoutoload script:
I tried it and sadly, it doesn’t seem to work with Godot 4.0. Is that correct, or did I do something wrong?

I have a as an active autoload script, with the name of the signal:
signal attackDamageRecieve

In my emitter node, i emit the signal:

Signals.emit_signal('attackDamageRecieve', attackDamage)

In my listener node, I connect to the signal:

Signals.connect('attackDamageRecieve', onAttackDamageRecieved)

this should trigger this function:

func onAttackDamageRecieved(sender, attackDamage):

Edit: I found the answer in the meantime: the listener doesn’t react, because the emitter only passes one argument, “attackDamage”, while the listener expects two. It silently fails though…I would like the listener to atleast register the emitted signal and throw away excess arguments or register the unsuplied arguments as null or something…

:bust_in_silhouette: Reply From: Marco-

If you emit the signal during runtime watch for the Error tab in the Debugger on the bottom.
If you scroll down in there is there maybe an error that says something like

[…] Error calling from signal ‘attackDamageRecieve’ to callable: ‘YourNode::attackDamageRecieve’: Method expected 2 arguments, but called with 1. […]

If yes then you simply forgot to add the reference to the sender when you emit the signal.
Try modifying your emitter node’s code from:
Signals.emit_signal(‘attackDamageRecieve’, attackDamage)

Signals.emit_signal('attackDamageRecieve', attackDamage)


Signals.emit_signal('attackDamageRecieve', self, attackDamage)

where self refers to sender you are asking for in your listener callable.

Hope this helps. And maybe look into Groups as they maybe can provide the functionality you seek as well.

Ahhh, Errors are there, I just never had the tab open till now and they don’t crash the game, so the tab isn’t automatically opened. Thank you, I will watch that tab more closely now :smiley:

nuttshell | 2023-03-16 19:53

That’s a good habit :smiley:
However that is a common problem with signals and groups alltogether. They might help to write cleaner code with less dependencies but also they increase the risk of unnoticed bugs. If you use signals and or groups always check thoroughly your function signatures and best copy and paste function names if you pass them as strings. That reduces the potential of any errors significantly.

Marco- | 2023-03-16 21:09