Understanding Metatables (Again x1001.)

Yes, i will have to come up with this question, because i literally cant find an actual solution to understand metatables, please don’t warn me for “duping topics”, i really want to learn it, as i want to have more and more knowledge on scripting.

I want to know a variety of things, such as what _index does? do we necessarily need to put 2 tables together in setmetatable(table_a,table_b)? what the underscore will do and etc.

An Search

1 Like

Well i’ll Answer a question. It’s 2 underscores (__) and having the underscores doesn’t really have to do with anything. It’s just that the designers of Liam decided to do that

__index allows u to have a fall trough table. If you try to do someTable.nilvalue what Lua does is it searches in your __index metamethod to see if it exists in there. It can also be a function too, that’s called when something is a nil value

So __index is like an GetChildren() on an table? (Aka it returns an array.)

Metatables and metamethods, a metatable is just a table with metamethods in them

metamethods are like __index etc. you can think of them like events “Part.Touched”
the underscores is just syntax it doesnt really serve anything

every table can be attached with a metatable you can do this by
setmetatable(sometable,metatable)

there are several different metamethods and you can sort of connect them to functions (again think of metamethods kinda like events or like callback functions)

local metatable = {
    __index = function(Tbl,Index) print(Index) end
}

local sometable = {}
setmetatable(sometable,metatable)
local x = sometable.A -- this will fire the __index metamethod causing it to run the function and print A

keep in mind that you do NOT need metatables to code efficiently or effectively nor do you need metatables to program in OOP styles, I recommend to express why you feel you need to learn metatables

No. When u do sometable.doesnotexist it searches inside __index and see if it exists In there. Or if it’s a function, call that function

There are also lots of other meta methods like __newindex Or __call which allows a table to act as a function

Then what __index is for? How will i know what is “inside” __index? Will i have to “insert” something in the “__index”?

It can either be a table or a function. U assign a metatable to a table using setmetatable()

__index is used for OOP

What is

When the “value” of that was set?/What is its “value”?

@Jaycbee05, have you seriously been typing/been here 30 minutes? Well, i will wait for your response regardings my question.(About metatables.)

1 Like

Tbl is the table it is referencing
i didn’t write value anywhere but im assuming your talking about index, index is what i was trying to index at the last line

you probably need to reread on how tables work

Well, i was going write more but other people had beat me to it, and i had restarted my response quite a few times /: . Essentially metatables are simply tables that hold metamethods, these metamethods, which are quite “powerful” and allow for pretty cool stuff are fired when certain actions are preformed with an associated table. __index is fired when the associated table is indexed, sometimes like Table[key]. You can give a table a metatable using setmetatable, also note that you can also set a table’s metatable to itself , which would allow you to put everything in one table.

But for a better explanation and a more in depth look if you haven’t already i would recommend reading this resource:


Also, Although metamethods are great to learn, don’t force yourself to learn them, i’m sure you’ll definitely find use-cases for them, when you need them.

4 Likes

How can we use __index to detect when the associated table is indexed?

@Thedagz already gave an example, but basically all you do is set __index to a function (within a metatable):

 local MainTable = setmetatable({},{ 
   __index = function(Table, index) 
       print(index.." Was indexed")
end})

local Value = MainTable[5] --fires the __index metamethod 

Also, one thing to note when setting __index to a function is that you should be careful indexing it’s associated table within it, because without some type of check, the stack will likely overflow (it will throw a c stack error)

So for example, this will run recursively until the stack is overflowed:

local MainTable = {}

setmetatable(MainTable ,{
   __index = function(Table, index) 
     local Badindexing = MainTable[index] ---__index being fired again..
end})

local Value = MainTable[5] --fires the __index metamethod 

1 Like

This community post has really helped me personally in learning metatables, and I really recommend you give it a read. It’s long, but quite worth the read!

2 Likes