State Machine
Named states with Enter, Exit, Fixed and Update hooks found by name on the node's scripts: EnterRun(), ExitRun(), FixedRun(dt), UpdateRun(dt). Scripts change the state with Go.
component gameplay.StateMachine
Try it
Choose the initial state
The State Machine enters Idle and calls its EnterIdle script hook, coloring the marker orange.
Change Initial from Idle to Run. The restarted machine enters Run and calls EnterRun, coloring the marker green. Reset restores Idle.
Scene2 nodes
Scriptmain.aes
package demo
// These are real state-machine hooks: the color changes when the machine
// enters a state, not by reading or mirroring its editable Initial field.
script StateIndicator {
func EnterIdle() {
node.Get<SpriteRenderer>().Tint = Color{1, 0.5, 0.05, 1}
}
func EnterRun() {
node.Get<SpriteRenderer>().Tint = Color{0.05, 0.8, 0.25, 1}
}
}
Fields
| Name | Type | Default | Description |
|---|---|---|---|
| States | []string | [1 items] | State names, also the suffix of the hook methods. |
| Initial | string | "Idle" | State entered when the node starts, after the scripts' Ready. Empty uses the first state. |
Runtime state
Values the component has while the game runs. They are not saved with the scene.
| Name | Type | Default | Description |
|---|---|---|---|
| Current | string | "" | State now. |
| Previous | string | "" | State before the last change. |
| Time | float | 0 | Seconds of fixed steps spent in the current state. |
| Index | int | 0 | Index of the current state in States. |
Methods
Go method
func (StateMachine) Go(state string) bool
Switches to a state after the current hook returns; Exit of the old state and Enter of the new one run in between. Returns false for an unknown state.
Is method
func (StateMachine) Is(state string) bool
Reports whether the machine is in a state.
TimeIn method
func (StateMachine) TimeIn() float
Returns the seconds spent in the current state.
Signals
Changed signal
signal Changed(from int, to int)
Fires after a state change with the indices of the old and the new state.
Related guides
- State machines A State Machine lists state names; the scripts on its node hold the behaviour.
- Gameplay kit The gameplay module adds the parts most 2D games rebuild by hand: a character controller, hit points, hitboxes and hurtboxes, kill zones, checkpoints with respawn, pickups and timed removal.