Its an alternate way to do OOP, without the need for metatables.
The metatable approach would look something like this:
[lua]-- Player.lua
local Player = {}
local mt = {__index = Player}
function Player:new(name)
local player = {
name = name
}
setmetatable(player, mt)
return player
end
function Player:speak()
print(“av’ at you!”)
end
function Player:attack(target)
print(self.name … " attacks " … target.name)
end
return Player[/lua]
declaring member variables in the classes is done with self all the time?
Yes, lua doesn’t has real Classes and OOP. So trying to do:
[lua]function Player:attack(target)
print(name … " attacks " … target.name)
end[/lua]
(note the missing self.name) is not possible.
This link explains the use of self quite well: http://lua-users.org/wiki/ObjectOrientationTutorial [import]uid: 118390 topic_id: 24987 reply_id: 101609[/import]