在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):unindented/lua-fsm开源软件地址(OpenSource Url):https://github.com/unindented/lua-fsm开源编程语言(OpenSource Language):Lua 99.4%开源软件介绍(OpenSource Introduction):lua-fsmA simple finite-state machine implementation for Lua. Based on @jakesgordon's InstallationYou can install through LuaRocks:
Or just download manually:
UsageInitializationYou can create a new state machine by doing something like: local fsm = require "fsm"
local alert = fsm.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" }
}
}) This will create an object with a method for each event:
along with the following:
If you don't specify any initial state, the state machine will be in the local alert = fsm.create({
events = {
{name = "startup", from = "none", to = "green"},
{name = "panic", from = "green", to = "red" },
{name = "calm", from = "red", to = "green"}
}
})
print(alert.current) -- "none"
alert.startup()
print(alert.current) -- "green" If you specify the name of your initial state, then an implicit local alert = fsm.create({
initial = "green",
events = {
{name = "panic", from = "green", to = "red" },
{name = "calm", from = "red", to = "green"}
}
})
print(alert.current) -- "green" If your object already has a local alert = fsm.create({
initial = {state = "green", event = "init"},
events = {
{name = "panic", from = "green", to = "red" },
{name = "calm", from = "red", to = "green"}
}
})
print(alert.current) -- "green" Finally, if you want to wait to call the initial state transition event until a later date you can defer it: local alert = fsm.create({
initial = {state = "green", event = "init", defer = true},
events = {
{name = "panic", from = "green", to = "red" },
{name = "calm", from = "red", to = "green"}
}
})
print(alert.current) -- "none"
alert.init()
print(alert.current) -- "green" CallbacksFour types of callback are available by attaching methods to your state machine, using the following naming conventions (where
For convenience, the 2 most useful callbacks can be shortened:
In addition, four general-purpose callbacks can be used to capture all event and state changes:
All callbacks will be passed the same arguments:
Callbacks can be specified when the state machine is first created: local fsm = require "fsm"
local alert = fsm.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 = {
on_panic = function(self, event, from, to, msg) print('panic! ' .. msg) end,
on_clear = function(self, event, from, to, msg) print('phew... ' .. msg) end
}
})
alert.warn()
alert.panic('killer bees')
alert.calm()
alert.clear('they are gone now') The order in which callbacks occur is as follows, assuming event
Deferred state transitionsYou may need to execute additional code during a state transition, and ensure the new state is not entered until your code has completed, e.g. fading a menu screen. One way to do this is to return local screens = fsm.create({
initial = "menu",
events = {
{name = "play", from = "menu", to = "game"},
{name = "quit", from = "game", to = "menu"}
},
callbacks = {
on_leave_menu = function (self)
fade_out(0.5, self.confirm)
return fsm.DEFERRED
end
}
})
screens.play() Meta
Contributors
LicenseCopyright (c) 2016 Daniel Perez Alvarez (unindented.org). This is free software, and may be redistributed under the terms specified in the LICENSE file. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论