#Can someone explain metatables and metamethods to me. Not sure I understand fully

1 messages · Page 1 of 1 (latest)

tulip shard
#

metatables are just tables containing information about another table

#

they can also function as tables, which allows you to mimic OOP

subtle hinge
#

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?

tulip shard
#

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

mystic sundial
#
local myMetatable = {} -- a normal table

local myTable = {} -- a normal table

setmetatable(myTable, metatable) -- sets myMetatable to be myTables metatable
tulip shard
#

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)

mystic sundial
#
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
quasi meadow
#

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

subtle hinge
#

thats the __index function

#

thats why modules are like this

subtle hinge
#
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
quasi meadow
#

Could you explain the __index more?

subtle hinge
#

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

mystic sundial
#
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
tulip shard
#

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

quasi meadow
#
local shrug = {
[0] = "red",
"blue",
"green",
nil,
"orange"
}

local metathis = {}

setmetatable(shrug, metathis)
print(shrug[3])
#

?

#

this will print nil

#

yes?

quasi meadow
#

so what exactly is the metatable doing?

rancid anchor
#

think of it like a table that stores functions

mystic sundial
ripe carbon
subtle hinge
ripe carbon
#

A power for tables

quasi meadow
#

actually that perfectly explains it

rancid anchor
#

those functions are metamethods

subtle hinge
mystic sundial
#

you can find all the metamethods here

quasi meadow
#

So all these metamethods have specific uses that ill have to look up in order to use and learn about

#

neat

rancid anchor
#

yea

quasi meadow
quasi meadow
#

sorry

quasi meadow
rancid anchor
#

i'm not in precalc yet

#

i take it next year

quasi meadow
#

let me explain it like this:

rancid anchor
#

errrrrrrrrrrrrrrrr

quasi meadow
#

matrices are tables with a certain number of x and y values. You can do math with them like multiplication and whatnot

#

an example:

rancid anchor
#

oh i see

quasi meadow
#

now, if I am understanding this right... metatables are SORT OF similar?

#

except instead of multiple positions, its only the table itself

rancid anchor
#

pretty much yea

quasi meadow
#

okay yeah then that explains everything

rancid anchor
#

so yea ig so

mystic sundial
mystic sundial
#

Metatables don't do anything out of the box

#

They do no math

quasi meadow
#

i know.

#

I was not saying they have anything to do with each other

#

i was making a comparison

rancid anchor
#

the math-related metamethods could do that

mystic sundial
#

I don't get the comparison

quasi meadow
#

not saying they are similar

quasi meadow
#

do stuff to the table with other table

#

Regardless. I understand it way better now.

mystic sundial
#

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

quasi meadow
#

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

mystic sundial
#

But are you sure what you know is correct

quasi meadow
#

not a comparison as in a logical one

mystic sundial
#

If I told you do make something with metatables could you do it?

quasi meadow
#

I mean yeah I think I have like 30% more understanding now than I did before.

rancid anchor
#

only one way to find out

quasi meadow
#

Which in my opinion, is way better than the 5% I had before.

rancid anchor
#

go make something using metatables @quasi meadow

mystic sundial
#
local value = 0

local myTable = {}

