From 87b82750fff29ff4ebcf3809ebbbf637d691ad1a Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Mon, 19 Aug 2013 14:10:00 +0200 Subject: [PATCH] Localize function registry to Gamestate.switch(). --- gamestate.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/gamestate.lua b/gamestate.lua index 6778c7b..e2e8c94 100644 --- a/gamestate.lua +++ b/gamestate.lua @@ -60,10 +60,6 @@ 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', @@ -71,17 +67,20 @@ local all_callbacks = { } 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})