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

In what cases is a metatable useful?

Asked by 9 years ago

The name says it all. I'm going into metatables, but at the moment, I see no use for it.

1
Im with you, but I think they are good for indexing arrays and tabes, but you can already do this. User#5978 25 — 9y

2 answers

Log in to vote
1
Answered by
funyun 958 Moderation Voter
9 years ago

Quick review: A metatable can be applied to tables to give them more functionality. Special keys called metamethods are added to the metatable to monitor, say, when you're trying to access something that doesn't exist in the table, or assigning new keys to the table. The most commonly used metamethods are:

__index: Controls what happens if you try to access something that's not in the table. This could be either a function or a table. Examples:

--Boring example 1

t = {
    a = 1,
    b = 2,
    c = 3
}

mt = {
    __index = function(self, key) --Where "self" is the table, and the key isn't actually in that table.
        print(key.." isn't in the table!")
    end
}

setmetatable(t, mt)

a = t.a
b = t.b
c = t.c
d = t.d --Try to access "d", which isn't in the table
--Boring example 2

t = setmetatable({a = 1, b = 2, c = 3}, {
    __index = {d = 4}
})

print(t.d) --"d" is not in the table, but it's in the metatable's __index, so this works.
--Fun example: Adding properties to parts??

template = Instance.new("Part") --For getting the part's properties
properties = {hacker = "funyun", secretmessage = "hacked lol"} --New properties for the part
part = setmetatable(properties, {__index = template}) --If it's not in the new properties, it's in the template

part.Parent = workspace
part.BrickColor = BrickColor.Red()
print(part.hacker)
print(part.secretmessage)

There's also __newindex for monitoring when a new thing is added to the table. Examples on that:

--Boring example 1

t = {a = 1, b = 2, c = 3}
mt = {
    __newindex = function(self, key, value) --Self = t, key = what you're putting in the table, value = what you set it to
        print("I can't let you do that.")
    end
}

setmetatable(t, mt)

t.d = 4
print(t.d)
--Boring example 2

t = {a = 1, b = 2, c = 3}
mt = {
    __newindex = function(self, key, value)
        rawset(self, key, value * 10) --Since doing "self.key = value * 10" would trigger __newindex again, I used rawset.
    end
}

setmetatable(t, mt)

t.d = 4
print(t.d)

Note how often you use tables in Roblox Lua. We use them for all kinds of stuff. Therefore, it's good to know about these metatables and their metamethods.

Ad
Log in to vote
0
Answered by 9 years ago

Answer

I never use metatables but what they do is that it's basically a function attached to a array/table that fires if you call the certain parameter/listener for the table.

http://wiki.roblox.com/index.php?title=Metatable

0
Actually it's a table of functions attached to a table with specific events called "metamethods". LuaQuest 450 — 9y
0
Which is what I said more easily explained. User#5978 25 — 9y

Answer this question