Skip to content

Commit

Permalink
Localize function registry to Gamestate.switch().
Browse files Browse the repository at this point in the history
  • Loading branch information
vrld committed Aug 19, 2013
1 parent b3c329a commit 87b8275
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions gamestate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,27 @@ function GS.current()
return stack[#stack]
end

-- holds all defined love callbacks after GS.registerEvents is called
-- returns empty function on undefined callback
local registry = setmetatable({}, {__index = function() return __NULL__ end})

local all_callbacks = {
'update', 'draw', 'focus', 'keypressed', 'keyreleased',
'mousepressed', 'mousereleased', 'joystickpressed',
'joystickreleased', 'quit'
}

function GS.registerEvents(callbacks)
local registry = {}
callbacks = callbacks or all_callbacks
for _, f in ipairs(callbacks) do
registry[f] = love[f]
love[f] = function(...) return GS[f](...) end
registry[f] = love[f] or __NULL__
love[f] = function(...)
registry[f](...)
return GS[f](...)
end
end
end

-- forward any undefined functions
setmetatable(GS, {__index = function(_, func)
return function(...)
registry[func](...)
return (stack[#stack][func] or __NULL__)(stack[#stack], ...)
end
end})
Expand Down

0 comments on commit 87b8275

Please sign in to comment.