Eloy Perez

Just random things

GDscript 2 Signals

Posted at — Mar 9, 2023

In Godot 3.X signal connections were configured in code like:

var areaNode: Area2D = $YourAwesomeArea2DNode
areaNode.connect("area_entered", self, "_on_area_entered")

This means that both the signal name and the method to run were specified dynamically.

Configuring signals this way had some issues. For example, if the method name changed, and you didn’t remember updating its usages, your game would stop working. The same could happen if the signal name changed, this happened if the signal was created by you.

Godot’s team has been working so functions are first-class elements of the language, allowing new and better ways of configuring signals, among other things.

Since version 2 of GDScript, available since the release of Godot 4, we can now connect signals in a safer way.

areaNode.area_entered.connect(_on_area_entered)

Now we can access directly the signal to configure it and as parameter of the connect method we specify the function to run.

If either the signal or the method stop being available (because they’ve been renamed, for example) the compiler will emit an error immediately, without needing to run the game and manually test that it works.