Signal ordering

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

I was wondering what is the ordering of the execution between signals. For instance, if when signal A is emitted will all methods connected to it be called before another signal is handled.

For instance, if signal A is emitted and one of the method connected to it emits a signal B, will all methods connected to signal A be called before any methods connected to signal B ?

very likely so, signals are handled almost immediately. Signal A should be received in the same frame for all listeners.

Inces | 2022-12-29 20:49

:bust_in_silhouette: Reply From: LeslieS

If a signal function emits a signal, that signal is processed before another signal on the same level.
Here is how I tested it (a 2d project with a button where the function test() is called from the button.pressed() signal):

extends Node2D
signal signalA
signal signalB
signal signalC

func _ready():
	connect("signalA", self, "sigA")
	connect("signalB", self, "sigB")
	connect("signalC", self, "sigC")
func test(): 
	emit_signal("signalA")
	emit_signal("signalB")

func _on_Button_pressed():
	test()

func sigA(): 
	print("signalA")
	emit_signal("signalC")

func sigB(): 
	print("signalB")
	
func sigC(): 
	print("signalC")

This prints:

signalA
signalC
signalB

Thank you for your answer,

I did some tests based on your example and these confirm (at least experimentally) that a signal is processed as soon as it is emitted (in your example, the methods connected to signal C will be called before any remaining methods connected to signal A not called).

This makes sense from an implementation point of view since signals are Godot’s version of the observer pattern.

Perococco | 2022-12-30 10:54