Im a complete NOOB with metatables, its been one of those things where ive avoided learning, but now ive decided to tackle it. With this being said, i'd like to ask you guys to dumb it down for me and explain metatables and meta methods, I ask humbly. Now, I know what ur gonna say "just go to the wiki", I did but the people who write articles for the wiki must have a big ego or something because they make everything very over-complicated. Im not saying that because i dont understand any of it, alot of the things they over-complicate i understand myself(learned another way). Sorry if this thread/question seems disorganized, i havent used this site in quite a while(do i have to provide example code?)
A table can be used as a metatable of another table. Multiple tables can have the same metatable. A table's metatable may have a metatable itself. Even a table can be its own metatable!
You can use setmetatable(x, m)
to set m
to be x
's metatable.
You can use getmetatable(x)
to get x
's metatable. This will return nil
if x
has no metatable.
Think of this as attaching one table to another. You're attaching m
, the table you wish to be the metatable of x
, to x
. You'd store metamethods in the metatable.
Think of metamethods as ROBLOX events. For example, with a ClickDetector and its MouseClick
event
clickDetector.MouseClick:Connect(function(client) -- # ... end)
Something happens that will invoke a metamethod/fire an event.
Now, I am not saying ROBLOX events and metamethods are the exact same, I am merely using that as an abstraction to show you how it would work. I will be covering __index
metamethod in this answer. There are a lot more metamethods.
__index
is pretty simple. It gets invoked when you index x
for a nil value; something that does not exist in x
.
__index
can be set to a function or a table.
When __index
is set to a table, Lua will look up the index in the table.
local x = {} local m = { __index = {inTheMeta = "hello"} } setmetatable(x, m) -- # setmetatable returns x back, so you can do `local x = setmetatable({...}, m))` if you please. print(x.inTheMeta) --> hello
Lua first checks if inTheMeta
is in x
. If it is, produce the value. If not, check if x
has a metatable. If it does, look for __index
. If __index
is there, look up the key in the table. If the key exists, produce the value of the key. If no metatable or no __index
metamethod, produce nil
.
If __index
is set to a function, produce the return value of said function
local x = {} local m = { __index = function(t, k) return "__index invoked; x was indexed for " .. k .. " but the key didn't exist" end } setmetatable(x, m) print(x.inTheMeta) --> __index invoked; x was indexed for inTheMeta but the key didn't exist
Lua first checks if inTheMeta
is in x
. If it is, produce the value. If not, check if x
has a metatable. If it does, look for __index
. If it's there, produce the value that the __index
function returns.
t
is the table that m
is the metatable of; x
.k
is the key you indexed for.
Locked by User#24403, DinozCreates, ihatecars100, and Ziffixture
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?