State Machine

The State Machine toolbox holds what you place inside one state of a state machine. A state machine is a graph of states joined by named transitions; the machine is in one state at a time. Each state is a graph of its own with three events:

  • Enter runs once when the machine enters the state.
  • Update runs on every tick while the state is active, 60 times a second by default.
  • Exit runs when a transition leaves the state.

It also has Set Update Frequency, to change how often Update runs, and Stop, to end the machine.

Use a state machine for anything that is in one of a few modes at a time: a traffic light, a character that walks, jumps or falls, a door that is open, closing or shut. The machine itself, its transitions and the loop that runs it are in State Machine Runtime.

  • Included in: Divooka Explore and Divooka Compute, every tier. In Compute, add the State Machine Host reference with Add reference (Ctrl+Shift+L).
  • Maturity: Preview · Support: Tier 1 (proposed) · what these mean
  • Open it: node library (Ctrl+Shift+Space) → State Machine, or search for a node with Ctrl+Space.
  • Examples and reference: Ctrl+Shift+E → Special (Experimental) > State Machine · node reference

New to graphs? Start with Your first program.

Main ideas

Transitions are tried on every tick. A transition can have a guard, a Function graph that returns true or false. An unguarded transition is always taken when reached. If several can be taken, the one with the higher priority wins.

Entering a state starts it fresh. A transition from a state back to itself exits it and enters a new copy, so it acts as a reset.

A state with no way out is final. The machine stops after that state's first Update.

Example: a traffic light

Open Special (Experimental) > State Machine (Ctrl+Shift+E). The machine canvas shows three states joined by arrows:

  1. Green. Its Update adds 1 to Ticks, a data member of the document, and prints Green - go.
  2. Two transitions leave Green. warm (to Yellow, priority 10) has the guard Enough, a Function graph that returns whether Ticks is at least 2. again (back to Green, priority 0) has no guard.
  3. Yellow prints Yellow - slow down. Its transition cool to Red has no guard.
  4. Red prints Red - stop, after {0} ticks with the value of Ticks. It has no outgoing transition, so it is final.

Run it with F5:

Green - go
Green - go
Yellow - slow down
Red - stop, after 2 ticks

Green prints twice. On the first tick Ticks is 1, Enough is false, so the lower-priority again re-enters Green. On the second tick Enough is true and warm wins.

Tips and limits

  • Experimental. State machine graphs are still finding their place in Divooka, so the editor and the file format may change.
  • Share data through data members. A state is built fresh on every entry, so values that must last, such as Ticks here, belong on the document or the machine, not on the state.
  • Update Frequency is in hertz (ticks per second). The loop reads it again on every tick, so a change takes effect at once.

What is in the toolbox

Category Some of its nodes
State Machine State Make State Machine State, Machine, Update Frequency, Set Update Frequency and 4 more

See also