Short Answer:
self:myFunction()
is the short form of
myClass.myFunction(self)
Long Answer:
When dealing with objects, you need something to tell you which object your are referring to (A pointer). In languages like C++ that is called ‘this’. In Lua, it is called ‘self’. Lets say you had a Box Class, and a couple of methods. (Lua is flexible, and you can code in different styles, this is just to convey the idea.)
Class Box = {}
function Box.create(x,y)
local self = {}
self.x = x
self.y = y
.
.
.
return self
end
function Box.update(self, deltaX, deltaY)
self.x = self.x + deltaX
self.y = self.y + deltaY
end
To create 2 boxes you would:
box1 = Box.create()
box2 = Box.create()
now to update, you can
Box.update(box1,1,0)
Box.update(box2,0,1)
or
box1:update(1,0)
box2:update(0,1)
It is just a syntax shorthand, (Lua calls it syntactic sugar) the pointer is still being passed, you just do not see it.
[import]uid: 41667 topic_id: 11658 reply_id: 42425[/import]