How to use Signals correctly?

Godot Version

4.2

Question

Hi,

I just started to use Godot and I’m a bit confused about the usage of signals. As far as I understand there are two ways to use Signals, but one of them might be unintended?

  1. Defining a Signal, than passing it down to a Button for example and awaiting the Signal.

     signal baz
    
     func _init():
         var foo = Foo.new(baz)
         game_loop()
    
     func game_loop():
         while(true):
             await baz
    
     class Foo:
         extends Button
     
         var bar: Signal
    
         func _init(p_bar: Signal):
             bar = p_bar
    
         func _pressed():
             bar.emit()
    
  2. Connecting a signal defined in the button for example to a method in another class, like explained in the docs: Using signals — Godot Engine (stable) documentation in English

I’ve been using the first example to generate multiple buttons in a menu that use the same signal to notify an Object, but I get the feeling that it is the wrong approach. Also… I didn’t test that code example. It might not be 100% correct.

I think you logic looks correct, but maybe since you pass a reference the signal is somehow assimilated into the nested class.

In other words it is now Foo.bar emitting and not self.baz.

Don’t get me wrong. The code I’m using works. I’m just asking, because I don’t know if this approach is correct.

Oh, k sorry.

On that note I would say eh… it would be more readable to show ownership of the signal. It could become a nightmare to trace.

Signals cam use the same callable function

Foo.connect.pressed(self._on_child_pressed)
Foo2.connect.pressed(self._on_child_pressed)

Connecting a signal this way would also make the children signals “private” with the parent. If done the proposed way above, adjacent Nodes to the parent could also listen for that signal of the buttons as a side effect of the child emitting on behalf of the parent. (Maybe parent child is incorrect, it could be delegate and owner of signal)

But if you had such a need to do this you could also just have a cascade of emitting signals. One from the button, then one from the parent that a button was pressed.

I don’t know if that while loop is just for demonstration purposes, but seems like an anti-pattern for how signals want to work. Just connect a callable function to the signal.

1 Like

Ohhh… I don’t know why I was so stuck on using the while loop. There is no need to await the Signal if there is no loop. Just connect the methods. Thanks. :smiley: Im new to Godot and Game developement in general. Sorry if the question was stupid. Just one more question.

Is there ever a need to use await Signal?

Oh no, I don’t think it’s stupid, it is an interesting use!

But I’m trying to imagine a situation where this approach would make sense. There could be. I just can’t think of one.

Maybe in the future Godot where struct data type exists you could bootstrap a signal to it this way.