How get "Path" from nD array?

Hello. I’m making custom pathfinding. For now, I’m stuck with path, which declared in nD array (explanation of nD: if path have 5 ceils, it will be 5D: Path[Vector1][Vector2][Vector3][Vector4][Vector5]). And with this nD array I got big problem: How I can write that all, to write another 1 VectorN to the end?

What my “Path” system looks like:

I tried make smth like:

local Path = Pathes -- "Pathes" is start of that giant nD array
for i = 1, #CurrentPath, 1 do -- "CurrentPath" is array which stores all Points in the current Path.
	Path = Path[CurrentPath[i]]
end

but this won’t give me actual end of array, just “copy” of it.
Can someone help me with my problem, please?

Switch out your code to this:

local Path = Pathes -- "Pathes" is start of that giant nD array
for i = 1, #CurrentPath, 1 do -- "CurrentPath" is array which stores all Points in the current Path.
	Path = Path[i]
end

I can’t, because CurrentPath have variables like:
{Vector3.new(1,1,1), Vector3.new(2,1,1), Vector3.new(3,1,2), Vector3.new(3,1,3), Vector3.new(3,2,4)}

So why can’t you just index it directly?

local Path = Pathes -- "Pathes" is start of that giant nD array
for i = 1, #CurrentPath, 1 do -- "CurrentPath" is array which stores all Points in the current Path.
	Path = CurrentPath[i]
end

I can’t index them like this, because Current path have only waypoints. Path have smt like this:

Pathes = {
    Vector3.new(0,0,0) = {
        Vector3.new(1,0,0) = {
             Vector3.new(1,0,1) = {}
        },
        Vector3.new(0,0,1) = {
            Vector3.new(0,0,2) = {
                Vector3.new(1,0,2) = {}
                Vector3.new(0,0,3) = {
                    Vector3.new(1,0,3) = {}
                }
            }
        }
    }
}

And if I just index “Path” with 1 waypoint, I won’t be able to add new waypoint to the end, for example to Pathes[Vector3.new(0,0,0)][Vector3.new(0,0,1)][Vector3.new(0,0,2)][Vector3.new(0,0,3)]

Can’t you use an array?

Paths = {
    {
        v1, v2, v3, { v11, v12, v13 }
    }
}

etc etc? Why is it structured like this anyway? Why can’t you just have;

local Path = {
    Waypoint1,
    Waypoint2,
    Waypoint3
}

for _, Waypoint in ipairs(Path) do
    GoTo(Waypoint)
end

?

Edit: Ok wait, this looks more like a node graph than a path.

--Node: {<Position>, {<indexes of connected nodes>}}

local Nodes = {
    Node1 = {Position1, {2}}, --connected to node 2
    Node2 = {Position2, {1}} --connected to node 1
}
1 Like