DriftScript
Documentation

Guide

Events and state machines

Typed events over the host queue, and state as a declaration rather than a variable.

Two forms that exist because everybody writes them anyway, and writing them by hand is where the mistakes live.

Events are typed and go through the host queue

The language integrates with the host's priority queue rather than inventing a second one, so an event a script emits and an event the host emits are the same kind of thing, ordered together, deduped together.

// A task is asynchronous behaviour with an owner.
//
// `spawn` starts one inside a `scope`, and leaving the scope cancels whatever has not finished.
// That is the rule that stops detached work outliving the thing that created it, and it is
// structural here rather than something every author has to remember.
//
// `begin` is a task rather than a function, and the compiler insists: a scope opened in a function
// would be left on the same tick it was opened, cancelling everything spawned into it.

data Signal {
    fired: bool = false
    count: u32 = 0
}

task pulse(signal: mut Signal) {
    await fixedTime(250ms)
    signal.count = signal.count +| 1
    signal.fired = true
}

task begin(signal: mut Signal) {
    scope effect {
        spawn pulse(signal)
        await fixedTime(1s)
    }
}
A record, a task, and a scope that owns it.

A state is a declaration

State is a `state` declaration rather than a variable somebody remembers to update, so a transition to a state nobody wrote is a compile error rather than a machine that silently does nothing.