add ipairs and numeric indexing support to vector3
This commit is contained in:
parent
b25e41bd49
commit
85df2fb243
@ -169,8 +169,25 @@ function Vector3:__le(b)
|
|||||||
return self.x <= other.x and self.y <= other.y and self.z <= other.z
|
return self.x <= other.x and self.y <= other.y and self.z <= other.z
|
||||||
end
|
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--------
|
--------API--------
|
||||||
|
|
||||||
function Vector3:length_squared()
|
function Vector3:length_squared()
|
||||||
|
Loading…
Reference in New Issue
Block a user