myTable + 5 -- make it so that when I add 5 it adds 1 to value and prints it
quasi meadow
# mystic sundial ```lua local value = 0 local myTable = {} myTable + 5 -- make it so that when ...
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))```
mystic sundial
quasi meadow
#

?

#

"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.

mystic sundial
#

When you do

Mytable + 5

#

It should add 1 to value

quasi meadow
#

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

tulip shard
quasi meadow
tulip shard
#

maybe there is a metamethod that fires when you do table + something ? 🙂

mystic sundial
#

I already showed you most of it

quasi meadow
#

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

mystic sundial
#

Did you run the code I showed above

quasi meadow
#

The video on metatables has a high pitched ringing that kills my ears, nor does this help me specifically

mystic sundial
#

I think it's ok to ignore that error

#

Make sure you type metatable correctly

quasi meadow
#

I copy pasted your code.

#

this is it with the right variable names

mystic sundial
quasi meadow
#

Hold on my boyfriend is snoring. Give me a moment

mystic sundial
#

Good now what did you learn

quasi meadow
#

absolutely nothing

mystic sundial
#

Ok change 5 to 8

quasi meadow
#

why did it print {} THEN the 5

mystic sundial
#

And run it again

quasi meadow
#

and why is myTable += 5 printing in the output without a print statement
~~yes I was blind Face ~~

#

actually no

mystic sundial
quasi meadow
#

scratch that

#

i see it.

#

still doesnt explain the {} then the 5

mystic sundial
#

Can you make it print {} 8

quasi meadow
#

i mean yeah

#

you just change the 5 to an 8

mystic sundial
#

Ok

#

Put something inside mytable

#

Like random value

#

What's it print now

quasi meadow
#

Im going to assume it's going to add the 8 to the value.

mystic sundial
#

No

quasi meadow
#

?

#

ill try it then

mystic sundial
#

Metatables don't do anything

quasi meadow
#

:T

mystic sundial
#

Ok what did you learn

quasi meadow
#

what i already knew before

mystic sundial
#

What's that

#

Nothing

quasi meadow
#

?

#

what do you mean nothing

mystic sundial
#

Ok first question

#

Why is it printing

quasi meadow
#

line 5.

mystic sundial
#

Ok

#

Do you know what ... Is

quasi meadow
#

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.

mystic sundial
#

Think of ... Like a list of values

#

A little like a table

#

Change ... To a, b, c

#

And print a,b,c

quasi meadow
mystic sundial
#

Yes

quasi meadow
#

actually nvm

#

itwont print anything at all

#

but thats because i need to update the function(...)

mystic sundial
#

Yes

quasi meadow
#

before I look at studio

#

Im going to guess it will look like

#

{5} 8 nil

mystic sundial
#

Ok so what's happening

quasi meadow
#

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

mystic sundial
#

Ok now do

#

Mytable + "hello"

#

Or maybe that won't work

quasi meadow
#

but we haven't made a __concat?

mystic sundial
#

Concat is ..

#

Not +

quasi meadow
#

e

#

oh

#

i was not expecting that

mystic sundial
#

It worked

quasi meadow
#

i mean i guess it's because nothign is being added to the table just yet so it still does make logical sense

mystic sundial
#

Ok so we know that if we do
Mytable + anyvalue

#

It will call the function inside the metastable

quasi meadow
#

hmm

mystic sundial
#

And that function will tell us the table and the value we tried to add

quasi meadow
#

I see.

#

So you want to check if b == 5 then add 1 to value then return value?

mystic sundial
#

So how could we make it so that if we add 5 it adds 1 to value

#

We don't have to return anything

quasi meadow
#

oh

#

is the first part correct at least?

mystic sundial
#

local newValue = mytable + 5

#

What do you think newvalue will be

quasi meadow
#

{} 5 ?

mystic sundial
#

Nope

quasi meadow
mystic sundial
#

It's nil

quasi meadow
#

yes..

#

I wonder why it is nil

mystic sundial
#

Ok now return a random value inside the function

#

For instance return "bob"

quasi meadow
#

nil

#

wait a sec

mystic sundial
#

Nope

quasi meadow
#

it doesnt return anything

mystic sundial
#

Mytable + anyvalue should now be equal to bob

#

Print(Mytable + anyvalue) will print bob

quasi meadow
#

ah

#

i see

mystic sundial
#

Not +=

quasi meadow
#

yeah i got that bit.

#

I got the error after I changed it to only +

mystic sundial
#

Also

quasi meadow
mystic sundial
#

You did += above

quasi meadow
#

ahhhhhh

#

okay

#

I see.

mystic sundial
#

That will set my table to bob

quasi meadow
#

I think this is why

#

but

#

i think its because it's not followed by anything

mystic sundial
#

You can ignore the error

quasi meadow
#

the print is fine

#

yes it prints bob :)

mystic sundial
#

What you do mytable += 5 it's like doing mytable = mytable + 5

quasi meadow
#

yes

mystic sundial
#

Ok

quasi meadow
#

I believe I was just trying to finish the statement so I found += worked

#

but print(myTable + 5) works as you were saying

mystic sundial
#

So hopefully if you followed along and understood everything you should be at 80% understanding

quasi meadow
#

Yeah.

#

So far I think so.

mystic sundial
#

So you can see why metatables are very powerful and can do many things

quasi meadow
#

I can't see it just yet unfortunately

#

But I can try to imagine systems I might use for it..

mystic sundial
#

When you do

quasi meadow
#

Like maybe an economy system?

mystic sundial
#

Vector3 + vector3

#

Or when you do vector3 * 2

#

Now you know how Roblox makes that work

quasi meadow
#

hhahaaha

#

ahaha

#

Vector3.new(Vector3.X * 2, Vector3.Y * 2, Vector3.Z * 2)

mystic sundial
#

Vector3 is just a table with a metatable

quasi meadow
#

/jk

#

Im going to try and research __index now

#

😃

mystic sundial
#

Maybe if you watch my video a second time it might be easier to understand now

quasi meadow
#

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

woeful zodiac
#

I’d say matrices are over complicating things tbh (as a non mather)

quasi meadow
#

one might say metatables are more like "tables with functions inside of them" xD

#

as a comparison

quasi meadow
#

I cant think of a use just yet which is unfortunate

#

i retain information better when I can apply it

woeful zodiac
#

Proxy tables ig

quasi meadow
#

but ty father the hairy balls have bestowed on me divine vision and have helped me immensely @mystic sundial

woeful zodiac
#

😧

quasi meadow
quasi meadow
#

gotcha

#

i see

#

i think

#

:)

#

I guess its an anticheat thing?

woeful zodiac
#

No

#

The term proxy should explain alot

quasi meadow
#

i mean i guess

acoustic river
#

basically metatables is this

quasi meadow
#

ah i see

#

brilliant

acoustic river
#

yup

acoustic river
#

like part.Touched

#

similarly table.__'metamethod here' will fire

#

👍

quasi meadow
#

suphi hand held me threw it

#

patreon benefits