class
Eagle::Emitter(*T)
- Eagle::Emitter(*T)
- Reference
- Object
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.crInstance Method Summary
-
#clear : Nil
Removes every handler.
-
#connect(&block : *T -> Nil) : Proc(*T, Nil)
Adds a handler and returns it, so you can
#disconnectit later. -
#connected? : Bool
True when at least one handler is connected.
- #disconnect(handler : Proc(*T, Nil)) : Nil
-
#emit(*args : *T) : Nil
Calls every connected handler with args.
-
#empty? : Bool
True when no handlers are connected.
-
#once(&block : *T -> Nil) : Proc(*T, Nil)
Adds a handler that runs on the next emit only, then disconnects itself.
-
#size : Int32
Number of connected handlers.
Instance Method Detail
Calls every connected handler with args. Handlers may connect or disconnect others while this runs.
Adds a handler that runs on the next emit only, then disconnects itself.