Skip to content

Lesson 4 of 9 · 26m

Signals Are the API Between Objects

Use signals to announce events upward or outward, connect them deliberately, and avoid the tangled observer chains that give event systems a bad name.

Signals solve a problem every game has: the object that knows an event happened is rarely the object that should decide what to do about it. A health component knows health changed. The HUD, audio system, and death logic may care. If health hard-codes all three, it becomes a dependency magnet. Signals let it announce facts instead.

Emit facts, not commands

A well-designed signal says what happened:

class_name Health
extends Node

signal health_changed(current: int, maximum: int)
signal died

func damage(amount: int) -> void:
    current = maxi(current - amount, 0)
    health_changed.emit(current, maximum)
    if current == 0:
        died.emit()

The health node does not know whether a label, camera shake, or death screen consumes the event. That separation is the point.

Connect locally and explicitly

Godot 4's typed callables make connections readable:

@onready var health: Health = $Health

func _ready() -> void:
    health.health_changed.connect(_on_health_changed)
    health.died.connect(_on_died)

func _on_health_changed(current: int, maximum: int) -> void:
    health_bar.value = current

Prefer connecting in the parent that owns both sides. If a node connects itself to a distant singleton in _ready, teardown and test setup become difficult to reason about. Editor-created connections are still architecture; inspect them before debugging duplicate callbacks.

Signals do not replace direct calls

Use direct calls when ownership is clear and the caller expects a result:

inventory.add_item(item)
door.open()

Use signals when the emitter should not know its observers:

health_changed.emit(current, maximum)
interaction_requested.emit(interactable)

A useful rule is direct down, signal up: parents command children directly; children notify parents with signals. Making every interaction a signal turns simple control flow into invisible jumps.

Keep callbacks traceable and teardown deliberate

Lambdas are fine for one-line UI glue, but named methods are searchable and visible in stack traces. For dynamically spawned objects, verify connection lifetimes and defer destructive work when ordering is unclear:

button.pressed.connect(_on_start_pressed)
call_deferred("free_pickup")

Deferred calls are a pressure valve, not an architecture. If everything needs deferral, ownership is probably inverted.

Field notes

  • Connecting the same callable twice can produce duplicate callbacks.
  • Signal arguments are API surface; changing them silently breaks observers.
  • Pass values or IDs rather than whole nodes when observers do not need node access.
  • A signal named update_stuff means the event boundary was never understood.
  • If observer order matters, use an explicit coordinator instead of hoping signal order stays stable.

Was this lesson helpful?

Sign in to rate