Understanding your question a bit more now, here is a further adaption which maps all Vector3
objects with X
, Y
, and Z
values in the range [-180, 180] in steps of 90 to an integer. The integer they are mapped to ignores their signs, such that the integer mapped from Vector3.new(-90, -90, -90)
is the exact same as Vector3.new(90, 90, 90)
. Also, any mapped integer supplied returns a Vector3
object with X
, Y
, and Z
values in the range [0, 180] in steps of 90:
-- Helper function that, when given two Vector3 values, returns if both Vector3 values are equal.
local function vector3IsEqual(vectorOne, vectorTwo)
return vectorOne.X == vectorTwo.X
and vectorOne.Y == vectorTwo.Y
and vectorOne.Z == vectorTwo.Z;
end
-- Helper function that, when given a Vector3 value, returns a Vector3 where the X, Y, and Z
-- values are either 0 or positive.
local function vector3ToAbsoluteValues(vector)
local absX = math.abs(vector.X);
local absY = math.abs(vector.Y);
local absZ = math.abs(vector.Z);
return Vector3.new(absX, absY, absZ);
end
local intToVector3Map = (function()
local intToVector3Map = {};
-- For every possible Vector3 with X, Y, and Z values in the range [-180, 180] in steps of 90,
-- map the vector (value) to the integer (index).
for x = 0, 180, 90 do
for y = 0, 180, 90 do
for z = 0, 180, 90 do
local vector = Vector3.new(x, y, z);
intToVector3Map[#intToVector3Map + 1] = vector;
end
end
end
return intToVector3Map;
end)();
local vector3ToIntMap = (function()
local vector3ToIntMap = {};
-- For every possible Vector3 with X, Y, and Z values in the range [-180, 180] in steps of 90,
-- map the string representation of the vector (index) to an already mapped integer(value).
for x = -180, 180, 90 do
for y = -180, 180, 90 do
for z = -180, 180, 90 do
local vector = Vector3.new(x, y, z);
local vectorAsAbsolute = vector3ToAbsoluteValues(vector);
for intIndex, mappedVector in ipairs(intToVector3Map) do
if vector3IsEqual(vectorAsAbsolute, mappedVector) then
vector3ToIntMap[tostring(vector)] = intIndex;
end
end
end
end
end
return vector3ToIntMap;
end)();
-- Addapted to not require the use of __index; no longer can use raw Vector3 values.
-- Opted to use tostring; highly recommend this practice.
print(vector3ToIntMap[tostring(Vector3.new(0, 0, 0))]);
-- Prints "1" (tested in Studio!)
print(intToVector3Map[1]);
-- Prints "0, 0, 0" (tested in Studio!)
-- Proof that signs are ignored:
local intValueOne = vector3ToIntMap[tostring(Vector3.new(-90, 90, -90))];
local intValueTwo = vector3ToIntMap[tostring(Vector3.new(90, -90, 90))];
-- Expect this statement to be true.
print(intValueOne == intValueTwo);
-- Prints "true" (tested in Studio!)
local vector = intToVector3Map[intValueOne];
-- Expect this expression to be equal to Vector3.new(90, 90, 90)
print(vector);
-- Prints "90, 90, 90" (tested in Studio!)