Top posting users this month | |
Latest topics | » Where to go from here.September 14th 2020, 1:20 pm by MrNicNac» Send me an EmailSeptember 14th 2020, 1:16 pm by MrNicNac» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]July 6th 2015, 7:38 pm by m27frogy» New Site PossiblyJuly 6th 2015, 4:16 pm by m27frogy» Ambassador!April 15th 2015, 11:40 pm by naknak» Boop - TagApril 13th 2015, 9:46 pm by naknak» Vip Class ScriptApril 13th 2015, 4:54 pm by naknak» Who's active?!April 13th 2015, 4:52 pm by naknak» Genesis PointJuly 17th 2014, 7:04 pm by branefreez» Reward SystemJuly 17th 2014, 5:41 am by m27frogy» Script RequestJuly 10th 2014, 11:43 am by naknak» local scripts?July 10th 2014, 11:39 am by naknak» Project: Reconstruction [Died]July 10th 2014, 11:36 am by naknak» Hi. I am new hereApril 26th 2014, 4:01 pm by altshiftkey» What's your favorite sport?January 1st 2014, 2:13 pm by m27frogy» FlashLight ScriptJanuary 1st 2014, 2:11 pm by m27frogy» Gun Making! [READ DESC]January 1st 2014, 2:10 pm by m27frogy» Hi, I am new here!November 26th 2013, 3:33 pm by Keanu73» Improve CodingOctober 26th 2013, 1:12 pm by pook03» Simple ButtonSeptember 1st 2013, 6:19 pm by branefreez |
|
| Metatables Tutorial | |
| | Author | Message |
---|
Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Metatables Tutorial February 12th 2011, 1:15 am | |
| Although, this tutorial will assume that you have a fair amount of experience with the tables and functions of Lua, I will explain everything to the letter. Metatables allow us to change the behavior of a table. When Lua tries to do an unexpected function with tables, Lua tries to find a certain field. If Lua finds this field, it calls the corresponding value (the so-called metamethod, which should be a function) to compute. Each table in Lua may have its own metatable. Any table can be the metatable of any other table, a group of related tables may share a common metatable (which describes their common behavior), and a table can be its own metatable (so that it describes its own individual behavior). Any configuration is valid. We can use the setmetatable function to set or change the metatable of any table. Do not mistake this function as a metamethod. __index is the only metamethod in this example: - Code:
-
local tab = {}
setmetatable(tab, { __index = function(t, k) return("attempt to access " .. tostring(k) .. " in " .. tostring(t)) end } )
print(tab.test) What happened here? First, we assigned a table to a local variable, tab (to enhance speed an prevent clutter). Then we used the setmetatable function to set an unsigned table, including the variable __index, to the table tab as the metatable. The __index function would get table and key you were trying to access. This is seen in our script as the local variables t and k. We ended our calls, of course. __index will be fired when we try to access an unexisting key in a table. Finally, when we tried to print a an unexisting key, test, in table tab, Lua finds the __index field and calls its function. Here is our output: Output: We will now use the __newindex metamethod. - Code:
-
local tab = {}
setmetatable(tab, { __newindex = function(t, k, v) local a = t.k a = v a = nil print("assigned " .. tostring(v) .. " in " .. tostring(k) .. " to " .. tostring(t)) end } )
tab.test = true __newindex is fired when we try to add a new index to our table. And our local variables t, k, and v represent our table, key, and value. So, our output will be: Output: You might have thought this would work too: - Code:
-
local tab = {}
setmetatable(tab, { __newindex = function(t, k, v) t.k = v print("assigned " .. tostring(v) .. " in " .. tostring(k) .. " to " .. tostring(t)) end } )
tab.test = true But that would only output to this: Output: There is a stack overflow because Lua has to find the table, key, and value, all at once! So, assigning nil to our variable a is no problem at all. Variables have no fixed relationship to tables whatsoever. Our table would be passed on to the local variable v. We could also apply this concept to read-only tables: - Code:
-
local tab = {}
setmetatable(tab, { __index = function(t) return("attempt to access read-only " .. tostring(t)) end,
__newindex = function(t) return("attempt to access read-only " .. tostring(t)) end } )
print(tab.test) tab.test = true Output: Also, proxies: - Code:
-
local tab = {} local pro = {}
setmetatable(pro, { __index = function(t, k) if tab.k == nil then return("attempt to access " .. tostring(k) .. " in " .. tostring(tab)) end end,
__newindex = function(t, k, v) local a = tab.k a = v a = nil print("assigned " .. tostring(v) .. " in " .. tostring(k) .. " to " .. tostring(tab)) end } )
print(pro.test) pro.test = true
Output: Another function of metatables is the getmetatable function. This function will get the metatable of a table. - Code:
-
local tab = {} local met = {} print(getmetatable(setmetatable(tab, met))) Output: The __metatable metamethod is simple. Suppose you want to protect your sets, so that users can neither see nor change their metatables. If you set a __metatable field in the metatable, getmetatable will return the value of this field, whereas setmetatable will raise an error. - Code:
-
tab = {} setmetatable(tab, {__metatable = "access denied"}) print(getmetatable(tab)) setmetatable(tab, {})
Output: What the error, stdin:1, means is that there is an error raised at level 1. Easy, huh? I hope this tutorial helped you. Metatables will unleash many creative ways to write code. Goodbye and good luck scripting!
Last edited by Supernapalm on June 24th 2011, 9:53 pm; edited 2 times in total | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Metatables Tutorial February 19th 2011, 12:34 am | |
| 18 views and no replies? D: | |
| | | naknak Administrator
Posts : 878 Join date : 2010-07-30
| Subject: Re: Metatables Tutorial February 19th 2011, 12:53 am | |
| People will not speak if nothing is to be said. | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Metatables Tutorial February 19th 2011, 2:30 am | |
| actually, im looking for a rank up. | |
| | | naknak Administrator
Posts : 878 Join date : 2010-07-30
| Subject: Re: Metatables Tutorial February 19th 2011, 11:58 am | |
| Ah. Well, I'll let another person be the judge of that. | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Metatables Tutorial March 19th 2011, 6:37 am | |
| | |
| | | m27frogy The Garbageman
Posts : 336 Join date : 2011-06-23
| Subject: Re: Metatables Tutorial June 24th 2011, 2:32 pm | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Metatables Tutorial June 24th 2011, 9:53 pm | |
| | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Metatables Tutorial March 11th 2012, 12:43 am | |
| | |
| | | Sponsored content
| Subject: Re: Metatables Tutorial | |
| |
| | | | Metatables Tutorial | |
|
Similar topics | |
|
| Permissions in this forum: | You cannot reply to topics in this forum
| |
| |
| Who is online? | In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest
None
Most users ever online was 136 on October 21st 2024, 12:16 pm
|
| Please tell us what time zone you are in here
Got a really good idea? Post it in projects and it might show up here! |
|