Skip to content

中文 | English

API: wingman.fsm ​

State machine module providing event-driven state machine functionality, used for scenarios such as managing game states and flow control.

Module Overview ​

The fsm module provides state machine creation and management:

  • Create state machines - Create a named state machine with a specified initial state
  • Define states - Define states with enter/exit callbacks
  • Define transitions - Define state transition rules with guard function support
  • Dispatch events - Trigger state transitions
  • Query state - Get the current state, check states

Creating a State Machine ​

create(name, initial) ​

Description: Creates a new state machine instance.

Signature:

python
create(name: str, initial: str) -> str
lua
create(name: string, initial: string) -> string

Parameters:

  • name - State machine name
  • initial - Initial state name

Returns:

  • The state machine ID
python
from wingman import fsm

# 创建状态机
machine_id = fsm.create("combat", "idle")

Defining States ​

state(machine_id, state_name, on_enter?, on_exit?) / state(machineId, stateName, onEnter?, onExit?) ​

Description: Defines or updates a state and its callbacks.

Signature:

python
state(machine_id: str, state_name: str, on_enter: Callable = None, on_exit: Callable = None) -> bool
lua
state(machineId: string, stateName: string, onEnter: function = nil, onExit: function = nil) -> boolean

Parameters:

  • machine_id / machineId - State machine ID
  • state_name / stateName - State name
  • on_enter / onEnter - Optional; callback invoked when entering the state, receives a context object {state, from?, event, payload}
  • on_exit / onExit - Optional; callback invoked when leaving the state, receives a context object {state, event, payload}

Returns:

  • Whether the operation succeeded
python
from wingman import fsm

machine_id = fsm.create("combat", "idle")

# 定义状态
fsm.state(machine_id, "idle", on_enter=lambda ctx: print("进入空闲状态"))
fsm.state(machine_id, "fight", on_enter=lambda ctx: print("进入战斗状态"))

Defining State Transitions ​

transition(machine_id, from, to, on?, guard?, action?) / transition(machineId, from, to, on?, guard?, action?) ​

Description: Defines a state transition rule.

Signature:

python
transition(machine_id: str, from: str, to: str, on: str = "", guard: Callable = None, action: Callable = None) -> bool
lua
transition(machineId: string, from: string, to: string, on: string = "", guard: function = nil, action: function = nil) -> boolean

Parameters:

  • machine_id / machineId - State machine ID
  • from - Source state
  • to - Target state
  • on - Optional; the event name that triggers the transition
  • guard - Optional; guard function that receives {from, to, event, payload}; the transition is allowed only when it returns True/true
  • action - Optional; transition action function that receives {from, to, event, payload}

Returns:

  • Whether the operation succeeded
python
from wingman import fsm

# 定义状态转移
fsm.transition(machine_id, "idle", "fight", on="enemy_found")
fsm.transition(machine_id, "fight", "idle", on="enemy_lost")

# 带守卫函数
fsm.transition(machine_id, "pending", "running",
                on="start", guard=lambda ctx: ctx['payload'].get('ready', False))

Dispatching Events ​

dispatch(machine_id, event, payload?) / dispatch(machineId, event, payload?) ​

Description: Dispatches an event to the state machine, triggering a state transition if one applies.

Signature:

python
dispatch(machine_id: str, event: str, payload: dict = None) -> bool
lua
dispatch(machineId: string, event: string, payload: table = nil) -> boolean

Parameters:

  • machine_id / machineId - State machine ID
  • event - Event name
  • payload - Optional; event payload

Returns:

  • Whether a transition was successfully triggered
python
from wingman import fsm

# 派发事件
fsm.dispatch(machine_id, "enemy_found", {"target": "boss"})

Getting the Current State ​

current(machine_id) / current(machineId) ​

Description: Gets the current state name.

Signature:

python
current(machine_id: str) -> str
lua
current(machineId: string) -> string

Parameters:

  • machine_id / machineId - State machine ID

Returns:

  • The current state name

Resetting the State Machine ​

reset(machine_id) / reset(machineId) ​

Description: Resets the state machine to its initial state.

Signature:

python
reset(machine_id: str) -> bool
lua
reset(machineId: string) -> boolean

Parameters:

  • machine_id / machineId - State machine ID

Returns:

  • Whether the operation succeeded
python
from wingman import fsm

# 重置状态机
fsm.reset(machine_id)

Events ​

fsm.changed ​

After every successful state machine transition, the module emits an fsm.changed event. Payload fields:

  • machine - State machine name
  • from - Source state name
  • to - Target state name
  • event - Name of the event that triggered the transition

Subscribe with wingman.event.on("fsm.changed", cb) to observe state changes in one place.


Available APIs ​

Python FunctionLua FunctionDescriptionParameters
create(name, initial)create(name, initial)Create a state machinename: state machine name
initial: initial state name
Returns: state machine ID
state(machineId, stateName, onEnter?, onExit?)state(machineId, stateName, onEnter?, onExit?)Define a statemachineId: state machine ID
stateName: state name
onEnter: enter callback (optional)
onExit: exit callback (optional)
Returns: whether it succeeded
transition(machineId, from, to, on?, guard?, action?)transition(machineId, from, to, on?, guard?, action?)Define a transitionmachineId: state machine ID
from: source state
to: target state
on: event name (optional)
guard: guard function (optional)
action: action function (optional)
Returns: whether it succeeded
dispatch(machineId, event, payload?)dispatch(machineId, event, payload?)Dispatch an eventmachineId: state machine ID
event: event name
payload: event payload (optional)
Returns: whether a transition was triggered
current(machineId)current(machineId)Get the current statemachineId: state machine ID
Returns: current state name
reset(machineId)reset(machineId)Reset the state machinemachineId: state machine ID
Returns: whether it succeeded