This guide shows how to define states, group them under a controller, switch between them, and follow best practices for reliable state-machine behavior in APIC.

1. Create a state

A State is an implementation of a logical execution of a single state unit. Define it by deriving from CACState:

class CSampleState : public CACState
{
public:
    CSampleState() : CACState(1000) {}       // period in MICROseconds: here every 1 ms
    const char* Name() const override { return "SampleState"; }
    void OnStateEntered() override {}
    bool OnStateChanged(EStateStatus eStateStatus) override { return true; }
    std::unique_ptr<CACState> Execute() override;
};

// SampleState.cpp
REGISTER_TYPE(CSampleState);

The constructor must pass the state's period to CACState in microseconds (there is no default): the state runs every period / minor frame frames when its controller is executed; a period of one minor frame or less (or 0) = every time. The state class needs a default constructor, and REGISTER_TYPE in its .cpp (AddController<T> and switching by name use it). Full step-by-step: How to develop a state machine.

2. Group states with a State Controller

A State Controller groups multiple states into a logical execution unit. Only one state runs at a time in each controller.

3. Initialize a controller in your model

Each model can declare and initialize its own state controllers:

// Add a controller with initial state
CStatesController::AddController<CSampleState>("StateControllerName");

// Set controller status
CStatesController::SetStateStatus(
"StateControllerName",
CACState::EStateStatus::ePlay
);

// Execute state
CStatesController::ExecuteState("StateControllerName");

4. Control the controller's status

Use SetStateStatus to change how a controller runs:

CStatesController::SetStateStatus("StateControllerName", CACState::EStateStatus::ePause);
ModeEffect
PlayThe state machine actively runs (ExecuteState() runs the current state).
PauseExecution stops but the current state is preserved. A new controller starts paused.
ResetExecution stops and the current state's OnStateChanged(eReset) is called. It does not go back to the initial state: switch to it with SwitchState, then set Play.

5. Switch between states

From inside a state's Execute() method, return the new state object:

return std::make_unique<COtherState>();      // or nullptr to stay

From anywhere else, call the controller directly:

CStatesController::SwitchState("StateControllerName", std::make_unique<COtherState>());
CStatesController::SwitchState("StateControllerName", "COtherState");   // by its REGISTER_TYPE name

State transitions occur by returning a new state object from Execute(). Returning nullptr keeps the current state active. At a switch the new state's OnStateEntered() runs, then the old state object is destroyed (its destructor is the exit action). Every switch creates a new state object (heap): switch on events, not every frame.

6. Implement the state lifecycle

OnStateChanged()

Called whenever the state controller changes the state status. Use it for cleanup or reset logic:

bool CSampleState::OnStateChanged(EStateStatus eStateStatus)
{
if (eStateStatus == EStateStatus::eReset) {
// Reset internal state
}
return true;
}

OnStateEntered()

Called once when the state becomes the controller's current state (entry action), before the previous state object is destroyed.

Execute()

This is where the core state functionality runs. The state may perform work, interact with LRUs, switch states, or maintain internal logic:

std::unique_ptr<CACState> CSampleState::Execute()
{
std::unique_ptr<CACState> switchToState;

// Access LRU
// lru_XXXX* pLRU = (lru_XXXX*)CLRUsProj::LRUXXXX->GetLRU();

// Change controller status if needed
// CStatesController::SetStateStatus("NormalFlow", CACState::EStateStatus::ePause);

// Switch to another state
// switchToState = std::make_unique<COtherState>();

return switchToState;
}

7. Follow best practices