Skip to content

Instantly share code, notes, and snippets.

@Castux
Forked from zach2good/module.lua
Last active December 14, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Castux/fb37b60ec853c690c8193c90b3510476 to your computer and use it in GitHub Desktop.
Save Castux/fb37b60ec853c690c8193c90b3510476 to your computer and use it in GitHub Desktop.
-- Original code
tpz = {}
tpz.testMethod = function(a, b, c)
print(a, b, c)
end
-- 'Module' helper
module = {}
module.override = function(t, name, func)
local old = t[name]
local thisenv = getfenv(old)
local env = { super = old }
setmetatable(env, { __index = thisenv })
setfenv(func, env)
t[name] = func
end
-- Module content
module.override(tpz, 'testMethod',
function(a, b, c)
super(a,b,c)
print(a + 1, b + 1, c + 1)
end)
module.override(tpz, 'testMethod',
function(a,b,c)
print "play it again, sam"
super(a,b,c)
end)
-- Game runtime
tpz.testMethod(1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment