I made a statemachine out of boredom
local traffic_light = StateMachine {
green = State {
on_enter = function()
print("BEGAN")
end,
on_flash = function(_,_,state_machine)
print("GREEN")
state_machine:set_state("yellow")
end
},
yellow = State {
on_flash = function(_,_,state_machine)
print("YELLOW")
state_machine:set_state("red")
end
},
red = State {
on_flash = function(_,_,state_machine)
print("RED")
state_machine:set_state("green")
end,
on_exit = function()
print("DONE")
end
},
}
traffic_light:set_state("green")
traffic_light:flash()
traffic_light:flash()
traffic_light:flash()
traffic_light:flash()
It outputs
BEGAN
GREEN
YELLOW
RED
DONE
BEGAN
GREEN
I don't think there's much point in sharing the source code since it's a very basic state machine with an attempt at a somewhat clean api (Also I'm 99% sure it's not very cleanly wrote, I might have to rewrite it to be more readable)
(Sorry mods)