26. Event data and custom signals
when event signal_powered
log("Source: " + str(event["source"]))
endfunction run()
emit_event("signal_powered", {"source": "lever"})
endemit_event() broadcasts the exact name passed to it and includes your data in the event context. The matching listener uses when event signal_powered. Use a name that describes what happened. This keeps the lever from having to know every lamp, door and journal entry that might care about power.
Within a dispatched handler, event is a dictionary snapshot of the context. Standard keys depend on the emitter: an interaction may include entityId, verb and itemId; a trigger includes its trigger ID. Check a missing value against null before you use it.
An exact shared hook can be written as when event OnInteraction. Filter its context in the body when the readable header has no suitable filter. Custom data should be a dictionary. A non-dictionary data value is placed under value.
Do not emit the same event from inside its own matching handler without a guard. A notification system can recurse just as effectively as a function can. Set one-time state before broadcasting and avoid a circular chain of listeners.
