Godot's signal (signal)

Godot engine signal (signal)

This article briefly introduces the use of signal (signal) in the Godot engine . The official Godot engine recommends that you use more signals in your game development to complete the communication between modules or functions. So you need to understand exactly how it works.

Definition/Addition of Signals

extends Node2D

#One of the additions of the signal
signal my_first_signal;


func _ready():
	#Addition of signal two  
	add_user_signal("my_secend_singal")
	pass


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Connection/binding of signals

extends Node2D

#One of the additions of the signal
signal my_first_signal;


func _ready():
	#Addition of signal 2 Note: This method can bind 1~n parameters to the signal when the signal is defined.
	add_user_signal("my_secend_signal",[1,2,3])
	#Binding of signals Note: Binding can only happen after the signal is added successfully!
	connect("my_first_signal",self,"_first_cb")
	connect("my_secend_signal",self,"_secend_cb",[4,5],CONNECT_ONESHOT)#flags:CONNECT_DEFERRED、CONNECT_ONESHOT、CONNECT_PERSIST、CONNECT_REFERENCE_COUNTED
	pass


#The signal callback has no parameters
func _first_cb():
	print("first signal call me!")

#Signal definition\adding binds three parameters, signal connection\binding binds two more parameters.
#The number of parameters must correspond to trigger the callback correctly.
func _secend_cb(one,two,three,four,five):
	print("secend signal call me! args: %d %d %d %d %d。"%[one,two,three,four,five])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

signal trigger

extends Node2D

#One of the additions of the signal
signal my_first_signal;


func _ready():
	#Addition of signal 2 Note: This method can bind 1~n parameters to the signal when the signal is defined.
	add_user_signal("my_secend_signal")
	#Binding of signals Note: Binding can only happen after the signal is added successfully!
	connect("my_first_signal",self,"_first_cb")
	connect("my_secend_signal",self,"_secend_cb",[1,2],CONNECT_ONESHOT)#flags:CONNECT_DEFERRED、CONNECT_ONESHOT、CONNECT_PERSIST、CONNECT_REFERENCE_COUNTED
	# signal trigger
	emit_signal("my_first_signal")
	emit_signal("my_secend_signal",3,4)
	pass


#The signal callback has no parameters
func _first_cb():
	print("first signal call me!")

#Signal definition\adding binds three parameters, signal connection\binding binds two more parameters.
#The number of parameters must correspond to trigger the callback correctly.
func _secend_cb(arg1,arg2,arg3,arg4):
	print("secend signal call me! arg1:",arg1," arg2:",arg2," arg3:",arg3," arg4:",arg4)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

pre-trigger check

Before the signal is triggered, make sure the signal has been added to the signal pool correctly! The check method has_user_signal("my_first_signal") can be used .
If has_user_signal("my_first_signal") returns True , then use the method is_connected("my_first_signal", self, "_first_cb") to check that the signal is connected correctly.

Related: Godot's signal (signal)