Signals in Godot

Errol Hassall
Partly Functional
Published in
4 min readJul 21, 2019

I’m a very functional guy, by that I mean functional programming. For the most part I’ve worked in functional programming languages, as a result I’ve forgotten most of my OOP fundamentals, they’re still there, they’re just not in the forefront of my mind. Today I would like to talk about signals, they’re a huge part of Godot and as a result they’re extremely useful in getting anything done properly.

According to the documentation signals are Godot’s implementation of the observer pattern. What’s the observer pattern? I don’t know, so lets find out. According to Wikipedia, the observer pattern is a software design pattern in which an object, known as the subject has a list of dependents, which are called observers. Observers will automatically be notified of any state changes of the subject usually by calling one of their methods.

It boils down to node X can listen to node Y and get the updates of that node. For example, if you had a button, instead of checking constantly if it’s been pressed the button will emit a “signal” when it’s pressed.

The primary use case for signals is to decouple your game objects. This is an easier way to organise your code, creates an easier management structure and most of all it makes testing your code a lot easier. This is achieved because instead of other objects expecting another object to be present they can instead emit a signal to other subscribers to respond too.

lets take a look at some examples. I have taken the tutorial from the Godot documentation about signals which can be found here: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/signals.html

Create a scene with one Timer and one Sprite like above. Make sure you create a script on the Sprite this will come in handy later.

Click on the Timer and then navigate to the Node section on the right side. Here you will see all the signals for the node, as you can see at the top is the timeout function. Double click this and it will allow you to connect this signal to another node.

The red Timer indicates that this is the node the signal is coming from

Click on the Sprite node and hit connect.

Godot will automatically create a function called “_on_Timer_timeout()” add the code you see there, this will toggle the visibility of the Sprite node. The $”.” indicates that you’re talking to this node.

Make sure you see this inside your Timer node otherwise the connection didn’t work.

Now you see me
Now you don’t

Simple, the Sprite is now toggling on and off every one second which is the default for the timer. When the timer hits zero the visibility will toggle.

Godot provides a tonne of built-in signals, but you can also define your own when you need to. When you need to tell another object something look at using a signal because it will make your life a whole lot easier. Signals give you the power to tell another object that something has changed without both objects being directly connected. Signals are pretty easy once you see them in action.

Thanks for reading, I hoped this helped you understand Signals in Godot.

--

--