Meta-table (Lua class)

Lua Object-Oriented Tutorial – Complete Guide

Creating Classes using Metatables in Lua

-- Creating a class
Vector2 = {}
Vector2.__index = Vector2
function Vector2.new(x, y)
  local self = setmetatable({}, Vector2)
  self.x = x
  self.y = y
  return self
end

Methods in Lua OOP

function Vector2:magnitude()
  return math.sqrt(self.x * self.x + self.y * self.y)
end

Instantiation and Method Calling in Lua OOP

-- Create an instance
local vec = Vector2.new(3, 4)
-- Call a method
print(vec:magnitude())  --prints 5

Inheritance in Lua

-- Creating a new class based on 'Vector2'
Vector3 = setmetatable({}, Vector2)
Vector3.__index = Vector3
function Vector3.new(x, y, z)
 local self = setmetatable(Vector2.new(x, y), Vector3)
 self.z = z or 0
 return self
end
-- Overriding the 'magnitude' method for 'Vector3'
function Vector3:magnitude()
 return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
end
Written on November 14, 2023, Last update on November 14, 2023
lua