I have had a really hard time learning metatables, could someone plz explain them to me
could someone better explain metatables to me and how I could use them?
1 answer
Metatables
Disclaimer: I disagree mostly with a lot of the blog posts because I think that a lot of them are poorly written or incomplete resources.
Metatables are a special type of table which define how objects behave through special callbacks called metamethods. In Lua, all values can technically have a metatable, however on Roblox you're only ever going to be concerned with tables having metatables.
Setting a metatable
Setting a metatable is done with setmetatable, which takes a source table and a metatable and applies the metatable to the source table. If you like short code, it also returns the table which it applied the metatable to.
local newTable = setmetatable({}, {});
Getting a metatable
Sometimes you might want to get the metatable from an object. This works on anything which has a metatable, but it will return the value of __metatable from the attached metatable if it exists.
local metatable = getmetatable(newTable);
Using metamethods
Metamethods are where all the magic happens. There are specific metamethods for specific things, some of which will not work in some cases. I'm going to cover the basic ones, because they're cool.
__index
__index tells the table what to do when there is no value for a key. If, for example, you decided to do newTable.nonExistantKey, __index would be invoked in order to try and give it a value. __index may be one of two things:
- table
When a table is used, anynilkeys will 'fall back' onto the given table, which happens recursively. This can be, and often is, used to create classes in Lua as it provides a convenient way to manage inheritance. - function
A function will be used similarly to a table, however it is called with two arguments: The table which is being accessed, and the key which is being accessed. This can be used for debugging, or more complex inheritance. The value which the function returns will be treated as the value for the given key.
__newindex
This metamethod tells the table what to do when you change a key. But only if the key had no value before. This metamethod may only be a function, and it is fired with three arguments: The table which is being changed, the key being changed, and the new value. This has applications in things such as read-only tables however it may be bypassed with rawset
__call
Fired when the table is called as though it were a function, this metamethod may also only be a function and has a variable number of parameters. The first parameter is always the table being called, and every parameter after that relates directly to every argument it is being called with. All return values are returned as with a normal function.
See more: Lua-users Wiki: Metatables
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
https://scriptinghelpers.org/blog/metatables-a-table-within-a-table-maybe-not