I have a problem where the metatable is not identified, if you look at the code below and test it you will notice that it will not print the value described in metaTable.__newindex.
local metaTable = {}
metaTable.__index = metaTable
function metaTable.__newindex(self, index, value)
print(index, value)
rawset(self, index, value)
end
local Table = {}
Table.Cat = setmetatable({
Health = 7,
MaxHealth = 9
}, metaTable)
Table.Cat.Health = 1
print(Table)
Well, I already knew about the .newindex and .index functions, but my error was occurring when trying to identify a change in values in the metatable. By the way, your code may cause a looping error, after all you defined the indexing function and then indexed a value, the same is repeated with new indexes.
I solved my problem by doing this:
local MetaTable = {}
function MetaTable.__index(self, index)
return self.__cache[index]
end
function MetaTable.__newindex(self, index, value)
rawset(self.__cache, index, value)
end
local MyTable = setmetatable({ __cache = {} }, MetaTable)