Appearance
twine / StateMachine
Class: StateMachine
Defined in: state-machine.ts:39
A scoreboard-backed finite state machine - the shared primitive behind quests, dialogue trees, puzzles and story flow, so each isn't re-hand-rolled from raw score latches. One Objective holds the machine; a fake-player holder is one instance (run several instances of the same machine by varying the holder).
The per-tick dispatch reads a snapshot of the current state and carries a "transitioned this tick" guard, which avoids the classic datapack footgun where a transition lands in a state whose block then also runs in the same tick. Build it, then drive it by calling the returned dispatch ref from a module's onTick (so it composes with area gating and tick throttling).
Constructors
Constructor
new StateMachine(
dp,name,instance?):StateMachine
Defined in: state-machine.ts:50
Parameters
dp
Datapack
name
string
instance?
string = ...
Returns
StateMachine
Properties
name
readonlyname:string
Defined in: state-machine.ts:52
Methods
build()
build():
FunctionRef
Defined in: state-machine.ts:130
Emit the machine: the load seeding of the initial state and the per-tick dispatch function. Returns the dispatch FunctionRef - call it from a module's onTick (optionally throttled) to run the machine.
Returns
FunctionRef
go()
go(
ctx,label):void
Defined in: state-machine.ts:117
Force a transition to label now: runs the current state's onExit (whichever it is), sets the state, then runs label's onEnter. Use for event-driven jumps (a player click, a command) outside the guard-evaluated dispatch.
Parameters
ctx
FunctionContext
label
string
Returns
void
initial()
initial(
label):this
Defined in: state-machine.ts:83
Set the state the machine starts in (seeded in load).
Parameters
label
string
Returns
this
is()
is(
label):ExpressionNode
Defined in: state-machine.ts:102
A condition that holds while the machine is in label - for external gating.
Parameters
label
string
Returns
ExpressionNode
state()
state(
label,config?):this
Defined in: state-machine.ts:75
Declare a state and its lifecycle bodies.
Parameters
label
string
config?
StateConfig = {}
Returns
this
transition()
transition(
from,to,when):this
Defined in: state-machine.ts:89
A guarded transition: while in from, switch to to once when holds.
Parameters
from
string
to
string
when
ExpressionNode
Returns
this