An open API service indexing awesome lists of open source software.

https://github.com/dmccuskey/lua-states-mixin

Add State Machine capability to your Lua objects
https://github.com/dmccuskey/lua-states-mixin

Last synced: over 1 year ago
JSON representation

Add State Machine capability to your Lua objects

Awesome Lists containing this project

README

          

## lua-states-mixin ##

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](https://github.com/dmccuskey/lua-objects) and has also been integrated in [dmc-objects](https://github.com/dmccuskey/dmc-objects) as a mixin.

### Features ###

* state methods

getState(), setState(), gotoState()

getPreviousState(), gotoPreviousState()

pushStateStack(), popStateStack()

resetStates(), setDebug()

### Examples ###

#### Mixin Class ####

This module can be used as a mixin project [dmc-objects](https://github.com/dmccuskey/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:

```lua
-- 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 ####

```lua
--== 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

```