View Single Post
06-11-15, 04:01 PM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by Mayron View Post
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:
  1. local parent[mode] = {}
  2. -- do stuff
  3. modules[mod] = setmetatable({}, {
  4.     __index = function(_, key)             
  5.         if (type(parent[mod][key]) == "function") then                 
  6.             -- do stuff
  7.         end
  8.         return parent[mod][key];
  9.     end,
  10.     __newindex = function(_, key, value)
  11.         if (type(value) == "function") then
  12.             -- do stuff
  13.         else
  14.             parent[mod][key] = value;
  15.         end
  16.     end,
  17. })
I think this is what you need then:

http://www.lua.org/pil/13.4.4.html
  Reply With Quote