loomgui.com ↗

Events

Events are one-shot messages from C# to the UI. They are useful for moments the UI should react to, but does not need to store as durable state.

Define an event

public sealed class DamageEvent
{
    public int Amount { get; set; }
    public string Source { get; set; } = "";
}

public Event<DamageEvent> TookDamage { get; } = new();

Fire an event

TookDamage.Fire(new DamageEvent
{
    Amount = 12,
    Source = "Fire",
});

Listen in the UI

Use onEvent from your UI code:

import { onEvent } from '@loomgui/bridge';

onEvent('tookDamage', (payload) => {
  showDamageFlash(payload.amount);
});

Events vs state

Use state for current truth:

  • Current health
  • Current screen
  • Current inventory
  • Current loading progress

Use events for transient moments:

  • Damage flash
  • Toast message
  • Reward burst
  • Menu open sound
  • Achievement animation

If the UI needs to recover the value after reload or reconnect, it should be state, not only an event.