Skip to content
This repository was archived by the owner on Nov 16, 2025. It is now read-only.

HierarchicalStateMachine isInState()

ged edited this page Sep 10, 2024 · 3 revisions

Definition

Checks if the state machine is currently in the specified state.

template<class TContext>
bool isInState(HierarchicalStateMachine<TContext>::State& state) const

Type Parameters

  • TContext: The type of the context, which holds data or behavior relevant to the state machine.

Parameters

  • state: The state to be compared.

Returns

true if the state is the current state, otherwise false.

Remarks

This function allows querying whether the current state matches the provided state.
Highly useful in hierarchical state machines, if you only want to execute a part of your code on that certain callback.

Usage

struct FooContext
{
static HierarchicalStateMachine<FooContext>::State state1;
static HierarchicalStateMachine<FooContext>::State state2;
static HierarchicalStateMachine<FooContext>::State state3;

int count = 0;

HierarchicalStateMachine<FooContext>* hsm = nullptr;
};

auto ctx = FooContext();
auto hsm = HierarchicalStateMachine<FooContext>(&ctx, true);
ctx.hsm = &hsm;

HierarchicalStateMachine<FooContext>::State FooContext::state1(nullptr, state1_enter, state1_update, state1_exit);
HierarchicalStateMachine<FooContext>::State FooContext::state2(&FooContext::state1, state2_enter, state2_update, nullptr);
HierarchicalStateMachine<FooContext>::State FooContext::state3(&FooContext::state1, nullptr, state3_update, state3_exit);

...

static void state1_enter(FooContext* const ctx)
{
    ctx->count = 0;
    // prints state1 enter
    if (ctx->hsm->isInState(FooContext::state1))
    {
        // prints state1 is in state (enter)
    }
}

static void state2_enter(FooContext* const ctx)
{
    // prints state2 enter
    if (ctx->hsm->isInState(FooContext::state2))
    {
        
        // prints state2 is in state (enter)
    }
}

static void state1_update(FooContext* const ctx)
{
    // prints state1 update
    ctx->count++;

    if (ctx->count == 2)
    {
        ctx->hsm->transitionTo(FooContext::state2);
    }

    if (ctx->hsm->isInState(FooContext::state1))
    {
        // prints state1 is in state (update)
    }
}

static void state2_update(FooContext* const ctx)
{
    // prints state2 update
    if (ctx->hsm->isInState(FooContext::state2))
    {
        // prints state2 is in state (update)
    }
}

static void state3_update(FooContext* const ctx)
{
    // prints state3 update
    if (ctx->hsm->isInState(FooContext::state3))
    {
        // prints state3 is in state (update)
        ctx->hsm->transitionTo(FooContext::state1);
    }
}

the above code will result to: (assuming S3 is the initial state)

S1 ENTER
S2 ENTER
S3 UPDATE
S3 IS IN STATE (UPDATE)
S3 EXIT
S2 EXIT
S1 EXIT
S1 ENTER
S1 IS IN STATE (ENTER)
S1 UPDATE
S1 IS IN STATE (UPDATE)
S1 UPDATE 
S1 IS IN STATE (UPDATE)
S2 ENTER
S2 IS IN STATE (ENTER)
S1 UPDATE
S2 UPDATE
S2 IS IN STATE (UPDATE)

Clone this wiki locally