Appearance
FSM
The FSM module provides a set of classes for creating finite state machines.
Example
lua
local MyModule = select(2, ...).MyModule
local FSM = MyModule:From("LibTSMUtil"):Include("FSM")
local fsm = FSM.New("ENEMY_NPC")
:AddState(FSM.NewState("ST_PATROLLING")
:SetOnEnter(function(context)
context.health = 100
end)
:AddTransition("ST_ATTACKING")
:AddEventTransition("EV_AGROED", "ST_ATTACKING")
)
:AddState(FSM.NewState("ST_ATTACKING")
:SetOnEnter(function(context)
print("I'm coming after you!")
end)
:AddTransition("ST_DEAD")
:AddTransition("ST_PATROLLING")
:AddEventTransition("EV_DEAGRO", "ST_PATROLLING")
:AddEvent("EV_DAMAGE", function(context, damage)
context.health = context.health - damage
if context.health <= 0 then
return "ST_DEAD"
end
end)
)
:AddState(FSM.NewState("ST_DEAD")
:SetOnEnter(function(context)
print("Argh!")
end)
:AddTransition("ST_PATROLLING")
:AddEventTransition("EV_RESPAWN", "ST_PATROLLING")
)
:Init("ST_PATROLLING", {health = 100})
fsm:ProcessEvent("EV_AGROED") -- print: I'm coming after you!
fsm:ProcessEvent("EV_DAMAGE", 70)
fsm:ProcessEvent("EV_DAMAGE", 40) -- print: Argh!
fsm:ProcessEvent("EV_RESPAWN")API
Inherits: LibTSMModule
Functions
New
lua
function FSM.New(name)
-> FSMObjectCreate a new FSM.
Parameters:
| Name | Type | Description |
|---|---|---|
name | string | The name of the FSM (for debugging purposes) |
Returns:
| Type | Description |
|---|---|
FSMObject |
NewState
lua
function FSM.NewState(state)
-> FSMStateCreate a new FSM state.
Parameters:
| Name | Type | Description |
|---|---|---|
state | string | The name of the state |
Returns:
| Type | Description |
|---|---|
FSMState |
Inherits: Class
Methods
AddState
lua
function FSMObject:AddState(stateObj)
-> selfAdd an FSM state.
Parameters:
| Name | Type | Description |
|---|---|---|
stateObj | FSMState | The FSM state object to add |
Returns:
| Type | Description |
|---|---|
self |
Init
lua
function FSMObject:Init(initialState, context)
-> selfInitialize the FSM.
Parameters:
| Name | Type | Description |
|---|---|---|
initialState | string | The name of the initial state |
context | table | The FSM context table which gets passed to all state and event handlers |
Returns:
| Type | Description |
|---|---|
self |
ProcessEvent
lua
function FSMObject:ProcessEvent(event, ...)
-> selfProcess an event.
Parameters:
| Name | Type | Description |
|---|---|---|
event | string | The name of the event |
... | any | Additional arguments to pass to the handler function |
Returns:
| Type | Description |
|---|---|
self |
SetLoggingEnabled
lua
function FSMObject:SetLoggingEnabled(enabled)
-> selfEnable or disable event and state transition logs (can be called recursively).
Parameters:
| Name | Type | Description |
|---|---|---|
enabled | boolean | Whether or not logging should be enabled |
Returns:
| Type | Description |
|---|---|
self |
Functions
New
lua
function FSMObject.New(name)
-> FSMObjectCreates a new FSM object.
Parameters:
| Name | Type | Description |
|---|---|---|
name | string | The name of the FSM (for debugging) |
Returns:
| Type | Description |
|---|---|
FSMObject |
Inherits: Class
Methods
AddEvent
lua
function FSMState:AddEvent(event, handler)
-> selfAdd a handled event.
Parameters:
| Name | Type | Description |
|---|---|---|
event | string | The name of the event |
handler | function | The function called when the event occurs |
Returns:
| Type | Description |
|---|---|
self |
AddEventTransition
lua
function FSMState:AddEventTransition(event, toState)
-> FSMStateAdd a simple event-based transition.
Parameters:
| Name | Type | Description |
|---|---|---|
event | string | The event name |
toState | string | The state to transition to |
Returns:
| Type | Description |
|---|---|
FSMState |
AddTransition
lua
function FSMState:AddTransition(toState)
-> selfAdd a transition.
Parameters:
| Name | Type | Description |
|---|---|---|
toState | string | The state this transition goes to |
Returns:
| Type | Description |
|---|---|
self |
SetOnEnter
lua
function FSMState:SetOnEnter(handler)
-> selfSet the OnEnter handler. This function is called upon entering the state.
Parameters:
| Name | Type | Description |
|---|---|---|
handler | function | The handler function to call |
Returns:
| Type | Description |
|---|---|
self |
SetOnExit
lua
function FSMState:SetOnExit(handler)
-> selfSet the OnExit handler. This function is called upon existing the state.
Parameters:
| Name | Type | Description |
|---|---|---|
handler | function | The handler function to call |
Returns:
| Type | Description |
|---|---|
self |
Functions
New
lua
function FSMState.New(name)
-> FSMStateCreate a new FSM state.
Parameters:
| Name | Type | Description |
|---|---|---|
name | string | The name of the state |
Returns:
| Type | Description |
|---|---|
FSMState |