Hi, I’m making a voxel game. I need to get a part and be able to see its closest neighbors. My solution was to put it into a dictionary. Is there a more efficient way to go about this or make it more optimized? Currently, it takes around 100ms for 60k parts.
Code:
local directions = {
["Left"] = Vector3.new(0,GRID_SIZE,0),
["Right"] = Vector3.new(0,-GRID_SIZE,0),
["Forward"] = Vector3.new(0,0,GRID_SIZE),
["Backward"] = Vector3.new(0,0,-GRID_SIZE),
["Up"] = Vector3.new(0,GRID_SIZE,0),
["Down"] = Vector3.new(0,-GRID_SIZE,0)
}
for i,v in pairs(game.Workspace.Map:GetChildren()) do
neighbors[v.Position] = {
["self"] = v,
}
for direction, vector in pairs(directions) do
neighbors[v.Position][direction] = neighbors[v.Position + vector] and v.Position + vector or false
if neighbors[v.Position][direction] then
local updateLeaf = neighbors[neighbors[v.Position][direction]]
for j,k in pairs(directions) do
if k == -vector then
neighbors[neighbors[v.Position][direction]][k] = v.Position
end
end
end
end
end
