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

Metatables : A table within a Table?

Asked by
woodengop 1134 Moderation Voter
9 years ago

I did look up on the ROBLOX Wiki on Metatables and they didn't give enough for me to understand, Help would be appreciated!

0
Metatables are one of those things that you don't need learn unless you're actually going to use them (In sandboxing, for example) MechaScripter 35 — 9y

2 answers

Log in to vote
3
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

What Are Metatables?

I'm sure you've heard the term 'metatable' plenty of times, but do you actually know what they are?

A metatable is basically a table that can be attatched to another table to make it more powerful! Why metatables can make tables more powerful is because you can assign metatables things called metamethods. Metemethods are sort of like events, but the roblox events you know are instance - specific. Metamethods are things that are table - specific! Things like whenever the + operator is used on a table, or when a new index is added.

Some useful metamethods are;

  • __newindex

    Fires whenever tab[index] is attempted to be set with the = operator. Does not fire with the table.insert function, or rawset.

  • __index

    Fires whenever tab[index] is attempted to be indexed, to get the data. Does not fire with the usage of rawget


How Do I Make A Metatable?

You can create a metatable with the setmetatable function. This function takes in two arguments, the first being the table you're setting the metatable to and the second being the metatable you're setting to the table!

local tab = {}
local meta = {}

setmetatable(tab,meta)

meta is now a metatable for tab. Note: 'meta' will still be a variable in the environment, setmetatable doesn't change that.

The setmetatable also returns the table you're setting the metatable to so, you can also do this if you care about line count(:

local meta = {}
local tab = setmetatable({},meta)

Note: You may only add up to 1 metatable per table, attempting to add another will result in no action; the additional metatable you're trying to add will not be added.


How Do I Get Metatables?

Alternatively, if you're trying to reach previously set metatables to per say add more data to them, or even add more metatables to your metatables.. then you would use the getmetatable function.

The getmetatable function is similiar to the setmetatable function.. except you're getting, rather than setting(:

The first and only argument being the table you're aquiring it's metatable from. This function will return nil if the table doesn't have a metatable, It will return the __metatable value if that metamethod is there. Otherwise it will return the metatable.

If you don't want the __metatable metamethod being called then you need to use rawget.


How Do I Add Metamethods To Metatables?

Before trying to add metamethods to your metatable, you should see a full list of them here.

To add a metamethod to your metatable, you need to define the function to add to the metamethod. It's like an event, you assign functions to them so when they're fired they will do the specified code.

So, if we wanted to print 'New Index' everytime the __newindex metamethod was called then we would do like so;

Note: The first value returned from metamethods is always the table the metamethod was indexed in. Any other thing is the custom values for each metamethod.

local tab = {}
local meta = {
    __newindex = function(self,i,v) --'i' and 'v' are index and value
        print("New value '"..tostring(v).."' added at "..i..")
    end
}

setmetatable(tab,meta)

Now, when we go like this;

tab[3] = 'Hello'

Then it would print: New value 'Hello' added at 3.


A last note is that metatables aren't necessarily used for practical uses. But if you keep learning and get to know them a little more you'll know how fun they are to mess around / challenge yourself with.

Ad
Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

A cursory google search will actually give you a few resources on SH itself.

If you have any specific metatable questions, feel free to ask more specific questions.

Edit:

I will say, though, that metatables are not 'Tables within Tables'. They are Tables about Tables; that is to say, they modify the behavior or Tables in Lua. 'Tables within Tables' are simply nested Tables.

Answer this question