中文 | 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:
create(name: str, initial: str) -> strcreate(name: string, initial: string) -> stringParameters:
name- State machine nameinitial- Initial state name
Returns:
- The state machine ID
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:
state(machine_id: str, state_name: str, on_enter: Callable = None, on_exit: Callable = None) -> boolstate(machineId: string, stateName: string, onEnter: function = nil, onExit: function = nil) -> booleanParameters:
machine_id/machineId- State machine IDstate_name/stateName- State nameon_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
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:
transition(machine_id: str, from: str, to: str, on: str = "", guard: Callable = None, action: Callable = None) -> booltransition(machineId: string, from: string, to: string, on: string = "", guard: function = nil, action: function = nil) -> booleanParameters:
machine_id/machineId- State machine IDfrom- Source stateto- Target stateon- Optional; the event name that triggers the transitionguard- Optional; guard function that receives{from, to, event, payload}; the transition is allowed only when it returnsTrue/trueaction- Optional; transition action function that receives{from, to, event, payload}
Returns:
- Whether the operation succeeded
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:
dispatch(machine_id: str, event: str, payload: dict = None) -> booldispatch(machineId: string, event: string, payload: table = nil) -> booleanParameters:
machine_id/machineId- State machine IDevent- Event namepayload- Optional; event payload
Returns:
- Whether a transition was successfully triggered
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:
current(machine_id: str) -> strcurrent(machineId: string) -> stringParameters:
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:
reset(machine_id: str) -> boolreset(machineId: string) -> booleanParameters:
machine_id/machineId- State machine ID
Returns:
- Whether the operation succeeded
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 namefrom- Source state nameto- Target state nameevent- 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 Function | Lua Function | Description | Parameters |
|---|---|---|---|
create(name, initial) | create(name, initial) | Create a state machine | name: state machine name initial: initial state name Returns: state machine ID |
state(machineId, stateName, onEnter?, onExit?) | state(machineId, stateName, onEnter?, onExit?) | Define a state | machineId: 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 transition | machineId: 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 event | machineId: state machine ID event: event name payload: event payload (optional) Returns: whether a transition was triggered |
current(machineId) | current(machineId) | Get the current state | machineId: state machine ID Returns: current state name |
reset(machineId) | reset(machineId) | Reset the state machine | machineId: state machine ID Returns: whether it succeeded |