TExcelTAD
Home › Godot 4 with GDScript › Signals

Signals

A signal is an event a node announces: a Button emits pressed, a Timer emits timeout, an Area2D emits body_entered. Other objects connect a function to the signal, and every connected function is called when it is emitted. The emitter doesn't know or care who is listening. That one property is why signals are the backbone of Godot architecture: a health component can announce died without knowing whether the HUD, the sound system or the game-over screen is listening.

In Godot 4, signals are first-class values. You write button.pressed.connect(_on_pressed): button.pressed is a Signal object and _on_pressed (without parentheses) is a Callable. The Godot 3 form connect("pressed", self, "_on_pressed") with strings is gone.

Declare, emit, connect

gdscript · try it yourself
extends Node

# frames: 0
class Health extends Node:
	signal changed(old_value: int, new_value: int)
	signal died

	var hp := 3

	func damage(amount: int) -> void:
		if hp == 0:
			return
		var old := hp
		hp = maxi(hp - amount, 0)
		changed.emit(old, hp)
		if hp == 0:
			died.emit()

var health: Health

func _ready():
	health = Health.new()
	health.name = "Health"
	add_child(health)
	health.changed.connect(_on_health_changed)
	health.died.connect(_on_died)
	health.died.connect(func(): print("lambda: play death sound"), CONNECT_ONE_SHOT)
	print("connected to died: ", health.died.get_connections().size())
	health.damage(1)
	health.damage(5)
	health.damage(1)
	print("connected to died after: ", health.died.get_connections().size())

func _on_health_changed(old_value: int, new_value: int) -> void:
	print("HUD: hp %d -> %d" % [old_value, new_value])

func _on_died() -> void:
	print("Main: player died, showing game over")
connected to died: 2
HUD: hp 3 -> 2
HUD: hp 2 -> 0
Main: player died, showing game over
lambda: play death sound
connected to died after: 1
  • signal changed(old_value: int, new_value: int) declares a signal with typed parameters. The types document the signal and are shown in the editor's Node dock; the editor uses them when it generates a handler for you.
  • changed.emit(old, hp) calls every connected function immediately and in order of connection, then continues. Emitting is a synchronous function call, not a message queue.
  • A handler's parameters must match what is emitted. If the counts differ (in either direction), that one call fails with Error calling from signal 'changed' to callable: ... Method expected 1 argument(s), but called with 2. and the other handlers still run. Use unbind(n) to drop arguments you don't need: changed.connect(_refresh.unbind(2)).
  • CONNECT_ONE_SHOT disconnects the function after its first call, which is why only one connection is left at the end.
  • Handlers are conventionally named _on_<emitter>_<signal>, like _on_start_button_pressed.

bind, lambdas and disconnect

Often the same handler serves several emitters and needs to know which one fired. Callable.bind() appends extra arguments:

gdscript · try it yourself
extends Control

# frames: 0
var clicks := 0

func _ready():
	var buttons := VBoxContainer.new()
	add_child(buttons)
	for level in [1, 2, 3]:
		var b := Button.new()
		b.text = "Level %d" % level
		buttons.add_child(b)
		b.pressed.connect(_on_level_pressed.bind(level))
	var quit_button := Button.new()
	quit_button.text = "Quit"
	buttons.add_child(quit_button)
	quit_button.pressed.connect(_on_quit_pressed)
	# Simulate clicks: a Button emits `pressed` when it is clicked.
	buttons.get_child(1).pressed.emit()
	buttons.get_child(1).pressed.emit()
	quit_button.pressed.emit()
	print("connected? ", quit_button.pressed.is_connected(_on_quit_pressed))
	quit_button.pressed.disconnect(_on_quit_pressed)
	quit_button.pressed.emit()
	print("connected? ", quit_button.pressed.is_connected(_on_quit_pressed))
	var on_click := func(): clicks += 1
	quit_button.pressed.connect(on_click)
	quit_button.pressed.emit()
	print("clicks = ", clicks)

func _on_level_pressed(level: int) -> void:
	print("load level ", level)

func _on_quit_pressed() -> void:
	print("quit pressed")
load level 2
load level 2
quit pressed
connected? true
connected? false
clicks = 1

Things to know:

  • Connecting the same callable twice is an error ("already connected"). Check is_connected() first, or pass CONNECT_REFERENCE_COUNTED if you really want counted connections.
  • To disconnect a lambda you need the same Callable value, so store it in a variable (on_click above) instead of writing the lambda inline.
  • Connections to a node are removed automatically when that node is freed. You only need disconnect() when both objects stay alive and the listener should stop listening.
  • CONNECT_DEFERRED delays the call to the end of the current frame (idle time). Use it when the handler changes the tree in response to a physics callback, for example adding nodes from body_entered.

Connecting in the editor

For nodes that already exist in a scene, connecting in the editor is common:

  1. Select the emitting node (say StartButton) in the Scene dock.
  2. Open the Node dock (next to the Inspector) and the Signals tab.
  3. Double-click pressed(). In the dialog, pick the receiving node (the one with the script) and keep or rename the method (_on_start_button_pressed).
  4. Click Connect. If the method doesn't exist, Godot adds a stub to the script; a green connection icon appears next to the function.

The connection is saved in the scene file, not in the script:

ini
[connection signal="pressed" from="UI/StartButton" to="." method="_on_start_button_pressed"]

Editor connections are quick, but they are invisible when you read the script. Many teams connect in code (in _ready()) for anything that isn't a simple UI hookup, so that a search for the handler name finds where it's wired.

Signals up, calls down

The rule that keeps a scene tree maintainable:

  • A parent may call methods on its children (it owns them and knows they exist): $Health.damage(1).
  • A child emits signals and lets whoever owns it decide what to do: died.emit(). It never reaches up with get_parent().get_parent().show_game_over().
  • Siblings don't talk directly: the common parent connects one sibling's signal to the other's method.
gdscript
# level.gd: the parent wires its children together
func _ready() -> void:
	$Player.health.died.connect($HUD.show_game_over)
	$Player.coin_collected.connect($HUD.add_coin)

The player now works in any level, in a test scene and in a unit test, because nothing in it depends on where it sits. For events that many unrelated scenes care about (an achievement unlocked, a setting changed), an event bus autoload with only signal declarations is a common pattern; see Project Settings & Autoloads and Field Notes: Scene Architecture.

Signals also work with await, which pauses a function until the signal fires. That is the next lesson: await & Coroutines.

Test yourself

Which line connects a button's pressed signal to _on_pressed in Godot 4?

  1. button.connect("pressed", self, "_on_pressed")
  2. button.pressed.connect(_on_pressed)
  3. button.pressed += _on_pressed
  4. connect(button.pressed(), _on_pressed())

Three level buttons share _on_level_pressed(level: int). How does each one pass its own number?

  1. Use a global variable
  2. b.pressed.connect(_on_level_pressed.bind(level))
  3. Read get_parent().name
  4. Emit pressed(level) from the button

Following 'signals up, calls down', how should a Player tell the game that it died?

  1. get_parent().get_parent().game_over()
  2. Emit a died signal and let the level (its owner) connect it to the game-over logic
  3. Call get_tree().quit()
  4. Set a global variable every frame