Last active
December 14, 2020 13:49
-
-
Save zach2good/3a2d6973be2454760f30eeea78121abf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Original code | |
tpz = {} | |
tpz.mob = {} | |
tpz.mob.onAttack = function(amount) | |
return amount | |
end | |
print('Original call: ', tpz.mob.onAttack(100)) | |
-- Prints 100 | |
-- 'Module' helper | |
module = {} | |
module.override = function(base_table, name, func) | |
local old = base_table[name] | |
local thisenv = getfenv(old) | |
local env = { super = old } | |
setmetatable(env, { __index = thisenv }) | |
setfenv(func, env) | |
base_table[name] = func | |
end | |
-- Module content | |
module.override(tpz.mob, "onAttack", function(amount) | |
return super(amount) * 1.5 | |
end) | |
-- Runtime call | |
print('Call after module applied: ', tpz.mob.onAttack(100)) | |
-- Prints 150 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment