# State machines

> Aego 0.12.5 · development; docs build `4a32b816933e02930662f6a443cb25abe6050d090c55e237cbff5c44ae9b25f8`. [Build manifest](/docs-build.json).

A [State Machine](/ref/gameplay.StateMachine.md) lists state names; the scripts on its node hold the behaviour. For a state `Run` it calls, when they exist:

| Hook | When |
|---|---|
| `EnterRun()` | the machine enters Run |
| `ExitRun()` | the machine leaves Run |
| `FixedRun(dt float)` | every fixed step while in Run, before Motion 2D moves |
| `UpdateRun(dt float)` | every frame while in Run, after the scripts' Update |

    func UpdateIdle(dt float) {
        if node.Get<Motion2D>().Velocity.X != 0 {
            node.Get<StateMachine>().Go("Run")
        }
    }

    func EnterRun() {
        node.Get<SpriteRenderer>().Tint = RunColor
    }

`Go(state)` only queues the change: after the running hook returns, Exit of the old state and Enter of the new one run, then `Changed(from, to)` fires with the state indices. A `Go` inside Enter or Exit runs after that transition, so `EnterRun` may send the machine straight on to `Air`; at most eight changes happen in a row. `Is(state)` and `TimeIn()` read the state; Current, Previous and Time show it in the Inspector.

The machine enters Initial in the first frame, after the scripts' Ready. Hooks are found by name and found again after a hot reload, so renaming a hook takes effect at once. Lifecycle methods, event handlers and coroutines are never hooks. Problems warns about a machine without states, a state listed twice or an Initial that is not one of the states ([GMP005](/diagnostics.html#GMP005.md)).

Walk, stop and jump in the scene: the colour and the label follow the state.
