On this page
  1. Fields
    1. go
    2. state
    3. seconds_in_state

rmp::behavior::StateMachine

behavior #include <rmp/behavior.h> include/rmp/behavior.h:1049
struct StateMachine

Named states and the moves between them: an enemy that idles, patrols, chases and flees; a boss's phases. It starts in initial at its first update, asks the transitions in order every frame — the first whose from matches and whose when says yes is taken — and runs the state's on_update. go() moves by name, at its next update.

Added to an object with add<rmp::behavior::StateMachine>(); the framework calls its _update with the object it is on.

Fields

FieldTypeDefaultWhat it is
statesstd::vector<State>

Every state it can be in, by name

initialstd::string

"" = the first state

transitionsstd::vector<Transition>

Asked in order; the first that says yes is taken

oursstruct { … }

What it is keeping track of. Readable, because sometimes you want it; not yours to write. It is one PUBLIC member and not a private block because a struct with a private member is not an aggregate in C++20, and then { .speed = 320 } stops compiling — which is the whole design. The name is the fence.

go

void go(std::string_view name)

Moves to the state called name at its next update; a name it does not have is said once and changes nothing.

state

std::string_view state() const

The state it is in; "" before its first update.

seconds_in_state

float seconds_in_state() const

How long it has been in it, in seconds.