1

I am looking to monitor changes made to one particular embedded table, in this case oranges, using the __newindex metamethod. My current code is able to detect the change, yet the value will obviously not update. I understand that the original data needs a proxy table in order to use the __newindex metamethod for detecting value changes.

local data = {cherries = {quantity = 0}, oranges = {quantity = 0}}
local _data = data
data.oranges = {}

local metaTable = {
    __newindex = function (_, key, value)
        print("Updating " .. tostring(key) .. " to " .. tostring(value))
        _data[key] = value
    end
}

setmetatable(data.oranges, metaTable)

data.oranges.quantity = 1 -- Invokes "Updating quantity to 1"
print(data.oranges.quantity) -- "nil" (I would like it to print 1)
  • 1
    Does this answer your question? [Is there a way to "listen" for changes in a Lua table?](https://stackoverflow.com/questions/57598440/is-there-a-way-to-listen-for-changes-in-a-lua-table) – Nifim Jul 28 '23 at 14:45

1 Answers1

1

You need to set an index metamethod:

local metaTable = {
    __newindex = function (_, key, value)
        print("Updating " .. tostring(key) .. " to " .. tostring(value))
        _data[key] = value
    end,
    __index = function (_, key, value)
        return data[key]
    end
}
lhf
  • 70,581
  • 9
  • 108
  • 149