Ok thanks, I found a way to do this, it may not look really professional, but at least it is working for me. It is modified from the examples in the Lua book 13.4.4, aimed to not make every table I have to have a metatable like this. I'm posting it here as it might be useful for some people here.
DetectChange = {}
DetectChange.mt = {
__index = function (t,k)
return _G[k]
end,
__newindex = function (t,k,v)
_G[k] = v
-- Whatever things you need to do when the value changes.
end,
}
setmetatable(DetectChange, DetectChange.mt)
-- Usage Example: DetectChange.test = 5
-- This will change the value of test variable to 5.
-- At the same time, it will trigger certain action that you have defined above.
-- Tested to work in layered tables, as long the global environment _G points to the right global table.
-- You need to change a variable value in this format, it does not work when you change the variable directly. |