Thread Tools Display Modes
08-08-13, 08:07 PM   #1
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
generic for and metatables.

I was wondering if a generic for ( in pairs ) can walk over values in a metatable from the parent table?
I can find nothing anywhere on if this can be done or if pairs can only walk the given table and not its metatable?
  Reply With Quote
08-08-13, 09:39 PM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Normally you know whats in your metatable. I'm sure if you post why and what you want to do someone comes up with a good and lengthy reply (Cant test it but a metatable is just a table like any other which stores functions for special events that happen to your table.)
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
08-08-13, 10:02 PM   #3
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
I suppose what TE is asking for is something like that:

Lua Code:
  1. -- create a namespace
  2. Window = {}
  3. -- create the prototype with default values
  4. Window.prototype = {x=0, y=0, width=100, height=100, }
  5. -- create a metatable
  6. Window.mt = {}
  7. -- declare the constructor function
  8. function Window.new (o)
  9.     setmetatable(o, Window.mt)
  10.     return o
  11. end
  12.  
  13. Window.mt.__index = Window.prototype
  14.  
  15. w = Window.new{x=10, y=20}
  16.  
  17. for k,v in pairs(w) do
  18.     print(k, v)
  19. end
(example taken from http://www.lua.org/pil/13.4.1.html)
where the output should be:
x 10
y 20
width 100
height 100

and not just the values for x and y.
  Reply With Quote
08-08-13, 10:15 PM   #4
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
If that is the case something like that should work:

Lua Code:
  1. Window.mt.__tostring = function(o)
  2.     local str = ""
  3.     for k in pairs(Window.prototype) do
  4.         str = str .. k ..  " " .. o[k] .. "\n"
  5.     end
  6.     return str
  7. end

Then a simple print(w) should do it. Note that string concatenation is not efficient and you should probably use string.format instead. Also the order returned is random.
  Reply With Quote
08-09-13, 01:55 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Billtopia View Post
I was wondering if a generic for ( in pairs ) can walk over values in a metatable from the parent table?
I can find nothing anywhere on if this can be done or if pairs can only walk the given table and not its metatable?
Consider:

Code:
local f = CreateFrame("Frame")
for k, v in pairs(f) do
    print(k, "is a", type(v))
end
From that, you will only get one print: "0 is a userdata"

Code:
local f = CreateFrame("Frame")
local mt = getmetatable(f)
for k, v in pairs(mt) do
    print(k, "is a", type(v))
end
That will get you "__index is a table".

Code:
local f = CreateFrame("Frame")
local mt = getmetatable(f)
local index = mt.__index
for k, v in pairs(index) do
    print(k, "is a", type(v))
end
Now you'll finally get all the Frame object methods, eg. "IsMovable is a function".

The same principles apply to any table and its metatable. If you want to look at the metatable, you have to use getmetatable and look at it directly.

If you want a real suggestion for solving your actual problem, you'll have to actually describe what that problem is. I don't know why half the questions people post lack any context whatsoever... it just makes it impossible to answer without resorting to guesses and tangents.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-09-13, 07:20 AM   #6
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
So, I have to look at a metatable directly or not at all... no fun there but it does make sense, I guess I will just load the default values into a saved variable on the first time the addon loads.

Thanks for the help.
  Reply With Quote
08-09-13, 08:41 AM   #7
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
You traverse the keys of the metatable (your defaults) but look for the corresponding key-value pair in the table the metatable is attached to. This will output either the value from the table or, if not present, will trigger the __index metamethod and do the look-up in the metatable instead.
  Reply With Quote
08-09-13, 09:06 AM   #8
Billtopia
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 110
I know how to indirectly access the values in the metatable when the keys are known as for basic addon settings where I make the list but the addon is being designed so that the user may change the key, values on the fly so that they are able to add/remove/change things from the table... which also made me think that a metatable would not do what I want either, as if they delete a default command the metatable would restore it unless it was also removed from the metatable. I guess I should have stuck to using the metatable for static known to me settings lol
It seems easier in coding, and in memory usage to just construct the table on the addon's first run as the settings and what not are never truly known to me once the user messes with it.


-------------------------------------------------------------------------------
It is an addon that can take user supplied lua scripts and turn them into slash commands. The meta tables came into play in this case by me giving a few built in default commands such as "/eject #" "/helm" "/cloak" and what not...
The addon stores the slash commands and code in strings until the addonloaded event fires when it then converts them into the commands to make the slash commands.

I was not wanting to have to keep an index of what commands are in existance in the addon (or by the addon) and it seems a normal table better suits this than a table with a metatable... I guess I just got used to using metatables for settings and tried to put a square peg in a round hole by habit...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » generic for and metatables.

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off