Skip to reading
PCA Script Programming

26. Event data and custom signals

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
when event signal_powered
    log("Source: " + str(event["source"]))
end
Code example
function run()
    emit_event("signal_powered", {"source": "lever"})
end

emit_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.

Page 34 of 357Permalink
Go to page