# rmp::behavior::StateMachine

> Named states and the moves between them: an enemy that idles, patrols, chases and flees; a boss's phases.

Source: https://omardev29.github.io/rmp-docs/reference/behaviors/StateMachine.html

behavior `#include <rmp/behavior.h>` [include/rmp/behavior.h:1049](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1049-L1075)

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

| Field | Type | Default | What it is |
|---|---|---|---|
| `states` | `std::vector<State>` |  | Every state it can be in, by name |
| `initial` | `std::string` |  | "" = the first state |
| `transitions` | `std::vector<Transition>` |  | Asked in order; the first that says yes is taken |
| `ours` | `struct { … }` |  | 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)

function [include/rmp/behavior.h:1057](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1057)

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

function [include/rmp/behavior.h:1059](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1059)

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

### `seconds_in_state`

float seconds_in_state() const

function [include/rmp/behavior.h:1061](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1061)

How long it has been in it, in seconds.
