Skip to content

Instantly share code, notes, and snippets.

@bladecoding
Created June 1, 2019 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bladecoding/e587fd13a94ebb8b0be7e823207e283b to your computer and use it in GitHub Desktop.
Save bladecoding/e587fd13a94ebb8b0be7e823207e283b to your computer and use it in GitHub Desktop.
function ProxyObj(obj)
return {
set = function(k, val)
obj[k] = val
end,
get = function(k)
local v = obj[k]
if type(v) == 'table' and not rawget(v, '__cfx_functionReference') then
return ProxyObj(v)
else
return v
end
end
}
end
function WrapProxy(obj)
return setmetatable({}, {
__index = function(t, k)
local v = obj.get(k)
--Don't proxy function references
if type(v) == 'table' and not rawget(v, '__cfx_functionReference') then
return WrapProxy(v)
else
return v
end
end,
__newindex = function(t, k, v)
obj.set(k, v)
end
})
end
someObj = {
var1 = {
subVar1 = 123
}
}
exports("someObj", function()
return ProxyObj(someObj)
end)
someObj2 = WrapProxy(exports.random:someObj())
--Initial value
print(someObj2.var1.subVar1)
--Modify the original object
someObj.var1.subVar1 = 142
print(someObj2.var1.subVar1)
--Changes to the proxy object propagate to the original object
someObj2.var1.subVar1 = 152
print(someObj.var1.subVar1)
--Adding a function reference to the original object
someObj.var1.func = function() return "Test" end
print(someObj2.var1.func())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment