Skip to main content

cturtle/events

include "cturtle/events";

Events let one part of a game tell the rest that something happened.

Each kind of event is an object type: declare an object whose fields hold the event's data, register it once with eventRegister, then subscribe to it and fire it by naming the type:

object ScoreChanged { int player; float score; }

eventRegister(typeof(ScoreChanged), eventFifo())?;
channel<ScoreChanged> scores = eventSubscribe(typeof(ScoreChanged), 16)?;
eventFire(ScoreChanged { player: 1, score: 10.0 })?;

The engine's own events, such as this package's UiInputEvent, are object types too. Subscribe to them the same way; they need no registering.

eventRegister, eventSubscribe and eventFire fail with: -450 when the type has not been registered as an event, -451 when an event of that name is already registered with different fields, -452 when scripts may not fire, or may not subscribe to, that event, -453 when the type cannot be an event (it is not an object, or a field is not bool, int, float, string or Entity), and -454 when delivery could not be set up.

Types​

EventDelivery​

Object

How an event reaches each subscriber; see eventFifo and eventConflate.

ResolvedCollisionHit​

Object

FieldTypeWritableDescription
sourceEntityyes
targetEntityyes
proxyEntityyes
providerintyes
payloadintyes
layerintyes
maskintyes
tagintyes
tfloatyes
exitTfloatyes
xfloatyes
yfloatyes
zfloatyes
normalXfloatyes
normalYfloatyes

UiInputEvent​

Object

FieldTypeWritableDescription
kindintyes
surfaceintyes
hitTokenintyes
buttonintyes
xfloatyes
yfloatyes
dxfloatyes
dyfloatyes
scrollXfloatyes
scrollYfloatyes
keyintyes
modifiersintyes
codepointintyes
textstringyes

UiSurfaceResized​

Object

FieldTypeWritableDescription
surfaceintyes
widthintyes
heightintyes

UiElementLayoutChanged​

Object

FieldTypeWritableDescription
elementintyes
xfloatyes
yfloatyes
widthfloatyes
heightfloatyes

UiTransitionFinished​

Object

FieldTypeWritableDescription
elementintyes
propertyintyes

UiBrowserLocationChanged​

Object

FieldTypeWritableDescription
locationstringyes

GameSessionStateChanged​

Object

FieldTypeWritableDescription
stateintyes
statusintyes
messagestringyes

FileDropped​

Object

FieldTypeWritableDescription
pathstringyes
indexintyes
countintyes

WindowCloseRequested​

Object

FieldTypeWritableDescription
requestintyes

StudioDocumentSnapshotRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes

StudioDocumentReplaceRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes
revisionintyes
sourcestringyes

StudioDocumentEditsRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes
revisionintyes
labelstringyes

StudioDocumentSaveRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes
revisionintyes

StudioSourceRevealRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes
lineintyes
columnintyes

StudioPluginFailed​

Object

FieldTypeWritableDescription
pluginintyes
messagestringyes

StudioGameRunRequested​

Object

FieldTypeWritableDescription
requestintyes
pluginintyes
generationintyes
uristringyes

ConsoleStateChanged​

Object

FieldTypeWritableDescription
revisionintyes
openboolyes

ConsoleLineAdded​

Object

FieldTypeWritableDescription
linestringyes

ConsoleStreamRequested​

Object

FieldTypeWritableDescription
maxLinesintyes

ConsoleCommandSubmitted​

Object

FieldTypeWritableDescription
commandstringyes

ConsoleDismissed​

Object

FieldTypeWritableDescription
byEscapeboolyes

ResolvedCollisionBatch​

Object

FieldTypeWritableDescription
sequenceintyes
partintyes
lastboolyes
startXfloatyes
startYfloatyes
endXfloatyes
endYfloatyes
radiusfloatyes
hitsarray<ResolvedCollisionHit>yes

Functions​

eventRegister​

fn eventRegister(type<$T> payload, EventDelivery delivery)

Makes the object type T an event that scripts can subscribe to and fire. Its fields must be bool, int, float, string or Entity. Registering the same type again succeeds; a type of the same name with different fields fails with -451.

eventSubscribe​

fn eventSubscribe(type<$T> payload, int capacity) -> channel<$T>

A new channel receiving every T event fired from now on. capacity is how many events the channel holds; with eventFifo delivery, further events wait in a queue instead of being dropped.

eventFire​

fn eventFire($T event)

Sends event to the engine and to every channel subscribed to its type.

eventFifo infallible​

infallible fn eventFifo() -> EventDelivery

Every event, in order. Events sent to a full channel wait in a queue instead of being dropped.

eventConflate infallible​

infallible fn eventConflate() -> EventDelivery

Only the newest event each subscriber has not yet received. The last event fired always arrives.