Aego Русский

State machines

A State Machine 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).

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

Try it

Change the example parameters and compare the result. Reset restores the initial scene.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene6 nodes
Floor
Transform
position[0, 3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[40, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[40, 1]
Wall
Transform
position[-12, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[1, 6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[1, 6]
WallRight
Transform
position[19.5, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[1, 6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[1, 6]
Step
Transform
position[4, 2.4]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#6b7280fforiginVec2[0.5, 0.5]sizeVec2[3, 0.2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[3, 0.2]
Player
Transform
position[0, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint1tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[0.6, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumkinematicmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationbooltruebulletboolfalseallowSleepboolfalseinterpolatebooltruelayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0restitutionfloat0densityfloat1sizeVec2[0.6, 1]
Motion 2D
modeenumplatformermaxSpeedfloat6jumpHeightfloat2.2stepHeightfloat0.25
State Machine
states[]string["Idle", "Run", "Air"]initialstring"Idle"
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat36viewportRect[0, 0, 1, 1]maskint-1orderint0clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Virtual Camera 2D
priorityint0outputstring""zoomfloat0blendTimefloat0.5blendCurveCurve{ ease=inOutQuad }timeDomainenumunscaleduseBoundsboolfalseboundsRect[0, 0, 0, 0]snapToPixelsboolfalse
Camera Follow 2D
targetNodeRefnode("d6900000000000000000000000000003")deadzoneRect[0.4, 0.4, 0.2, 0.2]dampingVec2[5, 5]lookaheadVec2[0.2, 0]maxLookaheadVec2[2, 1]lookaheadSmoothingfloat3snapDistancefloat20
Scriptmain.aes
package demo

import (
    "aego/debug"
    "aego/input"
)

// Player keeps its states in a State Machine: Idle, Run and Air, each with its own hooks and colour.
script Player {
    func Update(dt float) {
        m := node.Get<Motion2D>()
        x := 0.0
        if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
            x -= 1
        }
        if input.Down(input.KeyD) || input.Down(input.KeyRight) {
            x += 1
        }
        m.Move(x)
        if input.KeyPressed(input.KeySpace) || input.KeyPressed(input.KeyW) || input.KeyPressed(input.KeyUp) {
            m.Jump()
        }
        if input.KeyReleased(input.KeySpace) || input.KeyReleased(input.KeyW) || input.KeyReleased(input.KeyUp) {
            m.JumpReleased()
        }
        sm := node.Get<StateMachine>()
        p := node.WorldPosition()
        debug.Text(Vec2{p.X - 0.4, p.Y - 1}, sm.Current, Color{1, 1, 1, 1})
    }

    func UpdateIdle(dt float) {
        m := node.Get<Motion2D>()
        if !m.Grounded {
            node.Get<StateMachine>().Go("Air")
        } else if m.Velocity.X > 0.5 || m.Velocity.X < -0.5 {
            node.Get<StateMachine>().Go("Run")
        }
    }

    func UpdateRun(dt float) {
        m := node.Get<Motion2D>()
        if !m.Grounded {
            node.Get<StateMachine>().Go("Air")
        } else if m.Velocity.X < 0.5 && m.Velocity.X > -0.5 {
            node.Get<StateMachine>().Go("Idle")
        }
    }

    func UpdateAir(dt float) {
        if node.Get<Motion2D>().Grounded {
            node.Get<StateMachine>().Go("Idle")
        }
    }

    func EnterIdle() {
        node.Get<SpriteRenderer>().Tint = Color{0.96, 0.62, 0.04, 1}
    }

    func EnterRun() {
        node.Get<SpriteRenderer>().Tint = Color{0.2, 0.8, 0.4, 1}
    }

    func EnterAir() {
        node.Get<SpriteRenderer>().Tint = Color{0.3, 0.6, 1, 1}
    }
}