Adding 2 Vector's metatables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

Adding 2 Vector's metatables

Post by Lovingsoul1337 »

Hey there ! :)

Can someone tell me why my Vector Class dont work ? If i want add 2 Vector's together it say's i want to index a boolean value...

Code: Select all

local vector2D = {x, y}
vector2D.__index = vector2D

--Metafunctions

local vectorMeta = {
__call = function(a, b)
love.graphics.print("love")
end,
__add = function(a, b)
return vector2D(a.x + b.x, a.y + b.y)
end,
__sub = function(a, b)
return vector2D(a.b - b.x. a.y - b.y)
end
}

setmetatable(vector2D, vectorMeta)

--Constructor

function vector2D.new(x, y)
local self = {x = x or 0, y = y or 0}
setmetatable(self, vector2D)
return self
end

--Class functions

function vector2D:normalize(vector2D)
--do stuff
end
Thanks in advance ! :)
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Adding 2 Vector's metatables

Post by pgimeno »

Lovingsoul1337 wrote: Fri Apr 10, 2020 8:51 am Hey there ! :)

Can someone tell me why my Vector Class dont work ? If i want add 2 Vector's together it say's i want to index a boolean value...

Code: Select all

[...]

function vector2D.new(x, y)
local self = {x = x or 0, y = y or 0}
setmetatable(self, vector2D)
[...]
Thanks in advance ! :)
Shouldn't that be setmetatable(self, vectorMeta)?
Lovingsoul1337
Citizen
Posts: 53
Joined: Fri Feb 21, 2020 1:26 pm

Re: Adding 2 Vector's metatables

Post by Lovingsoul1337 »

I dont know but change it gives the same error...
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Adding 2 Vector's metatables

Post by 4vZEROv »

Code: Select all

    local vector2D = {}

    local mt = {}
    mt.__index = vector2D
    mt.__call = function(self, x, y) return vector2D.new(x, y) end
    mt.__add = function(a, b) return vector2D(a.x + b.x, a.y + b.y) end
    mt.__sub = function(a, b) return vector2D(a.x - b.x, a.y - b.y) end
    mt.__tostring = function(self) return self.x .. ", " .. self.y end

    setmetatable(vector2D, mt)

    function vector2D.new(x, y)
        local object = {}
        object.x = x or 0
        object.y = y or 0
        return setmetatable(object, mt)
    end

    local myVector = vector2D(5, 5) + vector2D(1, 3)
    print(myVector)
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Adding 2 Vector's metatables

Post by pgimeno »

Lovingsoul1337 wrote: Fri Apr 10, 2020 9:51 am I dont know but change it gives the same error...
You're setting the __call metamethod to a dummy function that just calls love.graphics.print("love"), and then using it as a constructor in the __add metamethod. Try setting it like this:

Code: Select all

function vectorMeta.__call(t, x, y)
  return vector2D.new(x, y)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 216 guests