Skip to content
Axiom/SDK
SDK / Events

Events

Some things a mod hears about rather than asks for: a button it drew was pressed, or a setting changed. They arrive as events, after the mod's frame, through one export the mod writes and one import every mod has.

Taking events

Export on_event. After a mod's on_frame, the host calls it once for each event waiting for that mod, with the event's id and the size of its payload. Inside it, axiom_event_read copies the payload into a buffer of the mod's.

EVENTS.Ca button press and a changed setting
static uint8_t s_Payload[128];

__attribute__((export_name("on_event"))) void OnEvent(uint32_t id,
                                                      uint32_t size) {
  if (size > sizeof(s_Payload) ||
      axiom_event_read(s_Payload, sizeof(s_Payload)) != AXIOM_EVENT_READ_OK) {
    return;
  }

  if (id == AXIOM_EVENT_UI_OVERLAY_PRESSED) {
    const AxiomUiOverlayPressedEvent* pressed =
        (const AxiomUiOverlayPressedEvent*)s_Payload;
    /* The id the mod gave the button, at an offset into the payload. */
    if (pressed->id.Offset + pressed->id.Length <= size) {
      const char* button = (const char*)s_Payload + pressed->id.Offset;
      OnPressed(button, pressed->id.Length);
    }
    return;
  }

  if (id == AXIOM_EVENT_CONFIG_CHANGED) {
    const AxiomConfigChangedEvent* changed =
        (const AxiomConfigChangedEvent*)s_Payload;
    if (changed->key.Offset + changed->key.Length <= size) {
      const char* key = (const char*)s_Payload + changed->key.Offset;
      OnChanged(key, changed->key.Length);
    }
  }
}

A mod without on_event takes no events, and that is not a fault: what was queued for it is dropped.

Reading a payload

axiom_event_read(out, capacity) is imported from axiom:event. It is part of the ABI rather than a capability, so every mod has it without declaring it.

Answer
Means
AXIOM_EVENT_READ_OK (0)
The payload was copied into out.
AXIOM_EVENT_READ_NONE (1)
There is no event to read: the call was made outside on_event.
AXIOM_EVENT_READ_TOO_SMALL (2)
capacity is smaller than size. Nothing was copied, because half a struct is not a smaller struct.

The payload is the generated struct named for the event: the event's fields in the order the IDL gives them. A string in it is an AxiomSpan whose Offset counts from the start of the payload, and the strings' bytes follow the fixed part. Cast the buffer to the struct, then read each string at the buffer plus its offset, after checking the offset and length fall inside size.

SURFACE.Hone event, as generated
#define AXIOM_EVENT_UI_OVERLAY_PRESSED 0x44f17222u
typedef struct AxiomUiOverlayPressedEvent {
  AxiomUiOverlayPanelId panel;
  AxiomSpan id;
} AxiomUiOverlayPressedEvent;

Event ids

An event's id is FNV-1a over its full name, such as ui.overlay@1.pressed, so it is the same number in every build and every language. surface.h defines a constant for each, and a struct for its payload:

Event
Constant
Payload
ui.overlay@1 pressed
AXIOM_EVENT_UI_OVERLAY_PRESSED
AxiomUiOverlayPressedEvent { panel, id }
config@1 changed
AXIOM_EVENT_CONFIG_CHANGED
AxiomConfigChangedEvent { key }
entity.query@1 spawned
AXIOM_EVENT_ENTITY_QUERY_SPAWNED
AxiomEntityQuerySpawnedEvent { entity }
entity.query@1 despawned
AXIOM_EVENT_ENTITY_QUERY_DESPAWNED
AxiomEntityQueryDespawnedEvent { entity }
entity.transform@1 moved
AXIOM_EVENT_ENTITY_TRANSFORM_MOVED
AxiomEntityTransformMovedEvent { entity, from, to }
input.observe@1 bound
AXIOM_EVENT_INPUT_OBSERVE_BOUND
AxiomInputObserveBoundEvent { id, key }
hook@1 called
AXIOM_EVENT_HOOK_CALLED
AxiomHookCalledEvent { hook }

Which events arrive today

Two are delivered, both in the Sandbox:

  • ui.overlay@1 pressed, when a button the mod drew is pressed in the overlay. It goes to the mod that drew the button, as the host recorded who was drawing, never as the mod named itself. id is the id the mod gave the button, and panel the panel it was in.
  • config@1 changed, once for each key whose value the person changed in the settings panel, to a mod that declared config@1. Read the new value with config@1 in the same callback or the next frame.

The others are declared in the surface and nothing sends them yet. A mod can handle them now and will hear them once a backend does.

Limits

  • A mod hears only its own capabilities. It gets events only from capabilities it declared, the same way it can import only those.
  • Sixteen a frame. At most sixteen events are delivered to a mod in one frame, out of the fuel its frame left. The rest wait for the next frame.
  • Sixty-four waiting. A mod falling behind keeps the oldest sixty-four, and newer ones are dropped and counted.
  • Next frame, not during the click. A press is delivered after the mod's next frame, never inside the click, so input state read in on_event is the frame's, not the moment of the press.