There are multiple ways to make nodes communicate in code.
Signals are only one of several, and you will get to learn and compare the alternatives as you make games with Godot. Every approach has its uses.
Here, we are focusing on signals as this is the last missing piece before creating a complete game from scratch.
The advantage of signals is that they can help keep your code clean and flexible.
All of Godot’s nodes and objects emit signals when a specific event occurs. For example, when a player clicks a button, the button emits the pressed signal.
You can connect that signal to another node, like a character, to react to that button press.
Signals connect to functions, and so when the player clicks a button, Godot will call the target function for you.
The editor also shows you signal connections with icons, which helps you track connections.
It does so in two places.
First, in the scene dock, where it displays an icon next to nodes that connect to another. You can click the icon to jump to the node dock.
Second, inside a script, next to a function that receives a signal, an icon tells you that Godot will call it.
You can click the icon to see a table of the connections.
In summary, signals are useful because they allow you to listen and react to specific events. Also, the editor helps you track the connections.
Breaking down the signal connection syntax
There are details of the signals' syntax in code I didn’t get to cover in the video. Let’s go over them here.
In the video, we got a reference to our Timer node and connected to its timeout signal like so:
# We get a reference to a child node named "Timer" and store it in a new# variable.var timer = get_node("Timer")
# We connect the timer's `timeout` signal to this node's# `_on_Timer_timeout()` function.
timer.connect("timeout", self, "_on_Timer_timeout")
Here, connect() is a method we call on the Timer node. It takes three arguments: the signal to emit, the object to connect to, and a function to call upon emitting the signal.
Note how we pass the name of the signal and the function’s name to call inside quotes.
We use quotes to represent strings of text. In Godot 3, you often use text to refer to a signal, a function, or a node’s name as in the call to get_node().
Understanding the self keyword
When connecting to the timeout signal, we used self as the second argument.
The self keyword in GDScript designates the object the script is attached to. In this case, it is our flying Godot head.
But not in GDScript, thanks to the engine. Godot always treats your GDScript code as implicitly running on the object or node you attached it to.
When we get a node, like so:
This is the same as writing:
It reads as: “I, the Godot node, want to get a reference to a child node named Timer”.
There are only a few cases where you need to use self in GDScript. Connecting a signal via code is one case. Another has to do with a language feature that’s beyond the scope of this series, setter and getter functions. We won’t be using them here.
With that, you went through all the fundamentals to start creating complete games in Godot. In the next part, you will get to create a complete 2D game from scratch, step-by-step.