Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

Need Guidance with Metatables and Metamethods?? [closed]

Asked by 5 years ago

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?)

0
I'm writing an answer for you. This is an amazing question. +1. User#24403 69 — 5y
0
Funny meeting you here, horrible_pun. EpicMetatableMoment 1444 — 5y
0
sinister i see you everywhere i go Horrible_Pun 27 — 5y

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?

1 answer

Log in to vote
11
Answered by 5 years ago
Edited 4 years ago

Metatables

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.


Metamethods

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.

Note: The keys must start with two underscores and named exactly how they are documented

__index

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

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

As a function

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.

This does not cover everything about metatables and metamethods. You can learn more on them here and here.


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
3
holy crooks thank you lord inca Horrible_Pun 27 — 5y
Ad