在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kyleconroy/lua-state-machine开源软件地址(OpenSource Url):https://github.com/kyleconroy/lua-state-machine开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):Lua Finite State MachineThis standalone lua module provides a finite state machine for your pleasure. Based heavily on Jake Gordon's javascript-state-machine. DownloadYou can download statemachine.lua. Alternatively:
UsageIn its simplest form, create a standalone state machine using: local machine = require('statemachine')
local fsm = machine.create({
initial = 'green',
events = {
{ name = 'warn', from = 'green', to = 'yellow' },
{ name = 'panic', from = 'yellow', to = 'red' },
{ name = 'calm', from = 'red', to = 'yellow' },
{ name = 'clear', from = 'yellow', to = 'green' }
}}) ... will create an object with a method for each event:
along with the following members:
Multiple 'from' and 'to' states for a single eventIf an event is allowed from multiple states, and always transitions to the same
state, then simply provide an array of states in the local machine = require('statemachine')
local fsm = machine.create({
initial = 'hungry',
events = {
{ name = 'eat', from = 'hungry', to = 'satisfied' },
{ name = 'eat', from = 'satisfied', to = 'full' },
{ name = 'eat', from = 'full', to = 'sick' },
{ name = 'rest', from = {'hungry', 'satisfied', 'full', 'sick'}, to = 'hungry' },
}}) This example will create an object with 2 event methods:
The
Callbacks4 callbacks are available if your state machine has methods using the following naming conventions:
You can affect the event in 3 ways:
For convenience, the 2 most useful callbacks can be shortened:
In addition, a generic All callbacks will be passed the same arguments:
Callbacks can be specified when the state machine is first created: local machine = require('statemachine')
local fsm = machine.create({
initial = 'green',
events = {
{ name = 'warn', from = 'green', to = 'yellow' },
{ name = 'panic', from = 'yellow', to = 'red' },
{ name = 'calm', from = 'red', to = 'yellow' },
{ name = 'clear', from = 'yellow', to = 'green' }
},
callbacks = {
onpanic = function(self, event, from, to, msg) print('panic! ' .. msg) end,
onclear = function(self, event, from, to, msg) print('thanks to ' .. msg) end,
ongreen = function(self, event, from, to) print('green light') end,
onyellow = function(self, event, from, to) print('yellow light') end,
onred = function(self, event, from, to) print('red light') end,
}
})
fsm:warn()
fsm:panic('killer bees')
fsm:calm()
fsm:clear('sedatives in the honey pots')
... Additionally, they can be added and removed from the state machine at any time: fsm.ongreen = nil
fsm.onyellow = nil
fsm.onred = nil
fsm.onstatechange = function(self, event, from, to) print(to) end or function fsm:onstatechange(event, from, to) print(to) end Asynchronous State TransitionsSometimes, you need to execute some asynchronous code during a state transition and ensure the new state is not entered until your code has completed. A good example of this is when you transition out of a You can now return If another event is triggered during a state machine transition, the event will be triggered relative to the
state the machine was transitioning to or from. Any calls to During a state change, Example of asynchronous transitions: local machine = require('statemachine')
local manager = require('SceneManager')
local fsm = machine.create({
initial = 'menu',
events = {
{ name = 'play', from = 'menu', to = 'game' },
{ name = 'quit', from = 'game', to = 'menu' }
},
callbacks = {
onentermenu = function() manager.switch('menu') end,
onentergame = function() manager.switch('game') end,
onleavemenu = function(fsm, name, from, to)
manager.fade('fast', function()
fsm:transition(name)
end)
return fsm.ASYNC -- tell machine to defer next state until we call transition (in fadeOut callback above)
end,
onleavegame = function(fsm, name, from, to)
manager.slide('slow', function()
fsm:transition(name)
end)
return fsm.ASYNC -- tell machine to defer next state until we call transition (in slideDown callback above)
end,
}
}) If you decide to cancel the async event, you can call Initialization OptionsHow the state machine should initialize can depend on your application requirements, so the library provides a number of simple options. By default, if you dont specify any initial state, the state machine will be in the local machine = require('statemachine')
local fsm = machine.create({
events = {
{ name = 'startup', from = 'none', to = 'green' },
{ name = 'panic', from = 'green', to = 'red' },
{ name = 'calm', from = 'red', to = 'green' },
}})
print(fsm.current) -- "none"
fsm:startup()
print(fsm.current) -- "green" If you specify the name of your initial event (as in all the earlier examples), then an
implicit local machine = require('statemachine')
local fsm = machine.create({
inital = 'green',
events = {
{ name = 'panic', from = 'green', to = 'red' },
{ name = 'calm', from = 'red', to = 'green' },
}})
print(fsm.current) -- "green" |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论