ooplualove2dmetatablemeta-method

Lua - trying to create a good Vec2 class


I'm learning how to use Lua and Love2d and I want to create a Vec2 class using metamethods and metatables. This is what I have so far:

class.lua: (The base class files)

local Class = {}
Class.__index = Class

-- Constructor
function Class:new() end

-- Inherite from Class
-- type = The name of the new class
function Class:derive(type)
  print("Class:", self)
  local cls = {}
  cls["__call"] = Class.__call
  cls.type = type
  cls.__index = cls
  cls.super = self
  setmetatable(cls, self)
  return cls
end

function Class:__call(...)
  local inst = setmetatable({}, self)
  inst:new(...)
  return inst
end

function Class:getType()
  return self.type
end

return Class

vec2.lua

local class = require "class"
local Vec2 = class:derive("Vec2")

function Vec2:new(x, y)
  self.x = x or 0
  self.y = y or 0
  getmetatable(self).__add = Vec2.add
end

function Vec2.add(a, b)
  local nx, ny
  nx = a.x + b.x
  ny = a.y + b.y
  return Vec2:new(nx, ny)
end

return Vec2

and in my main.lua I have:

local v1 = Vec2:new(10, 10)
local v2 = Vec2:new(5, 3)
local v3 = v1 + v2
print("v3:", v3.x, v3.y)

and I get this error:

Error: main.lua:12: attempt to perform arithmetic on local 'v1' (a nil value)


Solution

  • Vec2.new does not return a value. Hence the assignment local v1 = Vec2:new(10,10) results in v1 being nil

    Try local v1 = Vec2(10,10) instead. Same mistake in Vec2.add

    The instance is created in the __call metamethod which calls new with your parameters. You're not supposed to call new directly unless you want to re-initialize an existing instance.

    function Class:__call(...)
      local inst = setmetatable({}, self)
      inst:new(...)
      return inst
    end