#Can someone explain metatables and metamethods to me. Not sure I understand fully
1 messages · Page 1 of 1 (latest)
Discord: https://discord.gg/bEn49K5JUt
Patreon: https://www.patreon.com/Suphi
Donate: https://www.roblox.com/games/7532473490
In this video i will be showing you how metatables work
https://developer.roblox.com/en-us/articles/Metatables
http://lua-users.org/wiki/MetamethodsTutorial
http://lua-users.org/wiki/MetatableEvents
metatables are just tables containing information about another table
they can also function as tables, which allows you to mimic OOP
it basically gives more functionality to a table, which allows you to do epic stuff with them (such as OOP)
the metatable is just a table with all functions inside of it.
those functions are called methamethods
it basicly allows you to do stuff like, subtract tables from eachother. (__sub)
is there anything you specificly don't understand of them?
when you do setMetatable(table param, table param) you are saying that the table in paramater two will be a metatable for table in parameter one
local myMetatable = {} -- a normal table
local myTable = {} -- a normal table
setmetatable(myTable, metatable) -- sets myMetatable to be myTables metatable
table will index metatable for metamethods when certain events occur, like when you index table but there is no relevant key (but there is an __index key/pair in ur metatable)
local myMetatable = {} -- a normal table
-- if this table is a metatable then lua will use this tables __index key to know what to do when we access a key in the parent table
myMetatable.__index = function(...) print(...) end
local myTable = {} -- a normal table
setmetatable(myTable, metatable) -- sets myMetatable to be myTables metatable
From the tutorial and the Curve tutorial, I only grasped that metatables connect 2 tables together. When the main table didn't have a value, when you would index the code for it: the metatable would fill in for it
local Module = {}
Module.__index = Module
Module.Hello = "hi"
function Module.new()
local self = setmetatable({},Module)
-- self does not have the index Hello, so it will check the metatable, which has the index function, which will return the table module, which has hello, so it returns "hi"
print(self.Hello) --> expected output: "hi"
return self
end
return Module
Could you explain the __index more?
basicly this works that if it can't find it in the returned value ({}) then it will see that it has a metatable (Module) and then it will return any other function FROM Module
or index
local myMetatable = {} -- a normal table
-- if this table is a metatable then lua will use this tables __add key to know what to do when we try to add to the parent table
myMetatable.__add = function(...) print(...) end
local myTable = {} -- a normal table
setmetatable(myTable, metatable) -- sets myMetatable to be myTables metatable
myTable + 5 -- when we try to add to a table it will call the __add function
if your table as a metatable with __index it will reference whatever you set it equal to when table is indexed but there is no key
local shrug = {
[0] = "red",
"blue",
"green",
nil,
"orange"
}
local metathis = {}
setmetatable(shrug, metathis)
print(shrug[3])
?
this will print nil
yes?
I've updated this code
so what exactly is the metatable doing?
think of it like a table that stores functions
in your example nothing
It's like a power
updated it again
A power for tables
oh
actually that perfectly explains it
those functions are metamethods
i litterly said that lol
in this message
you can find all the metamethods here
So all these metamethods have specific uses that ill have to look up in order to use and learn about
neat
yea
i posted that first thing lol
kind of hard to keep track with 5 people trying to help at once lmao
sorry
would you consider it to sort of be like matrixes in precalculus?
true
errrrrrrrrrrrrrrrr
matrices are tables with a certain number of x and y values. You can do math with them like multiplication and whatnot
an example:
oh i see
now, if I am understanding this right... metatables are SORT OF similar?
except instead of multiple positions, its only the table itself
pretty much yea
okay yeah then that explains everything
Tables and matrices have nothing to do with each other
._.
._ .
i know.
I was not saying they have anything to do with each other
i was making a comparison
the math-related metamethods could do that
I don't get the comparison
not saying they are similar
have 2 tables
do stuff to the table with other table
Regardless. I understand it way better now.
They don't do stuff with each other
The metastable is like a list of settings for the first table
Metatable does nothing just holds the settings
even still.
The commparison helped me to expand my knowledge, and while they aren't inherently similar. It DOES help me in this case to know.
I'm not saying it's logical Suphi. I'm saying it's making a comparison to understand
But are you sure what you know is correct
not a comparison as in a logical one
If I told you do make something with metatables could you do it?
I mean yeah I think I have like 30% more understanding now than I did before.
only one way to find out
Which in my opinion, is way better than the 5% I had before.
go make something using metatables @quasi meadow
local value = 0
local myTable = {}
myTable + 5 -- make it so that when I add 5 it adds 1 to value and prints it
local value = 0
local times = 3
local myTable = {}
local metathismetathathaveyoueverevenmetagirlbefore = {
__call = function(five, val2)
for i, numbers in ipairs(five) do
local multi = numbers/5
value += 1 * multi --1*1, 1*2, 1*3; added together = 6
end
return value
end
}
task.wait(3)
for i = 1, times, 1 do
table.insert(myTable, 5 * i)
--print(myTable) --{5, 10, 15}
end
local itstimetoddddddddddddduel = setmetatable(myTable, metathismetathathaveyoueverevenmetagirlbefore)
print(itstimetoddddddddddddduel(myTable))```
?
"make it so that when I add 5, it adds 1 to value and prints it"
makes it so when you add exactly 5, it adds 1 value. Values in increment of 5 add additional value corresponding to the multitude of 5
the code itself works Suphi.
Then why don't you show me then.
I said I have roughly a 35% understanding of it.
Never said I could do anything with a 35% understanding
maybe there is a metamethod that fires when you do table + something ? 🙂
#1172253240478400542 message
I already showed you most of it
I was trying to use the documentation.
Also I despise surprises.
Im asking you to show me.
Not to redirect me and give me more hints
Did you run the code I showed above
The video on metatables has a high pitched ringing that kills my ears, nor does this help me specifically
Ok and what's it print when you run it
Good now what did you learn
absolutely nothing
Ok change 5 to 8
why did it print {} THEN the 5
And run it again
and why is myTable += 5 printing in the output without a print statement
~~yes I was blind
~~
actually no
This is where you need to connect the dots
Can you make it print {} 8
Im going to assume it's going to add the 8 to the value.
No
Metatables don't do anything
Ok what did you learn
what i already knew before
line 5.
you are setting the __add of the metatable to print whatever ... is, so in this case I'm assuming you are wanting to print what you add to MyTable. However this does not add to the MyTable itself just yet. Instead, you are printing it next to MyTable
I've seen ... here and there, but unfortunately: no. I do not.
Think of ... Like a list of values
A little like a table
Change ... To a, b, c
And print a,b,c
? im going to guess it will print the table, 8, then nil
Yes
actually nvm
itwont print anything at all
xD
but thats because i need to update the function(...)
Yes
Ok so what's happening
a is the table, b is the value we are attempting to add, and c has no variable associated with it. So it is nil
but we haven't made a __concat?
It worked
i mean i guess it's because nothign is being added to the table just yet so it still does make logical sense
Ok so we know that if we do
Mytable + anyvalue
It will call the function inside the metastable
hmm
And that function will tell us the table and the value we tried to add
So how could we make it so that if we add 5 it adds 1 to value
We don't have to return anything
{} 5 ?
Nope
It's nil
Nope
it doesnt return anything
Mytable + anyvalue should now be equal to bob
Print(Mytable + anyvalue) will print bob
Also
You did += above
That will set my table to bob
You can ignore the error
What you do mytable += 5 it's like doing mytable = mytable + 5
yes
Ok
I believe I was just trying to finish the statement so I found += worked
but print(myTable + 5) works as you were saying
So hopefully if you followed along and understood everything you should be at 80% understanding
So you can see why metatables are very powerful and can do many things
I can't see it just yet unfortunately
But I can try to imagine systems I might use for it..
When you do
Like maybe an economy system?
Vector3 + vector3
Or when you do vector3 * 2
Now you know how Roblox makes that work
Vector3 is just a table with a metatable
Maybe if you watch my video a second time it might be easier to understand now
potentially.
I will say I was partially right in my earlier comparison
it is like matrices, except it's allowing the matrices to be calculated with what you do with it
and the meta table is the math you want to do. Holding the important bits and pieces around what you do when you want to do the math
Yeah I can understand the video now
I dont understand when and where I apply index yet though
I’d say matrices are over complicating things tbh (as a non mather)
maybe
one might say metatables are more like "tables with functions inside of them" xD
as a comparison
Basically 
I cant think of a use just yet which is unfortunate
i retain information better when I can apply it
Proxy tables ig
but ty father the hairy balls have bestowed on me divine vision and have helped me immensely @mystic sundial
😧
?
i mean i guess
basically metatables is this
yup
ok a simpler way to look at it is just think of them like roblox functions
like part.Touched
similarly table.__'metamethod here' will fire
👍
yeah i get it now lol
suphi hand held me threw it
patreon benefits