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
- Host: GitHub
- URL: https://github.com/dmccuskey/lua-states-mixin
- Owner: dmccuskey
- License: mit
- Created: 2015-01-13T21:14:21.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-03-05T07:08:54.000Z (over 11 years ago)
- Last Synced: 2025-05-07T13:57:55.479Z (over 1 year ago)
- Language: Lua
- Size: 223 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```