Bynaric
|
  |
| Joined: 15 Apr 2014 |
| Total Posts: 2 |
|
|
| 19 Apr 2014 12:40 PM |
| I've been curios for a while. |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
|
| 19 Apr 2014 12:43 PM |
| Metatables allow you to do OOP in Lua. |
|
|
| Report Abuse |
|
|
Bynaric
|
  |
| Joined: 15 Apr 2014 |
| Total Posts: 2 |
|
| |
|
|
| 19 Apr 2014 02:22 PM |
@200AB thats the most vague answer i've ever heard its not just for OOP either its for making tables able to do functions |
|
|
| Report Abuse |
|
|
lolb3
|
  |
| Joined: 16 Jan 2010 |
| Total Posts: 2268 |
|
|
| 19 Apr 2014 02:31 PM |
| question is, why metatables? |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
| |
|
|
| 19 Apr 2014 03:14 PM |
Let me explain. For me, no matter how much people explain it, it takes a while to learn. Metatables are one of the most complex things in Lua so they are very hard to learn because they are far from English.
Metatables allow you to modify the behavior of tables. What does this mean? Well, suppose you have two tables with numbers in them, and you wanted to add the tables together in to 1 table with a bunch of numbers like this:
{1,2,3,4}+{5,6,7,8}
This would obviously error, as you can't add two tables together. However, with metatables, you can. Like this:
mt = {} mt._add = function(a,b) return a+b end t1 = {1,2} t2 = {3,4} setmetatable(t1,mt) setmetatable(t2,mt) print((t1+t2)[i]) -- prints 4.
This may not be perfect, but it's something like this lol. I don't know metatables inside and out yet, but hopefully this helps :D. |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
|
| 19 Apr 2014 03:28 PM |
They arent that complex. They just offer functions to tables like newindex, add, etc. That you can then set to your own functions to do whatever you want. its that simple. |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
|
| 19 Apr 2014 03:29 PM |
| Basically adds events to tables. |
|
|
| Report Abuse |
|
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 20 Apr 2014 09:52 AM |
Everything in Lua is a table.
Yes.
Numbers are tables. Instances are tables. Parts are tables. Strings are tables.
They all have a field which define their behavior. For instance, a number + a number should return the number added to another number. You define this behavior via metatables.
The cool thing is that this is also used by roblox instances. For example, when you set game.Workspace.BasePlate.Name = "Hello" the __index metatamethod is invoked which sets the Name to "Hello". This can also communicate with the C-side core of roblox (to handle rendering / network etc) to share this change will all clients.
Conclusion: Lua is awesome. |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2014 09:57 AM |
Jobro, don't you mean __newindex?
And also, other languages have similar features. For example, in Ruby, if I want to do the same as setting __add, I do
class Potato def +(other) return other+5 end end
potato=Potato.new puts(potato+1) #outputs 6 |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
| |
|
|
| 20 Apr 2014 10:04 AM |
They let you: get indexes of tables that aren't set change the way new indexes of tables are set call tables like functions(!) turn tables negative with a unary minus add, subtract, multiply and divide tables use moduli and exponents with tables change the way tostring works on the table change the conditions on which two tables are equal compare tables using >, >=, <, <= make tables weak (haven't figured this out yet) change what happens when a table is garbage-collected change the way the length (#) operator works with tables
yeahhhhhhhhhhhh
~This sig is false~ |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 20 Apr 2014 10:10 AM |
What would be nice is a way to customize the type with a metamethod.
taco = {} tacoMT = { __index={ kind = "hard", ingredients = { "cheese", "meat" } }, __tostring = function() return "ima taco" end, __type = "Taco" --or a func, though I think strings make more sense since types are likely to not be dynamically changing lol } setmetatable(taco,tacoMT) print(type(taco))
Output: Taco |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2014 10:14 AM |
@mett
you can always use getmetatable to distinguish them
~This sig is false~ |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 20 Apr 2014 10:20 AM |
| Ik, I just like making metatable based stuff protected even if I would be the only one using the objects with my code and would likely have them locked. __eq could work too I guess, but the ability to use something like __type would still be pretty interesting. |
|
|
| Report Abuse |
|
|
jobro13
|
  |
| Joined: 05 Aug 2009 |
| Total Posts: 2865 |
|
|
| 20 Apr 2014 11:51 AM |
As TheLuaWeaver pointed out, I indeed meant __newindex instead of __index.
Silly me. |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 20 Apr 2014 11:51 AM |
@Mett: Don't forget you can overwrite all the built in functions :P (unless Roblox locked them, idk if they did but I use code like this when I code in Love and Moonscript so that I know if I'm dealing with a random table or an instance of a Vector2 class)
do local oldTypeFunction = type;
local newTypeFunction = function (object) local objectType = oldTypeFunction(object);
if ((objectType == "table") and object.__type) then objectType = object.__type; end
return objectType; end;
type = newTypeFunction; end
|
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 20 Apr 2014 11:54 AM |
| True, completely forgot about that. Built in support would still be nice tho. |
|
|
| Report Abuse |
|
|
|
| 22 Apr 2014 11:11 AM |
@200
They are more complex than just adding methods to tables, as you could do that like this:
t = {}
function t:blag() print("Blag not blah") end t:blag() |
|
|
| Report Abuse |
|
|
200AB
|
  |
| Joined: 24 Aug 2010 |
| Total Posts: 1604 |
|
|
| 23 Apr 2014 10:36 AM |
@super I then went on to clarify they can be used to add events to tables. Please read all of what I said before replying. |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 08:46 AM |
| Just because they can be used for something doesn't mean they are as simple as that thing. Then again, what more are metatables than events to tables? Hmm.... Is that really all they are lol? They seem to be more than that to me, but I can't think of anything else they are lol :P. |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 08:47 AM |
@200
Just because they can be used for something doesn't mean they are as simple as that thing. Then again, what more are metatables than events to tables? Hmm.... Is that really all they are lol? They seem to be more than that to me, but I can't think of anything else they are lol :P. |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 09:42 AM |
t = {} function t:blag() print("Blag not blah") end t:blag()
That code has nothing to do with metatables. I don't see how this is relevant to the discussion. |
|
|
| Report Abuse |
|
|
|
| 26 Apr 2014 10:02 PM |
| That code prooves that metatables are way more complex than just methods for tables. |
|
|
| Report Abuse |
|
|