Enhancement: Access to (meta)methods for Vector2 and Vector3 types · Issue #32 · TSnake41/raylib-lua · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ffi.metatype assigns to all ffi-types one single global ffi-metatable (that is used for any metatype object):
localvec2=rl.new("Vector2")
localVector2_mt=getmetatable(vec2)
--> "ffi"localVector2_mt=debug.getmetatable(vec2)
localVector3_mt=debug.getmetatable(rl.new("Vector3"))
-- this is single global metatable for every metatype object that calls selects metamethod dependent on object type.print(Vector2_mt==Vector3_mt) --> true
So, we can't safely add something even with debug.getmetatable.
Possible fix.
/compat.lua:
localVector2_mt= {
__add=function(...) ...end,
...
}
-- add this little thingVector2_mt.__index=Vector2_mt-- looping but okayffi.metatype("Vector2", Vector2_mt)
Now we can take any Vector2, get it's index and fill it with new methods (or even metamethods):
localVec2=rl.new("Vector2")
localVector2_mt=Vec2.__indexVector2_mt.Negate=rl.Vector2NegateVector2_mt.__pow=function(self, n)
returnrl.new("Vector2", self.x^n, self.y^n)
end-- Can be used everywherelocalOtherVec2=rl.new("Vector2", 10, 20)
print(OtherVec2:Negate()) --> Vector2 (-100 -200)print(OtherVec2^2) --> Vector2 (100 400)
The text was updated successfully, but these errors were encountered:
HDPLocust
changed the title
Add default __index field for Vector2 and Vector3 types
Enhancement: Access to (meta)methods for Vector2 and Vector3 types
May 1, 2025
ffi.metatype
assigns to all ffi-types one single global ffi-metatable (that is used forany
metatype object):So, we can't safely add something even with
debug.getmetatable
.Possible fix.
/compat.lua:
Now we can take any Vector2, get it's index and fill it with new methods (or even metamethods):
The text was updated successfully, but these errors were encountered: