Need help creating equality metamethod

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
User avatar
the_spice_must_flow
Prole
Posts: 13
Joined: Thu Jan 04, 2018 10:49 pm

Need help creating equality metamethod

Post by the_spice_must_flow »

Hi everyone,

I am using a file I found online to implement OOP in my game. This class file uses a metatable when defining a new class type.
I have a vector class that uses this and I am wondering how I can setup equality checking of two vectors together. I am not exactly keen on how metamethods work in order to figure this out on my own. Am I out of luck with the metatable being used already?
Class.lua
The oop module.
(1.84 KiB) Downloaded 100 times
Vector2.lua
The vector class.
(159 Bytes) Downloaded 88 times
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Need help creating equality metamethod

Post by grump »

Code: Select all

function Vec2:__eq(rhs)
	return self.x == rhs.x and self.y == rhs.y
end
Beware of floating point precision issues.
User avatar
the_spice_must_flow
Prole
Posts: 13
Joined: Thu Jan 04, 2018 10:49 pm

Re: Need help creating equality metamethod

Post by the_spice_must_flow »

Thanks, this is the correct answer.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 7 guests