Meta-table (Lua class)

Lua Object-Oriented Tutorial – Complete Guide

In Lua, there’s no built-in “object” type like in Java or Python, but you can simulate objects using tables (Lua’s only data structure) plus metatables. - ChatGPT

Basic Table as an Object

-- Simple object-like table
Person = {
    name = "Alice",
    age = 25
}

function Person:greet()
    print("Hi, my name is " .. self.name)
end

Person:greet()  -- Hi, my name is Alice

Notice the : operator – it’s shorthand for passing self as the first argument.

Class

Constructor Pattern

-- 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

-- Create an instance
local vec = Vector2.new(3, 4)

Methods

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

Instantiation and Method Calling in Lua OOP

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

Inheritance

-- 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

see also

Written on November 14, 2023, Last update on November 14, 2023
lua