Skip to content

Instantly share code, notes, and snippets.

@Nimblz
Last active September 19, 2019 17:24
Show Gist options
  • Save Nimblz/3c854e18318c6db2ce516c071c725d4a to your computer and use it in GitHub Desktop.
Save Nimblz/3c854e18318c6db2ce516c071c725d4a to your computer and use it in GitHub Desktop.
-- trying to make an ergonomic state machine
-- maybe im just making madness
local FSM = {}
function FSM.new(states,startState)
local self = setmetatable({},{__index=FSM})
self.states = states
self:transition(startState)
return self
end
function FSM:transition(newState, ...)
if not newState then return end
local targetState = self.states[newState]
self.currentState = targetState
return self:transition(targetState.enter(...))
end
function FSM:step(...)
if not self.currentState.step then return end
if not typeof(self.currentState.step) == "function" then return end
self:transition(self.currentState.step(...))
end
return FSM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment