add ipairs and numeric indexing support to vector3

This commit is contained in:
Lera Elvoé 2025-03-03 13:41:38 +03:00
parent b25e41bd49
commit 85df2fb243
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc

View File

@ -169,8 +169,25 @@ function Vector3:__le(b)
return self.x <= other.x and self.y <= other.y and self.z <= other.z
end
Vector3.__index = Vector3
local NUMK = {"x", "y", "z"}
function Vector3:__index(key)
-- this allows constructs like `for i, v in ipairs(Vector3(3, 2, 1))` to iterate over components
if type(key) == "number" then
return rawget(self, NUMK[key])
end
return rawget(Vector3, key)
end
function Vector3:__newindex(key, value)
if NUMK[key] then
rawset(self, NUMK[key], value)
return
end
rawset(self, key, value)
end
--------API--------
function Vector3:length_squared()