Skip to content

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)
  -> FSMObject

Create a new FSM.

Parameters:

NameTypeDescription
namestringThe name of the FSM (for debugging purposes)

Returns:

TypeDescription
FSMObject

NewState

lua
function FSM.NewState(state)
  -> FSMState

Create a new FSM state.

Parameters:

NameTypeDescription
statestringThe name of the state

Returns:

TypeDescription
FSMState

Inherits: Class

Methods

AddState

lua
function FSMObject:AddState(stateObj)
  -> self

Add an FSM state.

Parameters:

NameTypeDescription
stateObjFSMStateThe FSM state object to add

Returns:

TypeDescription
self

Init

lua
function FSMObject:Init(initialState, context)
  -> self

Initialize the FSM.

Parameters:

NameTypeDescription
initialStatestringThe name of the initial state
contexttableThe FSM context table which gets passed to all state and event handlers

Returns:

TypeDescription
self

ProcessEvent

lua
function FSMObject:ProcessEvent(event, ...)
  -> self

Process an event.

Parameters:

NameTypeDescription
eventstringThe name of the event
...anyAdditional arguments to pass to the handler function

Returns:

TypeDescription
self

SetLoggingEnabled

lua
function FSMObject:SetLoggingEnabled(enabled)
  -> self

Enable or disable event and state transition logs (can be called recursively).

Parameters:

NameTypeDescription
enabledbooleanWhether or not logging should be enabled

Returns:

TypeDescription
self

Functions

New

lua
function FSMObject.New(name)
  -> FSMObject

Creates a new FSM object.

Parameters:

NameTypeDescription
namestringThe name of the FSM (for debugging)

Returns:

TypeDescription
FSMObject

Inherits: Class

Methods

AddEvent

lua
function FSMState:AddEvent(event, handler)
  -> self

Add a handled event.

Parameters:

NameTypeDescription
eventstringThe name of the event
handlerfunctionThe function called when the event occurs

Returns:

TypeDescription
self

AddEventTransition

lua
function FSMState:AddEventTransition(event, toState)
  -> FSMState

Add a simple event-based transition.

Parameters:

NameTypeDescription
eventstringThe event name
toStatestringThe state to transition to

Returns:

TypeDescription
FSMState

AddTransition

lua
function FSMState:AddTransition(toState)
  -> self

Add a transition.

Parameters:

NameTypeDescription
toStatestringThe state this transition goes to

Returns:

TypeDescription
self

SetOnEnter

lua
function FSMState:SetOnEnter(handler)
  -> self

Set the OnEnter handler. This function is called upon entering the state.

Parameters:

NameTypeDescription
handlerfunctionThe handler function to call

Returns:

TypeDescription
self

SetOnExit

lua
function FSMState:SetOnExit(handler)
  -> self

Set the OnExit handler. This function is called upon existing the state.

Parameters:

NameTypeDescription
handlerfunctionThe handler function to call

Returns:

TypeDescription
self

Functions

New

lua
function FSMState.New(name)
  -> FSMState

Create a new FSM state.

Parameters:

NameTypeDescription
namestringThe name of the state

Returns:

TypeDescription
FSMState