Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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