Metatables, metamethods, and the colon syntax are all inter-related. Allow me to quickly explain their relationships.
Let's say you have some entity. This entity is called Object. And on this entity, Object, you store a function that will color it pink. Such a function would look like this.
Code:
function Object.ColorMePink(myself)
myself:SetColor(255,255,0,255)
end
Now, as you go along, you realize that it becomes a bit tiredsome to do
Object.ColorMePink(Object) all the time. You think to yourself "Well, this function is stored on Object, and I only ever use it to affect Object. It'd be a lot quicker if I could just call
Object.ColorMePink(), and, since it's stored on Object, it'll automatically assume that I mean to do
Object.ColorMePink(Object).
And thusly, you add the colon syntax. Now instead of having to do
Object.ColorMePink(Object) every time you want to color Object pink, you can now simply do
Object:ColorMePink().
Put another way,
Object.ColorMePink(Object) and
Object:ColorMePink() do the exact same thing.
By extension, you can inter-related library.function and library:function syntax in such a way, regardless of the library or function, as shown by the relationship below.
Code:
-- This function
function library.SomeFunction(self)
DoSomething(self)
end
-- Is the same as this function
function library:SomeFunction()
DoSomething(self)
end
When you use metamethods by using ":", you are effectively calling that same function using ".", but Lua automatically throws in a first argument to that function. This argument is assigned the variable of "self," and will always refer to the entity that is calling it.
By extension, with this function:
Code:
function Object.SomeFunction(self)
DoSomething(self)
end
These operate exactly the same in calling it:
Code:
Code:
Object.SomeFunction(Object)
Object:SomeFunction()
And it works with functions with more than one argument. With this function:
Code:
function Object.SomeFunction(self,a,b,c)
DoSomething(a,b,c)
end
You can do either of these methods:
Object.SomeFunction(Object,1,2,3) or
Object:SomeFunction(1,2,3). They are, on an engine level, identical.
Much like you can call these functions interchangeably, so can you define them. These are all proper ways to define a function, and all three are identical in operation:
Code:
Object.SomeFunction = function(self,a,b,c)
DoSomething(self,a,b,c)
end
function Object.SomeFunction(self,a,b,c)
DoSomething(self,a,b,c)
end
function Object:SomeFunction(a,b,c)
DoSomething(self,a,b,c)
end
So effectively, metatables, metamethods, and the colon operator are all inter-related, and are simply a convenience method. Metatables and metamethods also extend into the fact that GLua is largely an Object-Oriented Language (an OOL), as opposed to a Non-OOL.
I hope this cleared some things up.