A Roblox Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeLatest imagesRegisterLog 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
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
naknak
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
Supernapalm
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
m27frogy
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
slayer9365
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
myrco919
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
branefreez
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
ninga95
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
CloneTrooper787
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
raboy117
Metatables Tutorial Vote_lcapMetatables Tutorial Voting_barMetatables Tutorial Vote_rcap 
Top posting users this month
No user
Latest topics
» Where to go from here.
Metatables Tutorial EmptySeptember 14th 2020, 1:20 pm by MrNicNac

» Send me an Email
Metatables Tutorial EmptySeptember 14th 2020, 1:16 pm by MrNicNac

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

» New Site Possibly
Metatables Tutorial EmptyJuly 6th 2015, 4:16 pm by m27frogy

» Ambassador!
Metatables Tutorial EmptyApril 15th 2015, 11:40 pm by naknak

» Boop - Tag
Metatables Tutorial EmptyApril 13th 2015, 9:46 pm by naknak

» Vip Class Script
Metatables Tutorial EmptyApril 13th 2015, 4:54 pm by naknak

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

» Genesis Point
Metatables Tutorial EmptyJuly 17th 2014, 7:04 pm by branefreez

» Reward System
Metatables Tutorial EmptyJuly 17th 2014, 5:41 am by m27frogy

» Script Request
Metatables Tutorial EmptyJuly 10th 2014, 11:43 am by naknak

» local scripts?
Metatables Tutorial EmptyJuly 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
Metatables Tutorial EmptyJuly 10th 2014, 11:36 am by naknak

» Hi. I am new here
Metatables Tutorial EmptyApril 26th 2014, 4:01 pm by altshiftkey

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

» FlashLight Script
Metatables Tutorial EmptyJanuary 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
Metatables Tutorial EmptyJanuary 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
Metatables Tutorial EmptyNovember 26th 2013, 3:33 pm by Keanu73

» Improve Coding
Metatables Tutorial EmptyOctober 26th 2013, 1:12 pm by pook03

» Simple Button
Metatables Tutorial EmptySeptember 1st 2013, 6:19 pm by branefreez


 

 Metatables Tutorial

Go down 
4 posters
AuthorMessage
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Metatables Tutorial Empty
PostSubject: Metatables Tutorial   Metatables Tutorial EmptyFebruary 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
avatar


Posts : 393
Join date : 2011-01-17

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

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


Posts : 878
Join date : 2010-07-30

Metatables Tutorial Empty
PostSubject: Re: Metatables Tutorial   Metatables Tutorial EmptyFebruary 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
avatar


Posts : 393
Join date : 2011-01-17

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

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


Posts : 878
Join date : 2010-07-30

Metatables Tutorial Empty
PostSubject: Re: Metatables Tutorial   Metatables Tutorial EmptyFebruary 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
blueymaddog


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

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

great tutorial so far!
Back to top Go down
m27frogy
The Garbageman
m27frogy


Posts : 336
Join date : 2011-06-23

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

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


Posts : 393
Join date : 2011-01-17

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

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


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

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

yay!
Back to top Go down
Sponsored content





Metatables Tutorial Empty
PostSubject: Re: Metatables Tutorial   Metatables Tutorial Empty

Back to top Go down
 
Metatables Tutorial
Back to top 
Page 1 of 1
 Similar topics
-
» Metatables Tutorial
» My first tutorial ~ Metatables
» I understand metatables, but..
» Instance.new() tutorial
» Scripting Tutorial Forum

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