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 | 
 

 My first tutorial ~ Metatables

View previous topic View next topic Go down 
AuthorMessage
Guest
Guest



PostSubject: My first tutorial ~ Metatables   January 22nd 2011, 11:38 pm

This tutorial is about metatables. Since it's in the Expert tutorials section, I expect you to understand tables, functions, and quite a bit more, since metatables are advanced stuff.

The first thing I will do is explain what exactly a metatable is.

Metatables are simply tables, but they affect how another table acts when certain things are done with it.

Metatables can tell a table what to do when you try to call it like a function, when you try to add a number/userdata/table to it, when you try to find something in a metatable that isn't there, and much more.

To make a metatable, you start by making a table:

Code:

Metatab = { }

You also need a table or a userdata to assign the metatable to. Please note that the metatable of all Roblox-created userdatas are locked, so you can't access the metatable of Instances, and trying to create a new one for them will raise an error.

So let's make our table for our metatable to go into:

Code:

Tab = { }

Now, we have to set the metatable to be the actual metatable of our other table. Some people think it's as simple as doing something like this:

Code:

Tab = { Metatab }

But those people are completely wrong. That is a multidimensional table. Not a metatable.

To set the metatable of a table or userdata, you use the Lua function setmetatable(). It takes two arguments, the first being the table, and the second being the metatable.

Code:

setmetatable(Tab, Metatab)

setmetatable() also returns the table (Tab), so this:

Code:

Tab = {}
setmetatable(Tab, {})

Is the same as this:

Code:

Tab = setmetatable({}, {})

If you'd like to retrieve the metatable of Tab, then you use the function getmetatable(), which takes the argument of the table.

Code:

getmetatable(Tab)

If you try to print that, then it will say "Table:" and a bunch of numbers and letters. That's where the data of the metatable is being stored.

Now, we have to learn what metamethods are. Remember in the beginning of this tutorial, how I said metatables can control how tables act? Well, metatables do that by using metamethods. I think of them kind of like Events, but they are very different. I've been told to look at them like methods (hence, metamethods) but I don't see the similarities.

The first metamethod we will learn how to use is the __index metamethod. It is fired when you try to look up something in the table that does not exist.

Code:

Tab = {"Pie"}
print(Tab[1]) -->Pie
print(Tab[2]) -->The script searches for a metatable with __index in it, and doesn't find one, so it doesn't know what to do, so it raises an error

