From 85df2fb24383f38aca95cd6ef3c6cdc6e654a9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 3 Mar 2025 13:41:38 +0300 Subject: [PATCH] add ipairs and numeric indexing support to vector3 --- data/scripts/types/vector3.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/data/scripts/types/vector3.lua b/data/scripts/types/vector3.lua index f203501..d6ae546 100644 --- a/data/scripts/types/vector3.lua +++ b/data/scripts/types/vector3.lua @@ -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()