louiskk
|
  |
| Joined: 31 Dec 2008 |
| Total Posts: 228 |
|
|
| 04 Jan 2015 04:22 AM |
Hi,
I'm currently trying to learn about metatables and metamethods. I've been following Anaminus' tutorial on it, but I can't get it to work.
tab = {t1 = 2, t7 = 32}
mt = {m1 = 22, m2 = 32, __index = function() print("Stuff") end}
setmetatable(tab, mt)
wait()
print(tab['t1'])
--print(mt['m1'])
I've tried changing the index like this with no success.
mt.__index = function() print("Stuff") end
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 04 Jan 2015 04:24 AM |
The __index metamethod is only called IF AND ONLY IF the key does not exist.
print(tab["t2"]) would fire that, you would see Stuff then nil printed. What's the point of the wait? :) |
|
|
| Report Abuse |
|
|
louiskk
|
  |
| Joined: 31 Dec 2008 |
| Total Posts: 228 |
|
|
| 04 Jan 2015 04:27 AM |
| Oh, thanks. I just added the wait as a test. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 04 Jan 2015 04:30 AM |
Also, same with __newindex. It is only fired when you do: tbl[key] = value and tbl[key] is nil |
|
|
| Report Abuse |
|
|