class Eagle::Emitter(*T)

Overview

A typed event that other code can subscribe to. This is Eagle's version of a Godot signal.

You normally create emitters with the signal macro, which adds three methods to your class: name returns the Emitter, on_name { ... } connects a handler and emit_name(...) fires it. Handlers receive the declared arguments with their types checked at compile time.

Signals keep nodes decoupled. A player can announce that it died without knowing about the HUD, the score or the sound system.

class Player < Node2D
  signal hit(damage : Int32)
  signal died

  @hp = 3

  def damage(amount : Int32) : Nil
    @hp -= amount
    emit_hit(amount)
    emit_died if @hp <= 0
  end
end

player = Player.new
player.on_hit { |dmg| puts "ouch, #{dmg}" }
player.died.once { puts "game over" } # runs a single time
player.damage(3)

Defined in:

eagle/core/signal.cr

Instance Method Summary

Instance Method Detail

def clear : Nil #

Removes every handler.


def connect(&block : *T -> Nil) : Proc(*T, Nil) #

Adds a handler and returns it, so you can #disconnect it later.


def connected? : Bool #

True when at least one handler is connected.


def disconnect(handler : Proc(*T, Nil)) : Nil #

Removes a handler returned by #connect or #once.


def emit(*args : *T) : Nil #

Calls every connected handler with args. Handlers may connect or disconnect others while this runs.


def empty? : Bool #

True when no handlers are connected.


def once(&block : *T -> Nil) : Proc(*T, Nil) #

Adds a handler that runs on the next emit only, then disconnects itself.


def size : Int32 #

Number of connected handlers.