Skip to content

StateMachine

Inherits: Resource

Manages the current State for some part of the application.

Description

A StateMachine is responsible for managing an arbitrary number of State objects. The StateMachine keeps a "stack" of states that can be set, pushed, popped, removed, or cleared, and will fire signals for each kind of change. This can allow the application to update and respond to different states of the StateMachine. Only one State is considered the "current" state in a StateMachine: the last state in the stack. A State will fire the "entered" signal whenever it becomes the "current" state, and fires the "exited" signal whenever it leaves the "current" state. The StateMachine takes advantage of the fact that Godot resources are globally unique. This allows you to load a StateMachine resource from anywhere in the project to subscribe to state changes.

Properties

Type Name Default
String logger_name "StateMachine"
int minimum_states 1
State[] allowed_states []
CustomLogger logger

Methods

Returns Signature
State current_state()
void refresh()
void set_state(new_stack: State[])
void push_state(state: State)
void push_state_front(state: State)
State pop_state()
void replace_state(state: State)
void remove_state(state: State)
void clear_states()
int stack_length()
State[] stack()
bool has_state(state: State)

Property Descriptions

logger_name

String logger_name = "StateMachine"

Name of the state machine to use for logging purposes

minimum_states

int minimum_states = 1

The minimum number of states that this StateMachine must have. This parameter can be used to ensure that states cannot be popped below this number of states.

allowed_states

State[] allowed_states = []

If set, only the given State objects will be allowed to be added to the StateMachine. Will panic if an invalid state is added to the stack. If empty, all State objects will be allowed.

logger

CustomLogger logger

Note

There is currently no description for this property. Please help us by contributing one!


Method Descriptions

current_state()

State current_state()

Returns the current state at the end of the state stack

refresh()

void refresh()

Emits the 'refreshed' signal on the current State. This can be used to trigger hand-offs between multiple state machines.

set_state()

void set_state(new_stack: State[])

Set state will set the entire state stack to the given array of states

push_state()

void push_state(state: State)

Push state will push the given state to the top of the state stack.

push_state_front()

void push_state_front(state: State)

Pushes the given state to the front of the stack

pop_state()

State pop_state()

Pop state will remove the last state from the stack and return it.

replace_state()

void replace_state(state: State)

Replaces the current state at the end of the stack with the given state

remove_state()

void remove_state(state: State)

Removes all instances of the given state from the stack

clear_states()

void clear_states()

Removes all states

stack_length()

int stack_length()

Returns the length of the state stack

stack()

State[] stack()

Returns the current state stack

has_state()

bool has_state(state: State)

Returns true if the given state exists anywhere in the state stack