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 |
|
| My first tutorial ~ Metatables | |
| | Author | Message |
---|
Guest Guest
| Subject: 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)) |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: My first tutorial ~ Metatables January 23rd 2011, 4:31 am | |
| Much more understandable than VoidHack's! well done! | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: 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 | |
| | | Guest Guest
| Subject: 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. |
| | | raboy117 Novice Scripter
Posts : 75 Join date : 2011-01-19 Age : 27 Location : Canada, PEI, Charlottetown
| Subject: Re: My first tutorial ~ Metatables January 24th 2011, 12:18 am | |
| Expert? I've learned MetaTables In an Intermediate Script Tutorial on Roblox... | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: 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. | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: Re: My first tutorial ~ Metatables January 24th 2011, 9:53 am | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: My first tutorial ~ Metatables January 24th 2011, 8:48 pm | |
| oh and those little dots are called varargs. | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: 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. | |
| | | Guest Guest
| Subject: 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: |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: 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? | |
| | | Guest Guest
| Subject: 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* |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: 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. | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: My first tutorial ~ Metatables January 26th 2011, 8:33 am | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: My first tutorial ~ Metatables January 29th 2011, 7:41 pm | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: My first tutorial ~ Metatables January 30th 2011, 6:43 pm | |
| ... still not edited. ima make my own metatable tutorial | |
| | | Guest Guest
| Subject: Re: My first tutorial ~ Metatables January 31st 2011, 4:17 am | |
| |
| | | ninga95 Administrator
Posts : 122 Join date : 2010-07-30 Age : 29
| Subject: Re: My first tutorial ~ Metatables January 31st 2011, 10:52 pm | |
| Maybe not expert, but advanced | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: 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. :( | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: Re: My first tutorial ~ Metatables March 19th 2011, 7:24 am | |
| | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: 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 | |
| | | Guest Guest
| Subject: 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 >_< |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: My first tutorial ~ Metatables March 27th 2011, 6:15 am | |
| oh, I though _G had a locked metatable :P | |
| | | Sponsored content
| Subject: Re: My first tutorial ~ Metatables | |
| |
| | | | My first tutorial ~ Metatables | |
|
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! |
|