Signals in Godot 4
Signals are a powerful feature of the Godot game engine that allow different objects to communicate with each other without being tightly coupled. Signals in Godot 4 are essentially a way for an object to send a message to other objects that are listening for it. This allows you to create a flexible and modular game architecture, where objects can interact with each other without knowing about each other’s implementation details.
In Godot 4, signals work similarly to previous versions of the engine. You can find my post about signals in Godot 3 here. They are defined in the script of an object using the
signal keyword, followed by the name of the signal. Let’s take a closer look.
Set up a basic scene
- In the editor on the top right, go to Scene > New Scene.
- Make a new “User Interface” scene.
- Add a Button.
- Click on the Control node, call it “SignalTut” or anything you like, and click “Add new script”.
- It should then look something like this:
Connect signals via editor
- Click on the Button and go to the right-hand side.
- There you will find under Node > Signals all built-in signals.
- Click on “pressed()” and then on “Connect”, or just double click “pressed()”.
- Pick “SignalsTut” and click “Connect”.
- A new function will be created automatically:
extends Control func _on_button_pressed(): pass # Replace with function body.
Connect signals via code
There are more ways of connecting a signal via code. Here you will find two different ways, though one of them is the recommended way in Godot 4. With the
emit_signal keyword, you can send a signal. Add the following code to your script:
extends Control signal custom_signal # not recommended anymore signal custom_signal_recommended # this is the recommended way signal custom_signal_with_argument(arg) # send arguments via signal func _ready() -> void: connect("custom_signal", _on_custom_signal) custom_signal_recommended.connect(_on_custom_signal_recommended) custom_signal_with_argument.connect(_on_custom_signal_with_argument) func _on_button_pressed() -> void: print("Button pressed") emit_signal("custom_signal") # you emit signals via code like this func _on_custom_signal() -> void: print("Custom Signal sent") emit_signal("custom_signal_recommended") func _on_custom_signal_recommended() -> void: print("Custom Signal Recommended sent") emit_signal("custom_signal_with_argument", 1) func _on_custom_signal_with_argument(arg: int) -> void: print("Custom Signal with argument '%d' sent" % arg)
Now you can test the scene.
- Save the scene.
- Run the scene (click “select this scene”, if you get asked).
- Click the button. You should see the following output in the console:
Button pressed Custom Signal sent Custom Signal Recommended sent Custom Signal with argument '1' sent
Disconnect signals in Godot 4
Of course, you can also disconnect signals. In code, you can use
disconnect and in the editor you again go to Node > Signals and just click on the signal and then the “Connect” button changes to “Disconnect”.
That are the basics about signals in Godot 4
Overall, signals are a powerful tool for creating flexible and modular game architectures in Godot 4. By using signals, you can create objects that are decoupled from each other, making your game easier to maintain and update over time.
If you want another tutorial, check this one out. Here you will find the official documentation.
Download the source files
If already subscribed, you can find all project files here. Otherwise, you can subscribe to the mailing list to get access to this and other project files for free and get notified, when a new tutorial is posted.