|
| 23 May 2014 04:09 PM |
Yea, you guessed it. Metatables.
(1 out every 2 people have already left the thread...)
I need help understanding them. Not anything specific.
(1 out of ever 4 people will have left now :P)
Anybody wanna help me?
(AgentFirefox and a few others are left.)
Thanks people who never left :D |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:11 PM |
funnily enough, i actually was just about to look into metatables, apparently they're not that difficult.
but then again it was people like agent and cnt who said that (y'know, those master genius guys).
|
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:12 PM |
cnt ain't master genius. He's said a few things on my threads that made 0 sense.
ANYWAY BEFORE I START A FIGHT XD
Can you help..? |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:28 PM |
"(AgentFirefox and a few others are left.)"
You were right! :o |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 23 May 2014 04:34 PM |
Metatables are tables contained within tables, they are most useful for metamethods, which are functions that automatically get called when doing certain things with the parent table's values, this can be overridden with the rawset and rawget functions.
This is different than
table = {1, {2, 3}, 4}
where indexing table[2] would return a table containing 2 and 3.
See also: http://wiki.roblox.com/index.php?title=Metatable |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 May 2014 04:38 PM |
"Metatables are tables contained within tables,"
This isn't quite true, but the rest of your post was accurate for a brief summary. |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:41 PM |
I know this;
Metatables are table attached the regular tables. They make regular tables more powerful with the use of Metamethods.
What I don't understand is how to use them.
Agent. Help meh! |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:41 PM |
what i dont understand is why you should use them
|
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:43 PM |
"what i dont understand is why you should use them"
Which is a very valid question, especially for Rbx.Lua. We generally have zero use for metatables in Roblox. The only real exception I can think of is attaching metatables to tables that are returned from ModuleScripts, so people using libraries don't accidentally overwrite something from within the table.
In 'normal' Lua, though, metatables are the only way to simulate OOP. |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 23 May 2014 04:46 PM |
Oh!
You can use the setmetatable and getmetatable methods to use metatables.
setmetatable - Takes two arguments, the table which you are setting the metatable to, and the metatable itself, both are tables.
getmetatable - Takes one argument, the table to retrieve the metatable from.
You can read up on metamethods here: http://wiki.roblox.com/index.php?title=Metamethods |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:54 PM |
Okay what if I use getmetatabld on a table with 2 metatables?
Can metatables be used on metatables?
How do I use metamethods?
By the way I read the metatable wiki article. |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:56 PM |
I kind of understand them now! Metamethods are kind of like functions that are called when you do something to a table.
Can someone tell me if I'm doing this right?
local mt = { __eq = function(self, other) for i, v in pairs(self) do if v ~= other[i] then return false end end return true end }
local a = {1, 2, 3} local b = {1, 2, 3} local c = {5, 9, 8}
setmetatable(a, mt) setmetatable(b, mt setmetatable(c, mt)
print(a == b) --true print(b == c) --false
Correct me if I did something wrong please! |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:58 PM |
Oh by the way. Agent, here;
http://www.roblox.com/Forum/ShowPost.aspx?PostID=134896222
You said I can't, now correct me if I'm wrong, but if someone accesses this through a table, will it invoke __index?
If I am, I think I must be a genius. I could have just corrected AgentFirefox. Take me now :D |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:58 PM |
I believe the old metatable is replaced with the new one if setmetatable is called more than once. Since metatables are just tables, they themselves can have metatables (but that's rather useless). The Metatables article has a table of metamethods and describe what they do. Just do some experimentation with them to get a grasp.
The most important ones are __index, __newindex, __metatable, __tostring, and __call. |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 04:59 PM |
@ForeverDev, AgentFirefox, coplox
Help me! |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 05:00 PM |
"Oh by the way. Agent, here;
http://www.roblox.com/Forum/ShowPost.aspx?PostID=134896222
You said I can't, now correct me if I'm wrong, but if someone accesses this through a table, will it invoke __index?
If I am, I think I must be a genius. I could have just corrected AgentFirefox. Take me now :D"
Technically, it is possible through metatables and ModuleScripts (or global variables). You'd have to use something similar to my Custom Hierarchy package: http://www.roblox.com/Custom-Hierarchy-Read-README-item?id=152127761 |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 23 May 2014 05:01 PM |
There can only be one metatable per table, if you use setmetatable on a table that already has a metatable, it will be overwritten.
Yes, you can give a metatable a metatable, those are called meta-metatables. You can even give meta-metatables metatables, those are called meta-meta-metatables. And if you're silly, you can even give meta-meta-metatables a metatable, which is called a meta-meta-meta-metatable. For the clinically insane, you can give a meta-meta-meta-metatable a metatable, which is called a
23.5.2014.17:58 ERROR LINE 3 - MAXIMUM STRING LENGTH EXCEEDED
Aside from that, the wiki article I have linked in the last post is the Metamethods article, not the Metatable article, it explains in detail what Metamethods are and how to use them. |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 05:04 PM |
Half the reason I wanna learn metatables was this post;
crazyman32
Joined: 13 Apr 2008 Total Posts: 17198 Crazy Aeronautics 05-23-2014 01:08 PM Oh, so like adding your own methods to an existing service? You could just wrap the service in a 'proxy' using a metatable and then basically extend the service with your own methods and properties.
Which was at my thread;
http://www.roblox.com/Forum/ShowPost.aspx?PostID=134898303
The other half is that its a topic I don't understand yet.
Can anybody explain his post? |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 23 May 2014 05:05 PM |
| He means making something look like it's the service according to the rest of the script when really it just redirects you to the service, and since that is your table, you can add fake methods to it. |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 05:06 PM |
@cop
What would you cal a metatable in a metatable that's in a table within a metatable which is in a table..? Lol
So basically;
Table -Metatable --Table ---Metatable ----Metatable -----Whats this called?
A meta-meta-table-metatable? XD |
|
|
| Report Abuse |
|
|
|
| 23 May 2014 05:07 PM |
Okay so you mean, instead of a service it's a table..?
If you go to the link then what he said really won't work for my situation. |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
| |
|
|
| 23 May 2014 05:09 PM |
Lol
You have created a maniac.
Seriously now though. Help me with metamethods! Please. Teach me everything you know :D |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 23 May 2014 05:09 PM |
| Yeah, it's a table, but the point is, fooling the rest of the script to think it's the service instead. You can use the __index metamethod to get the value from the real service if it exists, otherwise check if there is a custom value in the metatable, and if there is fetch it, or do nothing. |
|
|
| Report Abuse |
|
|