miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 18 Mar 2012 10:11 PM |
local x = {} local metaTable = {} -- metaTables are tables, too! setmetatable(x, metaTable) -- Give x a metatable called metaTable! print(getmetatable(x))
Can someone tell me how this can be useful knowing the tables memory? How can this be important? |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:22 PM |
So you can do something like this:
local mt = getmetatable(ATable) mt.__index = function(t, k) return "hi" end
|
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 18 Mar 2012 10:23 PM |
| Can you explain to me what that would do? I would appreciate it. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:24 PM |
| Look up the metamethods. There's a page on the wiki that explains it well enough. |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 18 Mar 2012 10:27 PM |
| I did. I know __index fires when the metatable is nil. But I just don't know what it means when you use it. That's why I'm asking if you could explain what you just did. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:28 PM |
The __index metamethod fires when you try to index a key-value pair where the value is nil. All it does is run the function, passing on the table at the first parameter, and the key to the second parameter. If the __index metamethod is a table, it returns the value in said table located at the key you're indexing in the original table. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 18 Mar 2012 10:28 PM |
| That's not right... Basically if I make a metatable, and then make reference to an "index", it fires, but only if the index ALREADY exists. If it doesn't it fires __newindex. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:30 PM |
| __newindex is self explanatory. __newindex fires when you're trying to add a new key-value pair to the table. This only happens when the key is not already found in the table. If the key is found in the table, the __newindex metamethod does not run. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 18 Mar 2012 10:31 PM |
Fail. I worded that so horribly it sounds like I don't know what I'm talking about...
__index = Referencing the key __newindex = Setting the key |
|
|
| Report Abuse |
|
|
dave2011
|
  |
| Joined: 02 Oct 2010 |
| Total Posts: 10581 |
|
|
| 18 Mar 2012 10:35 PM |
| mind = blown, what is a metatable and why is everybody posting these ___________ symbols LOL |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 18 Mar 2012 10:35 PM |
So to ask...
local metatable = { __index = {x = 1} } local t = setmetatable({}, metatable) print(t.x)
This looks for a variable called x in metatable. It doesn't find it so it looks through __index and find that so it prints what's in index? |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 18 Mar 2012 10:38 PM |
| Yeah, that's like a pointless example though. It only really works well once you use a function instead of a table of indices :/ |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:42 PM |
"This looks for a variable called x in metatable."
No. The setmetatable function returns the original table.
local t = {} local mt = { __index = {x=1} } local t2 = setmetatable(t, mt)
print( t==t2 ) --> true
print( t.x ) --> 1
print( mt.x ) --> nil |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 18 Mar 2012 10:43 PM |
Yeah...>.>
Can you show me an example printing a string ina table using like this? |
|
|
| Report Abuse |
|
|
| |
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 18 Mar 2012 10:46 PM |
| Miz's example is right. Partly because it's copied from the wiki. But it's still right. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:49 PM |
| I didn't say miz's example was wrong. I just corrected his incorrect interpretation. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 18 Mar 2012 10:50 PM |
| Ehhhhhh. Shoulda corrected by saying it looks for x in __index of the metatable of table. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2012 10:51 PM |
But he said that. ^_^ I only corrected the first part. |
|
|
| Report Abuse |
|
|