CSimpleStateMachine

Overview

Enum-based state machine with typed transitions, variant data storage, auto-forward, semaphore-locked actions, and debug output.

Classes

Name Description
UCSTMachine The state machine. Holds states, current state, variant data.
UCSTMachineState A state with an enum value, an action lambda, and an array of transitions.
UCSTMachineTransition A transition with a target state and a condition lambda.

Interface

Name Description
ICSTMachineOwner Implement getSTMachine() to expose state machines. Provides getSTMachine(FName), resetAnyStateMachine(), resetOtherStateMachine().

Utility

Name Description
StateMachineUtility::moveOnAllSimpleStateMachine Advances all state machines of a given family on all components of an actor.

Macros

Name Description
SMT_NEW_STATE Create a state with transitions and action.
SMT_BOOL_TRANSITION Transition based on a bool variant value.
SMT_COMPLEX_BOOL_TRANSITION Transition based on a complex bool expression.
SMT_AUTO_TRANSITION Unconditional transition.
// Define an enum for states
UENUM() enum class EMyState : uint8 { Idle, Walking, Attacking };

// Define an enum for variant data
UENUM() enum class EMyVar : uint8 { ShouldAttack };

// Create states with transitions
UCSTMachine* machine = createObjectAtRuntime<UCSTMachine>(this, [this](UCSTMachine* m) {
    m->init<EMyState>(FName("Combat"), {
        SMT_NEW_STATE(EMyState::Idle, {
            // action when entering Idle
        },
            SMT_BOOL_TRANSITION(EMyState::Attacking, EMyVar::ShouldAttack, true)
        ),
        SMT_NEW_STATE(EMyState::Attacking, {
            // action when entering Attacking
            m_->set(EMyVar::ShouldAttack, false);
        },
            SMT_AUTO_TRANSITION(EMyState::Idle)
        )
    });
});

// Trigger a transition
machine->write(EMyVar::ShouldAttack, true);

// Check current state
if (machine->statusIs(EMyState::Attacking)) { /* ... */ }

// Reset
machine->reset();

// Move all state machines of a family on an actor
StateMachineUtility::moveOnAllSimpleStateMachine(myActor, CSTMachineFamillyName::CNAME_STM_ANIMATION_FAMILLY);

Predefined families

Name Description
CNAME_STM_ANIMATION_FAMILLY Animation state machines.
CNAME_STM_TURNBYTURN_FAMILLY Turn-by-turn state machines.