Godot Signals and How to Use Them

Godot Signals and How to Use Them

Godot Signals allow a node to send a notification to listening nodes, whenever something happens. This is a great way to decouple your objects, which leads to better manageable code and allows a more modular structure. So instead of forcing an object to expect another object to always be present, they just notify interested objects that something happened and said interested objects then can respond.

Create Godot Signals

In order to use signals, you have to first create a signal. There are two ways to do that: Either via the editor or by code.

Built-in signals

Different nodes already have built-in signals. Take the timer, for example: if you go to the Signals Tab you will find the timeout() signal.

Timer signals
Timer signals

Most of the built-in signals are self-explanatory, but if you are unsure, you can always check the official documentation for more information about any specific node.

Custom signals

You can create your own custom signals for cases the built-in signals do not cover. Just use the signal keyword to create a custom signal:

signal my_signal
signal my_signal_with_arguments(x, y)

You will notice, that these signals will appear in the Signals Tab of the node as well.

Custom signals
Custom signals

In order to emit a signal, you have to use the emit_signal keyword. For example:

func _ready():
	emit_signal("my_signal")
	emit_signal("my_signal_with_arguments", 1, 2)

Connect Godot Signals

If you want nodes to respond to emitted signals, you have to connect them. There are two ways to connect signals. Either via the editor or in code:

Connect signals via the editor

Double click the signal you want to connect. The following window will open:

Connect signals via the editor
Connect signals via the editor

You have a couple of options here: If you want, you can create a new function in the chosen script. Deferred connections queue their signal and the methods are not called until idle time. If you check Oneshot, the signal will only be called once and then disconnected.

Connect signals in code

You can also connect signals via code. This is especially useful if you create multiple instances of an object.

func _ready():
	connect("my_signal", self, "_on_Timer_my_signal")
	connect("my_signal_with_arguments", self, "_on_Timer_my_signal_with_arguments")

Whereby the first argument is the name of the signal, the second is the target and the third is the method, that will get called. If you want to make the connection Oneshot or Deferred, you can do this like so:

connect("my_signal", self, "_on_Timer_my_signal", [], CONNECT_DEFERRED | CONNECT_ONESHOT)

When instancing objects, you can connect them like this for example:

object.connect("my_signal", self, "_on_Timer_my_signal")

Consider the following little code snippet, where we create an instance of the so-called myCustomObject.tscn scene.

func _ready():
	var mco = load("res://myCustomObject.tscn").instance()
	mco.connect("my_signal", self, "_on_my_signal")
	add_child(mco)	
	get_child(0).emit()
	
func _on_my_signal():
	print("signal")

And the code in the myCustomObject.tscn looks like the following:

signal my_signal

func emit():
	emit_signal("my_signal")

Disconnect Godot signals

You also may want to disconnect a connected signal. You can do this either by using the editor (if created via the editor) or with a line of code (when created in code).

Disconnect signals via the editor

You can disconnect connected signals by using the editor. Just go to the Signals Tab, select the signal and click on Disconnect.

Select Signal and click disconnect
Select Signal and click disconnect

Disconnect signals in code

You can easily disconnect signals with the following line of code:

disconnect("my_signal", self, "_on_my_signal")

More information


To learn more about the Godot Engine I can recommend Godot Engine Game Development Projects by Chris Bradfield. If you are interested, you can follow the affiliate link to also support this website.


Godot Signals
Godot Signals

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment