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
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
naknak
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
Supernapalm
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
m27frogy
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
slayer9365
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
myrco919
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
branefreez
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
ninga95
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
CloneTrooper787
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
raboy117
My first tutorial ~ Metatables Vote_lcapMy first tutorial ~ Metatables Voting_barMy first tutorial ~ Metatables Vote_rcap 
Top posting users this month
No user
Latest topics
» Where to go from here.
My first tutorial ~ Metatables EmptySeptember 14th 2020, 1:20 pm by MrNicNac

» Send me an Email
My first tutorial ~ Metatables EmptySeptember 14th 2020, 1:16 pm by MrNicNac

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

» New Site Possibly
My first tutorial ~ Metatables EmptyJuly 6th 2015, 4:16 pm by m27frogy

» Ambassador!
My first tutorial ~ Metatables EmptyApril 15th 2015, 11:40 pm by naknak

» Boop - Tag
My first tutorial ~ Metatables EmptyApril 13th 2015, 9:46 pm by naknak

» Vip Class Script
My first tutorial ~ Metatables EmptyApril 13th 2015, 4:54 pm by naknak

» Who's active?!
My first tutorial ~ Metatables EmptyApril 13th 2015, 4:52 pm by naknak

» Genesis Point
My first tutorial ~ Metatables EmptyJuly 17th 2014, 7:04 pm by branefreez

» Reward System
My first tutorial ~ Metatables EmptyJuly 17th 2014, 5:41 am by m27frogy

» Script Request
My first tutorial ~ Metatables EmptyJuly 10th 2014, 11:43 am by naknak

» local scripts?
My first tutorial ~ Metatables EmptyJuly 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
My first tutorial ~ Metatables EmptyJuly 10th 2014, 11:36 am by naknak

» Hi. I am new here
My first tutorial ~ Metatables EmptyApril 26th 2014, 4:01 pm by altshiftkey

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

» FlashLight Script
My first tutorial ~ Metatables EmptyJanuary 1st 2014, 2:11 pm by m27frogy

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

» Hi, I am new here!
My first tutorial ~ Metatables EmptyNovember 26th 2013, 3:33 pm by Keanu73

» Improve Coding
My first tutorial ~ Metatables EmptyOctober 26th 2013, 1:12 pm by pook03

» Simple Button
My first tutorial ~ Metatables EmptySeptember 1st 2013, 6:19 pm by branefreez


 

 My first tutorial ~ Metatables

Go down 
5 posters
AuthorMessage
Guest
Guest
avatar



My first tutorial ~ Metatables Empty
PostSubject: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
blueymaddog


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

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

Much more understandable than VoidHack's! well done!
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar



My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
raboy117


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

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
myrco919


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

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

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


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar



My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar



My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
avatar


Posts : 393
Join date : 2011-01-17

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyJanuary 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
blueymaddog


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

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

Indeed
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

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

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


Posts : 393
Join date : 2011-01-17

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

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



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

I DON'T WANNA EDIT IT.

:(
Back to top Go down
ninga95
Administrator
Administrator
ninga95


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

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

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


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

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

can you add in metatable proxies to the tutorial, I don't know how to use them yet. :(
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter
myrco919


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

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

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


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

My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyMarch 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
Back to top Go down
Guest
Guest
avatar



My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables EmptyMarch 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
blueymaddog


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

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

oh, I though _G had a locked metatable :P
Back to top Go down
Sponsored content





My first tutorial ~ Metatables Empty
PostSubject: Re: My first tutorial ~ Metatables   My first tutorial ~ Metatables Empty

Back to top Go down
 
My first tutorial ~ Metatables
Back to top 
Page 1 of 1
 Similar topics
-
» Metatables Tutorial
» Metatables Tutorial
» 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: