Aego Русский

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
On this page

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.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene2 nodes
State indicator
Transform
position[0, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")tintColor#f59e0bffsizeVec2[6, 3]
State Machine
states[]string["Idle", "Run"]initialstring"Idle"
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40clearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
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

NameTypeDefaultDescription
States[]string[1 items]

State names, also the suffix of the hook methods.

Initialstring"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.

NameTypeDefaultDescription
Currentstring""

State now.

Previousstring""

State before the last change.

Timefloat0

Seconds of fixed steps spent in the current state.

Indexint0

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.