Lesson 6 of 8
OOP & Metatables
Lua has no classes. Instead it has metatables, which change how tables behave. Learn how to use them to build objects with and to overload operators.
A metatable customizes what happens on events like 'key not found' (__index) or 'table + table' (__add). The classic class pattern: put methods on a table, and point ' __index at it — missing lookups fall through to the prototype.
local Player = {}
Player.__index = Player
function Player.new(name, hp)
local self = setmetatable({}, Player)
self.name = name
self.hp = hp
return self
end
-- Colon DEFINES a method with an implicit 'self' parameter:
function Player:takeDamage(amount)
self.hp = self.hp - amount
if self.hp <= 0 then
print(self.name .. " is down!")
end
end
function Player:heal(amount)
self.hp = math.min(100, self.hp + amount)
end
local hero = Player.new("Ada", 100)
hero:takeDamage(30) -- colon CALLS passing hero as self
print(hero.hp) -- 70-- Operator overloading via metamethods:
local Vec = {}
Vec.__index = Vec
function Vec.new(x, y)
return setmetatable({ x = x, y = y }, Vec)
end
Vec.__add = function (a, b)
return Vec.new(a.x + b.x, a.y + b.y)
end
Vec.__tostring = function (v)
return "(" .. v.x .. ", " .. v.y .. ")"
end
local pos = Vec.new(1, 2) + Vec.new(3, 4)
print(pos) -- (4, 6)Finished reading?Mark it done to track your progress.
Tip: use the ← and → keys to move between lessons.