Page 1 of 1

Need help creating equality metamethod

Posted: Thu Sep 06, 2018 2:43 pm
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 91 times
Vector2.lua
The vector class.
(159 Bytes) Downloaded 81 times

Re: Need help creating equality metamethod

Posted: Thu Sep 06, 2018 3:21 pm
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.

Re: Need help creating equality metamethod

Posted: Thu Sep 06, 2018 5:07 pm
by the_spice_must_flow
Thanks, this is the correct answer.