Signals in Godot 3
Signals in Godot 3 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 interesting objects that something happened, and said interesting objects then can respond.
Dear reader, please consider supporting my game ❤
Create Signals in Godot 3
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.
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.
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 Signals in Godot 3
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:
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 signals in Godot 3
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.
Disconnect signals in code
You can easily disconnect signals with the following line of code:
disconnect("my_signal", self, "_on_my_signal")
More information
If you like the post, let me know. Also, you may be interested in my post on inheritance in Godot. Or maybe about localization in Godot. For more information, visit the official documentation about signals. Also, feel free to check out other topics about Godot Engine on the website.