Forum

> > CS2D > Scripts > What is metatable?
Forums overviewCS2D overview Scripts overviewLog in to reply

English What is metatable?

8 replies
To the start Previous 1 Next To the start

old What is metatable?

Deleted User

Quote
Hey guys, I know about Lua pretty much, but the thing that I STILL can't understand is metatables. I don't know what is it and what it need to be used for. So can someone help me here? Please?

Also I saw a lot of tutorials and examples, but still can't understand (even on Russian).

old Re: What is metatable?

Avo
User Off Offline

Quote
I would to know the same thing, I understand theory, but I can't use it for anything.

old Re: What is metatable?

Yates
Reviewer Off Offline

Quote
Well, if nothing is found in t, then it will be looked for in t1. As given in the example.

It could be used for a backup, but you really wouldn't need it.

old Re: What is metatable?

Flacko
User Off Offline

Quote
Metatables define extra behaviour for what would be a regular table.
For example, let's say I've got a list of the users' names who posted in this thread:
1
t = {"Factis","Bolt","Yates","Flacko"}
Now, suppose I want to get a subset table, ranging from 1 to 3. That is {"Factis","Bolt","Yates"}. The most intuitive way that comes to mind is this:
1
t[1,3]
Sadly, it's not valid in Lua, although it could be in another language. Yet not all hope is lost.

This is when Lua metatables come to save the day. We still can't get the subset in the way I showed before, however, we could do it in a similar fashion:
1
t(1,3)
Of course, this is still not valid until we actually start working with metatables.
A metatable is just a regular table, it has no special traits or whatever.
1
subset_meta = {}
So there it is. Our metatable.
Of course, this isn't a metatable strictly speaking, as what makes a table a metatable is it's relation with another table (although a table can be metatable'd to itself).
Let's do it then:
1
setmetatable(t,subset_meta)
Alright, we've set up our metatable, however it doesn't do anything yet, so calling it will still throw an error.
We will define a metamethod in our metatable. What is a metamethod? Just like a metatable is just a table, a metamethod is just a function. To make a metamethod we have to define a function inside our metatable with one of the following names:
Quote
__unm --unary minus (-t)
__len --length (#t)
__sub --substraction (t-x)
__add --addition (t+x)
__mult --multiplication (t*x)
__div --division (t/x)
__pow --power (t^x)
__mod --modulo (t%x)
__concat --concatenation (t..x)
__eq --equality (t==x)
__lt --less than (t<x)
__le --less-equal (t<=x)
__index --index (t[x])
__newindex --new index (t[x] = y)
__call --calling (t(x,y,...))

There are some gotchas for some metamethods, for example, the comparison metamethods (__eq,__le,__lt) only work if both elements have the same metatable. Meaning that if t's metatable is subset_meta then x's metatable must be subset_meta too.
The __index and __newindex metamethods only work if the table index you're trying to access is not defined. Meaning that if t[x] isn't nil, the metamethods won't be called.

Let's get back to our example and define our __call metamethod once and for all:
1
2
3
4
5
6
7
subset_meta.__call = function(tbl,start,e)
	local r = {}
	for i=start,e do
		r[#r+1] = tbl[i]
	end
	return r
end
All metamethods take the affected table as their first parameter meaning that in our case, tbl will be t. The rest of the parameters are taken directly from the call to t.

So let's test our work:
1
2
3
for k,v in pairs(t(1,3)) do
	print(v)
end

Note that all this work could be packed into a separate function without using metatables:
1
2
3
4
5
6
function getsubset(tbl,start,e)
	--same code as in subset_meta.__call
end
for k,v in pairs(getsubset(t,1,3))
	print(v)
end
This, however, doesn't look as cool as with metatables.
edited 1×, last 23.07.12 11:39:58 am

old Re: What is metatable?

omg
User Off Offline

Quote
so basically u overload default signs? nothing i havent heard of in c++ lol. thanks for making it shorter to read
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview