generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: [SFBC] - AFAP - Metatables

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
09 Jun 2015 04:25 PM
This is official documentation of the Scripters Forum Breakfast Club. Created on 6/9/2015. Official group: http://www.roblox.com/My/Groups.aspx?gid=2582784

This is the 9th installment of SFBC’s, ‘As Fast as Possible’ series. Officially abbreviated ‘AFAP’, AFAP will be aiming to teach the basic – intermediate subjects as fast as possible. Every single AFAP will include an exercise which is an optional task for the reader to see if they understand it.

This installment will cover the metatables: what they are, and how to use them.

Before reading this I ask of you to first make sure you understand tables, http://www.roblox.com/Forum/ShowPost.aspx?PostID=162704485 and functions, http://www.roblox.com/Forum/ShowPost.aspx?PostID=162455804 and returning,
http://www.roblox.com/Forum/ShowPost.aspx?PostID=162702608


Okay, so let's begin. We should start with a super-basic question. What exactly is a metatable? Well it isn't a new datatype, it's still just a regular table. So what's the difference between tables and metatables? Metatables get attached to tables.

Sound odd? Well let's begin with attaching them. To attach a metatable to a table, first you must create the tables required.

local tab = {}
local meta = {}

Good, now assuming you understand variables (http://www.roblox.com/Forum/ShowPost.aspx?PostID=162454910) you will know that the variables are just words, so the above variables could be anything. I chose named which represent what they are.
To attach a metatable to the table, you have to set it, we can use this using the function setmetatable:

local tab = {}
local meta = {}

setmetatable(tab, meta)


Good, we're doing nicely here. We have successfully set a metatable to the table. How do you get this metatable from the original name though? Instead of SETmetatable, we'd use GETmetatable:

local tab = {}
local meta = {}

setmetatable(tab, meta)
print(#getmetatable(tab))


A table can never have more then one metatable.

If you aren't asking yourself "How are these possibly useful?" yet I have never met someone like you. Well, I'm going to explain exactly how these are useful. They fire Metamethods.
Metamethods are special, they execute when something happens, a lot like events. There are many ROBLOX events, and they all have their a specific syntax, but when we're talking about metatables the syntax is entirely different.
In order to use these metamethods, we need to know their names, there are quite a few, so I will not be mentioning them all, but I will direct you to a list: http://wiki.roblox.com/index.php?title=Metamethods (also, one unmentioned in that article is __metatable)

Some common/simple ones are __index, __newindex, __add, __tostring I'll explain the syntax using __add as an example.

__add fires when you try to add to a table. Sounds crazy right? Well not actually. In the meta table, simply set __add to a function, you can pass it 2 arguments, the table it's set to, and the value being added to it:

local tab = {}
local meta = {
__add = function(self, num)
return 1 + num
end
}

setmetatable(tab, meta)
print(tab + 5)
--> 6


Most metamethods (there is only one which does not abide by this rule) always pass the table as the first argument, never the 2nd, nor 3rd, etc. Remember this, it will come in handy.

For an explanation on other metamethods, please see the link I gave you above.


Now for the test, use your knowledge to make it so that when you print a table, it prints "Hello World!" (Hint: print automatically tostrings it's arguments)
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 04:26 PM
You missed the practical applications of metatables.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
09 Jun 2015 04:28 PM
@eLunate I didn't think that was necessary to add, as it's up to the user to figure out how they will be using them. If you want you can start posting some in the comments, post them now just in case some flame war happens and the reader isn't bothered to read your posts.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 04:33 PM
Applications:
* Showing off
* Being inefficient
* Pretending you know what you're doing when you don't
* Classes
* Compact behaviours that make sense
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 Jun 2015 04:34 PM
Knowing metatables doesn't give you the right to show off, anyone can learn them. They're honestly really simple but people just overrate it "omg metatables ur so pro"

Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
09 Jun 2015 04:35 PM
I even know metatables XD. But you RARELY HAVE to use them.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
09 Jun 2015 04:36 PM
"Being inefficient"
I thought YOU said that metatables were handled in C, so it is a bit more efficient than the alternatives (To that particular part of the wrapper you were working on, I don't remember exactly where.)
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
09 Jun 2015 04:36 PM
@fish, You know metatables? Okay show me something you can do with them over a PM (Just please not just an example, something real)
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 04:36 PM
They're for showing off, really. People don't use metatables for anything other than just for the sake of using metatables, most of the time.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 Jun 2015 04:37 PM
Jarod, just because if you use it correctly it IS more efficient, many people don't use it correctly and do stuff that could be done faster without metatables.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 04:38 PM
My being inefficient remark was a cynical gesture
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
09 Jun 2015 04:43 PM
@war I just sent you some things that can be done with certain metamethods.
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
09 Jun 2015 04:47 PM
The problem of learning metatables is the way everyone approaches them.
Lots of people (including me) saw the more expereienced scripters use them in complex ways.

Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
09 Jun 2015 04:48 PM
"was a cynical gesture"

Paraphrasing:
"I prefer 'cynical sardonicism' since it sounds cooler" -eLunate

Never mind, I did a simple Google search and found it.
http://www.roblox.com/Forum/ShowPost.aspx?PostID=156746619&pageindex=2#156780452
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
09 Jun 2015 04:49 PM
Or perhaps this is a better link:

http://www.roblox.com/Forum/ShowPost.aspx?PostID=156746619&pageindex=2#156755565
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 04:55 PM
Sardonic cynicism
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
09 Jun 2015 04:58 PM
Oh
I see...
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
09 Jun 2015 05:01 PM
Eh, muahaha?
Report Abuse
suremark is not online. suremark
Joined: 13 Nov 2007
Total Posts: 6315
09 Jun 2015 05:13 PM
I prefer function closures to metatables for pseudo-OOP in Lua. Private access is much easier with closures--with metatables it's... well, it's actually not possible.

Metatable based OOP has the advantage of using less memory however, because only one function is ever created for every instance of an object. With closures, a new set of methods is instantiated in each object's local environment, which uses significantly more overhead despite being faster.
Report Abuse
Froast is not online. Froast
Joined: 12 Mar 2009
Total Posts: 3134
09 Jun 2015 05:17 PM
learnxinyminutes>
Report Abuse
Darkenus is not online. Darkenus
Joined: 17 Jul 2014
Total Posts: 1997
09 Jun 2015 11:20 PM
OMG, why must scripters have fights. *facepalm*
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
10 Jun 2015 02:03 AM
There's no point in generating new members for every class, unless those members can be set. Metamethods have their many advantages if you know what you're doing, allowing you to handle things that closures never could.

That, and if you use closures you lose the whole class idea unless you give functions which still take self, which ruins the point of using closures.
Report Abuse
drager980 is not online. drager980
Joined: 25 May 2009
Total Posts: 13385
10 Jun 2015 02:52 AM
in all honesty i have not learnt metatables yet because ive never found a use for them in the few years ive been scripting,



AND THE TIGER GOES ROAR
Report Abuse
bosswalrus is not online. bosswalrus
Joined: 04 Jan 2013
Total Posts: 5430
30 Jun 2015 07:36 PM
Anaminus explains this a little bit more

http://www.roblox.com/Forum/ShowPost.aspx?PostID=1971993
Report Abuse
drager980 is not online. drager980
Joined: 25 May 2009
Total Posts: 13385
30 Jun 2015 08:27 PM
why did you post a reply 20 days later



AND THE TIGER GOES ROAR
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image