Easily dispatch events in your scene when stuff happens.
Declare an event type implementing IGameEvent
with all the properties you want to pass around.
public record DamagedEvent(
GameObject Attacker,
GameObject Victim,
int Damage ) : IGameEvent;
Implement IGameEventHandler<T>
for your custom event type in a Component
.
public sealed class MyComponent : Component,
IGameEventHandler<DamagedEvent>
{
public void OnGameEvent( DamagedEvent eventArgs )
{
Log.Info( $"{eventArgs.Victim.Name} says \"Ouch!\"" );
}
}
Dispatch the event on a GameObject
or the Scene
, which will notify any components in its descendants.
GameObject.Dispatch( new DamagedEvent( attacker, victim, 50 ) );
You can control the order that handlers are invoked using attributes on the handler method.
Early
: run this firstLate
: run this lastBefore<T>
: run this before T's handlerAfter<T>
: run this after T's handler
[Early, After<SomeOtherComponent>]
public void OnGameEvent( DamagedEvent eventArgs )
{
Log.Info( $"{eventArgs.Victim.Name} says \"Ouch!\"" );
}