module Docs::D_NODE_DSL_AND_SIGNALS

Overview

D. Node DSL, Lifecycle & Signals

The node macro is the central declarative building block in LibGodot. It provides a clean, expressive DSL for authoring Godot classes in Crystal.


1. The node Macro Syntax

require "libgodot"

# Inherits from Godot::Node by default:
node CameraRig do
  # ...
end

# Inherits from a specific Godot class:
node Player < CharacterBody3D do
  # ...
end

# Subclassing another custom Crystal node:
node Warrior < Player do
  # ...
end

2. Lifecycle Callback Dispatch

Godot nodes execute lifecycle methods at specific stages in the frame lifecycle. LibGodot automatically detects these methods at compile time and registers virtual function pointers with the GDExtension bridge:

Virtual method calls are dispatched directly from the C++ bridge via call_virtual, bypassing Variant reflection for maximum execution speed.


3. Type-Safe Signal System

Signals allow nodes to notify observers when state changes occur, decoupled from listeners:

node BossEnemy < CharacterBody3D do
  # Define signals with typed arguments:
  signal phase_changed(new_phase : Int32)
  signal health_updated(current : Int32, max_health : Int32)
  signal defeated

  def take_damage(amount : Int32) : Void
    @health -= amount
    emit_health_updated(@health, @max_health)
    if @health <= 0
      emit_defeated
    end
  end
end

Under the hood:

  1. The signal declaration is harvested at compile time.
  2. A CrystalSignalDesc entry is registered into Godot's ClassDB.
  3. A type-safe emission helper method (emit_<signal_name>(...)) is generated.
  4. Arguments are converted into CrystalSignalArg buffers and dispatched via Bridge.object_emit_signal.
  5. GDScript, C#, and other Crystal nodes can connect to these signals natively.

4. Scene Tree APIs

Custom nodes have access to Godot's scene tree hierarchy methods:


5. In-Editor Tool Scripts (@[Tool] or tool)

Adding @[Tool] or invoking tool inside a node instructs Godot to run the node inside the Godot Editor in real time:

@[Tool]
node ProceduralArchway < Node3D do
  @[Export]
  property radius : Float32 = 5.0_f32

  def _process(delta : Float64) : Void
    # Runs inside Godot Editor! Updates visual mesh when radius changes.
  end
end

6. Network Replication (@[RPC])

Methods marked with @[RPC] configure multiplayer network replication:

@[RPC(mode: :any_peer, call_local: true)]
def sync_position(pos : Vector3) : Void
  self.position = pos
end

Defined in:

libgodot/docs.cr

Class Method Summary

Class Method Detail

def self.lifecycle_methods : Array(String) #

Dummy method for documentation visibility


[View source]