View Single Post
05/15/14, 09:20 AM   #10
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Edda View Post
rawget(table, key) : I really don't see the point : example plz
Metatables magic
http://www.youtube.com/watch?v=CYxMfVy5W00

http://nova-fusion.com/2011/06/30/lu...bles-tutorial/

EDIT (example):
Lets set simple empty table called "t" with metatable "mt":
lua Code:
  1. local defaults = { a = "hello world" }
  2. local mt = { __index = defaults }
  3. local t = {}
  4.  
  5. setmetatable(t, mt)
Now if you try:
lua Code:
  1. d(t["a"])
It will print "hello world".
Key "a" is not present in table "t", but because we have defined metamethod __index in metatable, you will get value from "defaults" table.

If you try:
lua Code:
  1. d(rawget(t, a))
It will print "nil", because rawget returns the real value without invoking any metamethod.

Last edited by Garkin : 05/15/14 at 09:43 AM.
  Reply With Quote