Signals, Callbacks & Event Bus

🔔 Signals, Callbacks & The Event Bus Pattern in Godot 4

Signals are Godot’s implementation of the Observer Pattern. They decouple sender nodes from receiver nodes, preventing spaghetti code where deep child nodes hold hardcoded references to UI or manager nodes.


🏛️ The Golden Rule of Godot Architecture

"Call Down, Signal Up."


🚌 2. The Global Event Bus (Autoload Singleton)

When two unrelated nodes on opposite sides of the scene tree need to communicate (e.g., an Enemy dying in the 3D world and a ScoreCounter updating in the 2D HUD):

# EventBus.gd (Registered as Autoload)
extends Node

signal player_health_changed(current_hp: float, max_hp: float)
signal enemy_defeated(score_value: int)
signal level_completed()
# Enemy.gd (Broadcasting)
func die() -> void:
    EventBus.enemy_defeated.emit(100)
    queue_free()

# ScoreUI.gd (Listening)
func _ready() -> void:
    EventBus.enemy_defeated.connect(_on_enemy_defeated)

func _on_enemy_defeated(points: int) -> void:
    score += points