Hey there. I just wanted to ask which one of these ways is better for my use case.
I found a solution online to use a special trick to have a multidimensional array by useing “fake” arrays
I am trying to create a fluid simulation in Roblox and I would like to know what your thoughts are on which one would be better to use for my case. I don’t plan on having the fluid exit the simulation area It will change a lot of how I will program the stuff so if you could choose that would be great
-- Trick way
local function IndexX(Position: Vector3, Size: Vector3)
return ((Position.X) + (Position.Y) * Size.X + (Position.Z) * Size.X * Size.Y)
end
-- Normal way
local function Create3DArray(Size: Vector3, DefaultValue: any)
local Array_3D = {}
for x = 1, Size.X do
Array_3D[x] = {}
for y = 1, Size.Y do
Array_3D[x][y] = {}
for z = 1, Size.Z do
Array_3D[x][y][z] = DefaultValue
end
end
end
return Array_3D
end
-- Normal
local 3dArray = Create3DArray(Vector3.new(2,2,2),0)
3dArray[2][1][2] = 10
--[[
{
{{1, 1},
{1, 1},
{1, 1}},
{{1, 10},
{1, 1},
{1, 1}}
}
]]
-- Trick
local Array = {}
Array[IndexX(Vector3.new(2,1,2),Vector3.new(2,2,2] = 10
-- {1,1,1,1,1,1,1,10,1,1,1, 1}
Does it make a difference? Is there a better way to do this? I will be indexing this A LOT during the frame so it would be nice to know which on is better for performance.