Skip to content

Commit

Permalink
Merge pull request #1 from TannerRogalsky/master
Browse files Browse the repository at this point in the history
Allow gotoState to pass arguments to the new state.
  • Loading branch information
kikito committed Mar 1, 2013
2 parents 538de9b + 37ffe03 commit ada968b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 11 additions & 0 deletions spec/unit.lua
Expand Up @@ -104,6 +104,17 @@ describe("Unit tests", function()
assert_true(e.mark)
end)

it("passes all additional arguments to enteredState and exitedState", function()
local FooEnemy = Enemy:addState("FooEnemy")
local testValue = "Bar"

local validateVarargs = function(self, ...) assert_equal(..., testValue) end
FooEnemy.enteredState = validateVarargs
FooEnemy.exitedState = validateVarargs

local e = Enemy:new()
e:gotoState("FooEnemy", testValue)
end)
end)

it("raises an error when given an invalid id", function()
Expand Down
10 changes: 5 additions & 5 deletions stateful.lua
Expand Up @@ -89,8 +89,8 @@ local function _assertExistingState(self, state, stateName)
assert(state, "The state " .. stateName .. " was not found in " .. tostring(self.class) )
end

local function _invokeCallback(self, state, callbackName)
if state then state[callbackName](self) end
local function _invokeCallback(self, state, callbackName, ...)
if state then state[callbackName](self, ...) end
end

local function _getCurrentState(self)
Expand Down Expand Up @@ -133,9 +133,9 @@ function Stateful.static:addState(stateName, superState)
return self.static.states[stateName]
end

function Stateful:gotoState(stateName)
function Stateful:gotoState(stateName, ...)

_invokeCallback(self, _getCurrentState(self), 'exitedState')
_invokeCallback(self, _getCurrentState(self), 'exitedState', ...)

if stateName == nil then
self.__stateStack = { }
Expand All @@ -144,7 +144,7 @@ function Stateful:gotoState(stateName)

local newState = _getStateFromClassByName(self, stateName)
self.__stateStack = { newState }
_invokeCallback(self, newState, 'enteredState')
_invokeCallback(self, newState, 'enteredState', ...)
end

end
Expand Down

0 comments on commit ada968b

Please sign in to comment.