Right now I have this code that detects changes in a table, however, I need to know the key’s exact location, how can I do that?
local function Proxify(currentTable: {any})
local proxy = newproxy(true)
local meta = getmetatable(proxy)
meta.__index = function(self, key)
local idx = currentTable[key]
if idx and type(idx) == "table" then
idx = Proxify(idx)
end
return idx
end
meta.__newindex = function(self, key, value) -- THIS KEY
if currentTable[key] ~= value then
currentTable[key] = value
print("Change made in ", key, " = ", value)
end
end
return proxy
end
For example if I do this:
local data = Proxify({ store = { coins = 50 } })
data.store.coins = 100
I want to get "data.store.coins" as location of the key that changed.
Yes, but I am asking on how I can listen for changes in a table.
If I use newproxy, it will create userdata object which won’t work with table.insert.
If I don’t use newproxy, there will be still a problem since table.insert won’t trigger changes because it’s using rawset.
Comparing tables every frame would be my best option but that would be inefficient.