If we use the __index metamethod, we can control what happens when we try to look up the second index in Tab (which clearly doesn't exist).

Code:

Tab = {"Pie"}
setmetatable(Tab, {__index = function(Tab, Ind)
return "You have attempted to look up a nonexistant index in a table";
end})
print(Tab[1]) -->Pie
print(Tab[2]) -->You have attempted to look up a nonexistant index in a table

See what happened there? We tried to look up the second index of tab, which didn't exist, so the __index metatable was fired. The __index metamethod gets the arguments of the Table and the Index. In our example, the arguments passed would be Tab and 2.

You can also set the __index metamethod to a table. When it's fired, it will look through the table to see if it can find the nil index in that table, and if it can, that's what it returns. If it can't, then it will error. Not sure how to fix this, maybe by giving a metatable to the __index table with its own __index metamethod. Never tested it, though.

Code:

Tab = { }
setmetatable(Tab, {__index = {Pie = "HAI"}})
print(Tab.Pie) -->HAI

:D

Now, we talk about the __newindex metamethod. It fires when you attempt to add an index to the table. Note: It doesn't get fired when you attempt to change an already existing index. newindex also stops the value being set to the index.

Code:

Tab = { "Pie" }
setmetatable(Tab, {__newindex = function(t,i,v)
print("newindex was fired with arguments: ",t,i,v)
end})
print(Tab[1]) -->Pie
Tab[2] = "Potato" -->newindex was fired with arguments: Table:(stuff) 2 Potato
Tab[1] = "Caik" -- newindex doesn't get fired
print(Tab[1]) -->Caik
print(Tab[2]) -->nil

__newindex gets the arguments of the table, the index, and the value that is trying to be set to that index.

There's also the __call metamethod. It is fired when you try to call the table like a function.

Code:

tab = {}
setmetatable(tab, {__call = function(t, arg)
print("You have just called a table with the argument of", arg)
end})
tab("Pie") -->You have just called a table with the argument of Pie

The first argument is the table, and the second is the first argument. If you aren't sure how many arguments will be given, you should use those three little dots that I have no idea what they're called.

Code:

Tab = {}
setmetatable(Tab, {__call = function(t, ...)
print("A table was called with arguments", table.concat({...}, ", ")
end})
Tab("Pie", "Potatoes") -->A table was called with arguments Pie, Potato
Tab("a","b","c","d") --> A table was called with arguments a, b, c, d

The last metamethod I'm going to teach you about is the __metatable metamethod. This metamethod controls what is returned when you try to access the metatable, and it will stop anyone from being able to set a new metatable if it is there.

Code:

Tab = {}
setmetatable(Tab, {__metatable = "UR NOT ALLOWED TO SEE DIS METATABUL"})
print(getmetatable(Tab)) -->UR NOT ALLOWED TO SEE DIS METATABUL
setmetatable(Tab, {}) -- this raises an error, and doesn't affect the metatable

That is all the metamethods I am talking about in this tutorial. For more, try searching up metatables on the Roblox wiki. It has a list of metamethods at the bottom, along with the arguments passed.

Now, a small note for the end of this tutorial.

When you have data in a metatable, it cannot be found using the table unless you use the getmetatable function.

Code:

Tab = {}
setmetatable(Tab, {Pie = "Hai"})
print(Tab.Pie) -->nil
print(getmetatable(Tab).Pie) -->Hai

And while this may be one way to use them, it is certainly not their intended use.

And the final note of my tutorial:

Since metatable are just tables, they can also have their own metatables.

Code:

Tab = { }
MetaTab = { }
MetaMetaTab = { }
setmetatable(Tab, MetaTab)
setmetatable(MetaTab, MetaMetaTab)
print(getmetatable(Tab)) -->Table:(stuff) (this is MetaTab)
print(getmetatable(MetaTab) -->Table:(stuff) (this is MetaMetaTab)
print(getmetatable(getmetatable(Tab))) -->Table:(stuff) (this is also MetaMetaTab)

Well, that's the end of my first tutorial! Feedback pl0x.


Last edited by NotAPotato on February 25th 2011, 11:11 pm; edited 2 times in total (Reason for editing : I FIXED WHATEVER WAS WRONG OKAY (and I also added a few things to make it better and stuff))
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

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

PostSubject: Re: My first tutorial ~ Metatables   January 23rd 2011, 4:31 am

Much more understandable than VoidHack's! well done!

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


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 12:02 am

there's a thing that needs to be optimized.
this is your script:

Code:
Tab = {"Pie"}
setmetatable(Tab, {__index = function(Tab, Ind)
print("You have attempted to look up a nonexistant index in a table")
return;
end}
print(Tab[1]) -->Pie
print(Tab[2]) -->You have attempted to look up a nonexistant index in a table

this is the optimized script:


Code:
Tab = {"Pie"}
setmetatable(Tab, {__index = function(Tab, Ind)
return "You have attempted to look up a nonexistant index in a table"
end}
print(Tab[1]) -->Pie
print(Tab[2]) -->You have attempted to look up a nonexistant index in a table

i think this is the right way to do it, there might be more things to optimize
Back to top Go down
http://hackthissite.org
Guest
Guest



PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 12:05 am

That's a good point Supernapalm. I'll try to add it. If I can figure this thing out.
Back to top Go down
raboy117
Novice Scripter
Novice Scripter
avatar

Posts : 75
Join date : 2011-01-19
Age : 20
Location : Canada, PEI, Charlottetown

PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 12:18 am

Expert? I've learned MetaTables In an Intermediate Script Tutorial on Roblox...
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 12:20 am

yeah... well its perfect for intermediate and up. people who are learning tables, dont try to learn it. but still, metatables should be in expert because of its special abilities.
Back to top Go down
http://hackthissite.org
myrco919
Intermediate Scripter
Intermediate Scripter
avatar

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 9:53 am

Nice ;)
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 24th 2011, 8:48 pm

oh and those little dots are called varargs.
Back to top Go down
http://hackthissite.org
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 25th 2011, 12:52 am

found mistakes i think.

NotAPotato's
Code:
Tab = {}
setmetatable(Tab, {__call = function(t, ...) print("A table was called with arguments", table.concat({...}, ", "))
Tab("Pie", "Potatoes") -->A table was called with arguments Pie, Potato
Tab("a","b","c","d") --> A table was called with arguments a, b, c, d

Corrected
Code:
Tab = {}
setmetatable(Tab, {__call = function(t, ...) print("A table was called with arguments", table.concat({...}, ", ") end})
Tab("Pie", "Potatoes") -->A table was called with arguments Pie, Potato
Tab("a","b","c","d") --> A table was called with arguments a, b, c, d

also when you said

Code:
tab = {}
setmetatable(tab, {__call = function(t, arg)
print("You have just called a table with the argument of", arg)
end})
tab("Pie") -->You have just called a table with the argument of Pie

isn't it supposed to be
Code:
tab = {}
setmetatable(tab, {__call = function(arg)
print("You have just called a table with the argument of", arg)
end})
tab("Pie") -->You have just called a table with the argument of Pie

and this
Code:
Tab = {}
setmetatable(Tab, {__call = function(t, ...) print("A table was called with arguments", table.concat({...}, ", "))
Tab("Pie", "Potatoes") -->A table was called with arguments Pie, Potato
Tab("a","b","c","d") --> A table was called with arguments a, b, c, d

supposed to be this
Code:
Tab = {}
setmetatable(Tab, {__call = function( ...) print("A table was called with arguments", table.concat({...}, ", "))
Tab("Pie", "Potatoes") -->A table was called with arguments Pie, Potato
Tab("a","b","c","d") --> A table was called with arguments a, b, c, d

i dunno. either you're not explaining correctly, or parts of your scripts are wrong.
Back to top Go down
http://hackthissite.org
Guest
Guest



PostSubject: Re: My first tutorial ~ Metatables   January 25th 2011, 8:49 pm

Yeah, I guess I made a bunch of mistakes. It was my first tutorial, I've never put a tutorial somewhere when I can't see all the functions and everything highlighted in blue D:
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 25th 2011, 9:29 pm

so which one's were mistakes, and which one's need to be re-clarified?
Back to top Go down
http://hackthissite.org
Guest
Guest



PostSubject: Re: My first tutorial ~ Metatables   January 25th 2011, 10:04 pm

The two involving the __call metamethod were both incorrect, but for a reason different than you said. Mine were more correct than yours, but I may have forgotten to end the functions, and I forgot to end the metatables :/

*is too lazy to fix it*
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 25th 2011, 11:55 pm

think about all the people who are reading this. are you just going to let them have misunderstanding? i think you should fix it.
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

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

PostSubject: Re: My first tutorial ~ Metatables   January 26th 2011, 8:33 am

Indeed

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


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 29th 2011, 7:41 pm

fail tutorial. fail!
Back to top Go down
http://hackthissite.org
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: My first tutorial ~ Metatables   January 30th 2011, 6:43 pm

... still not edited. ima make my own metatable tutorial
Back to top Go down
http://hackthissite.org
Guest
Guest



PostSubject: Re: My first tutorial ~ Metatables   January 31st 2011, 4:17 am

I DON'T WANNA EDIT IT.

:(
Back to top Go down
ninga95
Administrator
Administrator
avatar

Posts : 122
Join date : 2010-07-30
Age : 22

PostSubject: Re: My first tutorial ~ Metatables   January 31st 2011, 10:52 pm

Maybe not expert, but advanced
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

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

PostSubject: Re: My first tutorial ~ Metatables   March 19th 2011, 6:01 am

can you add in metatable proxies to the tutorial, I don't know how to use them yet. :(

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

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: My first tutorial ~ Metatables   March 19th 2011, 7:24 am

Zomg o.o
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

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

PostSubject: Re: My first tutorial ~ Metatables   March 20th 2011, 5:53 am

nvm, I figured it out with wiki.

ps: anyone know how to hack into the metatable of _G? just wondering if metatables have weaknesses. :3

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



PostSubject: Re: My first tutorial ~ Metatables   March 26th 2011, 9:57 pm

Bluey, _G doesn't have a metatable in the first place. You can give it your own metatable.

And by the way, I FIXED IT, OKAY? Tell me if there's anything that I missed >_<
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

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

PostSubject: Re: My first tutorial ~ Metatables   March 27th 2011, 6:15 am

oh, I though _G had a locked metatable :P

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




PostSubject: Re: My first tutorial ~ Metatables   

Back to top Go down
 
My first tutorial ~ Metatables
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: