Understanding signal behaviors in GDscript

Godot Version

v4.1.3.stable.official [f06b6836a]


I need help understanding the behaviors when working with signal in gdscript

This is a custom signal in the API called test_signal in the parent node, in class name SignalHolder. To emit this signal on another script I tried getting the node reference @onready var signal_ref = '..' , and calling emit_signal(signal_ref.test_signal) though this outputs an error; Invalid type in function ‘emit_signal’ in base ‘NodeName (ScriptName.gd)’. Cannot convert argument 1 from Signal to StringName.

But when using signal_ref.test_signal.emit() it appears to work fine. What could be the cause?

Also when using class reference and not the node to emit the signal. this outputs an error; Error at (these points): Invalid argument for “emit_signal()” function: argument 1 should be “StringName” but is “Signal”.

Another error also happens when using the class ref to connect to its signal signal_ref.test_signal.connect(_on_test_signal_emitted) ; Invalid get index ‘test_signal’ (on base: ‘Nil’).

Hope someone clears these complexities for me get a better understanding, Thanks

emit_signal() works with strings. You are supposed to give it a string name of a signal, not a signal itself.

emit() works on a signal and doesn’t need parameters (generally).

Invalid get index ‘test_signal’ (on base: ‘Nil’) means that signal_ref is null at the moment when you try to connect a signal.

1 Like

Thank a lot, I see now, I had to give it the string name of the signal, but it is on another script and it can’t emit the non-existent signal, can you introduce me to some syntax?

Right now I only know to use the node ref to connect or emit the signal. I thought signals were part of the godot API, why do I still need the node reference?

I’m likely misunderstanding so I’d like to know

You need a reference because a lot of different objects can emit a signal with the same name.

Imagine you have 10 enemies with “died” signal, but you are only interested in connecting to this signal on blue enemies, not red enemies. You need references to enemies to connect to their signals.

Edit: I would suggest reading this page to get better understanding of signals:

Fair enough, thanks for the support