Awaiting a user signal

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

I’m using godot beta 17 and in GDScript 2.0 I can’t figure out how to await a user signal added through add_user_signal. There doesn’t seem to be any api for this. Maybe someone can point me towards something I’m missing.

Any help would be appreciated.

EDIT:
Ideally I’m looking to recreate this syntax

extends Node

func _init():
	add_user_signal("some_dynamic_signal")

func await_dynamic():
	while(true):
		await #some_dynamic_signal
		print("my dynamic signal was emitted")

doing this with a declared signal like so works:

extends Node

signal some_declared_signal

func await_declared():
	while(true):
		await some_declared_signal
		print("my declared signal was emitted")

But I created a few dynamic signals and just want to cleaner style of using awaits rather than random events being called

I was able to locate the documentation!

Cam | 2023-02-05 03:31

:bust_in_silhouette: Reply From: Cam

The function add_user_signal does the same thing as defining a signal using the signal keyword, however this function can be used to modify signals at runtime rather than being hard coded.

The documentation for signal features can be found here and specific functions are in the documentation for Object.

In order to “await” a user signal, the signal has to be declared, and then connected to a method.

For example:

extends Node
signal my_signal

func _ready():
    connect("my_signal", self, "my_method")

func my_method():
    pass

The connect function should be called on the object that has the signal, in this case self, through having no dot before the function call. It has three arguments, first is a string of what signal, second is the object that has the function we want (self again), and the third is a string with the name of the function.

In order to “await” a user signal, the signal has to be declared, and then connected to a method.

Doesn’t declaring the signal just defeat the entire purpose of using a user signal? Ideally I don’t want to use connect and use the await syntax instead

phteven | 2023-02-04 08:24

Yes it does, I was not aware they added that keyword in Godot 4. Tough question to answer, the docs are not done, and it’s entirely possible this is a bug.

Cam | 2023-02-04 09:01

:bust_in_silhouette: Reply From: vonagam
await Signal(self, 'some_dynamic_signal')

thank you so much bro, i cant find this syntax in anywhere… this is what i need, thanks again

musyarama | 2023-03-12 09:55