A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeFAQRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
m27frogy
 
slayer9365
 
myrco919
 
branefreez
 
ninga95
 
CloneTrooper787
 
raboy117
 
Top posting users this month
Latest topics
» Send me an Email
January 8th 2016, 10:14 am by naknak

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
July 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
July 6th 2015, 4:16 pm by m27frogy

» Ambassador!
April 15th 2015, 11:40 pm by naknak

» Boop - Tag
April 13th 2015, 9:46 pm by naknak

» Vip Class Script
April 13th 2015, 4:54 pm by naknak

» Who's active?!
April 13th 2015, 4:52 pm by naknak

» Where to go from here.
April 13th 2015, 4:51 pm by naknak

» Genesis Point
July 17th 2014, 7:04 pm by branefreez

» Reward System
July 17th 2014, 5:41 am by m27frogy

» Script Request
July 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 here
April 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
January 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
January 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 Coding
October 26th 2013, 1:12 pm by pook03

» Simple Button
September 1st 2013, 6:19 pm by branefreez


Share | 
 

 Metatables Tutorial

View previous topic View next topic Go down 
AuthorMessage
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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
Back to top Go down
http://hackthissite.org
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Metatables Tutorial   February 19th 2011, 12:34 am

18 views and no replies? D:
Back to top Go down
http://hackthissite.org
naknak
Administrator
Administrator
avatar

Posts : 878
Join date : 2010-07-30

PostSubject: Re: Metatables Tutorial   February 19th 2011, 12:53 am

People will not speak if nothing is to be said.
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Metatables Tutorial   February 19th 2011, 2:30 am

actually, im looking for a rank up.
Back to top Go down
http://hackthissite.org
naknak
Administrator
Administrator
avatar

Posts : 878
Join date : 2010-07-30

PostSubject: Re: Metatables Tutorial   February 19th 2011, 11:58 am

Ah. Well, I'll let another person be the judge of that.
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Metatables Tutorial   March 19th 2011, 6:37 am

great tutorial so far!

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
m27frogy
The Garbageman
avatar

Posts : 336
Join date : 2011-06-23

PostSubject: Re: Metatables Tutorial   June 24th 2011, 2:32 pm

Not bad.
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Metatables Tutorial   June 24th 2011, 9:53 pm

this tutorial is done
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Metatables Tutorial   March 11th 2012, 12:43 am

yay!

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Sponsored content




PostSubject: Re: Metatables Tutorial   

Back to top Go down
 
Metatables Tutorial
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Great Tank and Weathering tutorial
» Power Sword Tutorial (with more to follow)
» Instant Mold Tutorial
» good c++ tutorial series
» After Effects Tutorial - Organic Cell Edges

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Expert-
Jump to: