signals-cheatsheet

Signals: connection and good practices

This guide recaps everything we saw about signals so far:

A signal is a message a node emits when something occurs to it. For example, when you click on a Button, it emits the pressed signal.

By connecting a signal to a node, you can let Godot call a function of that node every time the signal emits. We call that function a callback because it gets “called back” when the signal emits.

Connecting signals

There are two ways to connect signals:

  1. Using the Node dock in the Godot editor.
  2. By calling the connect() function in code.

When to connect with code or the editor

Connect signals using the editor if:

Connect signals using code when:

Let’s see how to connect a button’s pressed signal using either approach. We’ll use our Scoreboard scene’s HideButton node again as an example.

How to connect using the editor

To connect a signal using the editor, select the node with the signal you want to connect in the Scene dock.

On the right of the interface, head to the Node dock, next to the Inspector. The dock lists the selected node’s signals.

Double-click a signal you want to connect to open the signal connection window.

The node you connect to must have a script attached to it for Godot to create what it calls the Receiver Method (the callback function).

Nodes you can connect to will have their name in white while the others are grayed out.

Double-click on a node or click Connect to connect the signal.

Godot will automatically create the callback function in your script if it does not exist yet. It will also take you to the script editor and scroll to the connected function.

When connecting with the editor, two icons appear.

First, in the Scene dock, you have the emission icon next to the emitting node.

Clicking on it reopens the Node dock, which shows the connected signals.

Then, in the script editor, an icon appears in the margin to the left of the callback function.

Click it to display information on which node connects which signal to this function.

It’s especially useful when you start to connect multiple signals to the same callback function as they all get listed in this popup.

How to connect via code

To connect to a signal via code, call the connect() function on the node that has the signal.

The connect() function has three required arguments:

  1. The signal’s name as a String.
  2. A reference to the node holding the callback function. Use self to connect to the node to which you attached the script.
  3. The name of the callback function as a String.

The function has more optional arguments we’ll explore later in the course.

To connect the button’s signal with code, we first need to get the button node. We do this with an onready variable and connect the signal in the _ready() function.

# We get a reference to the HideButton node.
onready var hide_button := $MarginContainer/VBoxContainer/HideButton

func _ready() -> void:
    # The pressed signal exists on the Button node, so we need to call the
    # connect() function on the button.
    hide_button.connect("pressed", self, "_on_HideButton_pressed")

The call to the connect() function reads as “connect the HideButton’s pressed signal to this node’s _on_HideButton_pressed() function.”

# self means "the node with this script attached."
hide_button.connect("pressed", self, "_on_HideButton_pressed")

All that’s left is writing the _on_HideButton_pressed() function and giving it the instructions you need to run upon pressing the button.

# Godot will call this function whenever the player presses the button.
func _on_HideButton_pressed() -> void:
    hide()

Callback functions

Godot users follow a convention to name the signal callback functions: _on_NodeName_signal_name().

For example, if you connect the pressed signal of a node named HideButton, you would call the function _on_HideButton_pressed().

It’s only a guideline. Later, you will see that we often prefer to connect signals directly to existing functions rather than creating a new callback function.

If you connect the node using the editor, Godot will take care of naming the node based on this convention.

And that’s it for this first signals recap guide. Moving forward, you’ll learn how to create new signals, emit them from code, and new techniques to manage multiple signal connections.