Add State Machine capability to your Lua objects
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Add State Machine capability to your Lua objects

This module can add State Machine capability to any of your objects. It can be used either as a mixin class or by "monkey-patching" your object. It was designed to work with lua-objects and has also been integrated in dmc-objects as a mixin.


  • state methods

    getState(), setState(), gotoState()

    getPreviousState(), gotoPreviousState()

    pushStateStack(), popStateStack()

    resetStates(), setDebug()


Mixin Class

This module can be used as a mixin project dmc-objects contains the ObjectBase sub-class which shows how to use this module as a mixin with multiple inheritance.

Here it is in a nutshell:

-- import the events mixin module (adjust path for your project)
local StatesMixModule = require 'dmc_lua.lua_states_mix'

-- create ref to mixin (optional)
local StatesMix = StatesMixModule.StatesMix

-- do multiple inheritance !
NetStream = newClass( { ObjectBase, StatesMix } )

-- Then call init method in your OO Framework construction phase
ObjectBase.__init__( self, ... )
StatesMix.__init__( self, ... )

-- When destroying, you can call __undoInit__
ObjectBase.__undoInit__( self )
StatesMix.__undoInit__( self )

Monkey Patching

--== Import module

local StatesMixModule = require 'dmc_lua.lua_states_mix'

--== Setup ref to mixin (optional)
local StatesMix = StatesMixModule.StatesMix

--== Patch an object ==--

-- create one for yourself (eg, with OOP library)

local obj = {}  -- empty or create one from your OOP library
obj = StatesMix.patch( obj ) -- returns object

-- or have patch() create one for you

local obj = StatesMix.patch()  -- returns a new object