Originally Posted by Mayron
It's hard explaining without showing the real code but doesn't the __index method only trigger if it cannot find the value in the normal table and then checks another table (the parent one like inheritance)?
Because basically I want the __index function to ALWAYS trigger so to do this I use __newindex to add entries to the parent table rather than the normal table so every time a value is indexed using the normal table, it has to use the __index function which returns the value from the parent table, as well as perform other stuff.
Here's an abbreviated example of the real code I am working on:
Lua Code:
local parent[mode] = {} -- do stuff modules[mod] = setmetatable({}, { __index = function(_, key) if (type(parent[mod][key]) == "function") then -- do stuff end return parent[mod][key]; end, __newindex = function(_, key, value) if (type(value) == "function") then -- do stuff else parent[mod][key] = value; end end, })
|
I think this is what you need then:
http://www.lua.org/pil/13.4.4.html