|
| 12 Sep 2017 09:00 PM |
Ok so I'm starting to understand how to use metatables and metamethods, but my main question is...
WHY ARE THEY USEFUL :V
I mean they must be useful in SOME way... But how? 🤔🤔🤔
How do you guys use metamethods?
discord.gg/fissy Scripters' Academy Discord (just starting up): discord.gg/VrbkYPs yeah yeah multiply that math.random by the vector3 to get the cframe and use them htmls to bypass the mainframe of the mysql yeah |
|
|
| Report Abuse |
|
|
kennydies
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 825 |
|
|
| 12 Sep 2017 09:08 PM |
They pretty much just make default lua tables more powerful. An example of something you could do is whenever some code tries to reference an index in a table which is nil, you can return a non-nil value. Off the top of my head this could be used in, for example, allowing a player to do a table lookup of some sort. Imagine you have a dictionary which stores different things like {apple = 5} and you're letting a player search it, if they input something which doesn't have a value you could return 'Error: no value found' within a metamethod rather than having to handle that somewhere else in the code.
There are a lot of possibilities; it's not like metatables as a concept are only good for one thing.
|
|
|
| Report Abuse |
|
|
|
| 12 Sep 2017 09:57 PM |
They're only good if you know why you need them. If you don't know why you need them, you don't need them. In Roblox more especially. Outside of Roblox, they are more useful (Or so I hear), but you have very little need for them here in our environment.
|
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
|
| 12 Sep 2017 09:59 PM |
| Vectors & Matrices are a top use for them personally |
|
|
| Report Abuse |
|
|
|
| 12 Sep 2017 10:01 PM |
Yes. For the most part, they allow better implementation of OOP in Lua. In Roblox, OOP is not needed on your part because everything Roblox does is OOP anyways (Instances, Vectors, CFrames, Colors, blah blah blah)
They're okay if you are making a Module or Plugin that someone else is going to use, to make it simpler for them. But if you're doing this for yourself, you won't need them.
|
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 12 Sep 2017 10:26 PM |
It is all about ease of use, and userdata.
All of roblox's type like Vector3, CFrame, Part, etc are userdatas with metatables providing their behavior. |
|
|
| Report Abuse |
|
|
|
| 13 Sep 2017 11:49 PM |
"are userdatas with metatables providing their behavior." Except not really because they're not metatables, but we know what you mean.
|
|
|
| Report Abuse |
|
|
|
| 13 Sep 2017 11:55 PM |
ROBLOX does use metatables backend; these are made and implemented in the C++ using the Lua C api.
As for how they are useful? Memoization, cacheing, proper handling of data and garbage collection (weak tables), improved OOP (which can sometimes help out when you don't want 400 functions for each individual process), and last but not least, my favorite of them all, just because.
Tables are already a powerful tool as they are but this just extends their usage to far past storage. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 13 Sep 2017 11:55 PM |
No, exactly what I said.
They are userdatas with metatables providing their behavior. |
|
|
| Report Abuse |
|
|
|
| 14 Sep 2017 08:56 AM |
No, and I agree with you fully. But I understood what you meant, I was just trying to beat Flux to the draw. Pretend my other post doesn't exist, and we'll try again.
inb4 cntkillme says "They aren't real 'metatables' since it's C side, but whatever."
|
|
|
| Report Abuse |
|
|
|
| 14 Sep 2017 11:01 AM |
They're real metatables, just implemented in a lower level. They are by all means the same functionally and structurally. (As proven by a couple exploits.) |
|
|
| Report Abuse |
|
|
|
| 14 Sep 2017 11:50 AM |
Huh, I can't find where I thought cntkillme said that. In fact, it looks like he has a lot of posts saying otherwise. My bad then.
|
|
|
| Report Abuse |
|
|