Skip to content

state

FreeBodyEngine.core.state #

State(enter=None, func=None, exit=None) dataclass #

A Generic State, the state's function must return the name of the next state, otherwise it must not return anything.

enter = None class-attribute instance-attribute #

exit = None class-attribute instance-attribute #

func = None class-attribute instance-attribute #

StateMachine(states) #

Runs a set of named States, one active at a time - see State for how a state's function signals a transition to another state.

states maps state name -> State. Starts with no current state set (current_state is '', which won't match any entry in states until set_state() is called).

current_state = '' instance-attribute #

states = states instance-attribute #

enter() #

Calls the current state's enter hook, if there is a current state and it defines one.

exit() #

Calls the current state's exit hook, if there is a current state and it defines one.

run() #

Calls the current state's func, if there is a current state and it defines one. Returns whatever func returns - per State's contract, either the next state's name or nothing.

set_state(state) #

Transitions to state: exits the current state, switches current_state, then enters the new one.

update(dt) #

Runs the current state's func and, if it returned a state name, transitions to that state.

Note: dt is accepted but unused - the state machine itself isn't time-based, it only relays a state function's own return value into a transition.