Testing, CalculatePath(), and Reverse()+foreach vs. for() loop downwards

I did my own implementation of CalculatePath() first.
Once I followed the video lesson in which I added the course’s version, I did a bit of a hybrid by replacing the second part with the course version…

    private List<GridPosition> CalculatePath(PathNode endNode)
    {
        List<PathNode> pathNodeList = new();

        PathNode currentNode = endNode;

        do
        {
            pathNodeList.Add(currentNode);
            currentNode = currentNode.GetCameFromPathNode();
        }
        while (null != currentNode);

        pathNodeList.Reverse();

        List<GridPosition> gridPositionList = new();
        foreach (PathNode pathNode in pathNodeList)
        {
            gridPositionList.Add(pathNode.GridPosition);
        }

        return gridPositionList;
    }

So, at first instead of doing the pathNode.Reverse() I just looped over it backwards in a plain old for() loop, and adding the grid positions to the list. using foreach() somehow looks cleaner (why deal with counters and list boundaries yourself when the compiler can do this for you? :grinning: ) but I wonder if the Reverse() doesn’t just add an unneccessary extra performance hit…

As for showing the calculated path in the Tester, I did it like this:

if (Input.GetKeyDown(KeyCode.T))
{
    GridPosition startGridPosition = new GridPosition(0, 0);
    GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
    GridSystemVisual.Instance.HideAllGridPositions();
    List<GridPosition> gridPositionList = Pathfinding.Instance.FindPath(startGridPosition, mouseGridPosition);
    GridSystemVisual.Instance.ShowGridPostionList(gridPositionList, GridSystemVisual.GridVisualType.Yellow);
}

I think this is much easier to see…

I do find that while I get a path to the target postion, I seem to not always get the same and the debugging values differ. Specifically I don’t seem to get a 0 for the target position’s h value…
I suppose I will see in the next lesson when obstacles are added whether there is an issue somewhere in my code, or it’s just that I have a different order in the neighbour’s list…

When it comes to calculating the path (following the breadcrumbs, basically), there is more than one solution to the problem.

I’m honestly not sure what the performance hit on a Reverse is. I’m suspecting it may be fairly fast, as it’s a C# native code method, but without profiling it’s hard to be sure (especially because when profiling, our code is generally in debug mode and the Reverse() won’t be, so our finger is on the scale slowing our method down compared to the native method.

Another clever method is to use and return a Stack instead of a List

private Stack<GridPosition> CalculatePath(PathNode endNode)
{
    //Queue<GridPosition> pathNodeQueue = new();
    Stack<GridPosition> pathNodeStack = new();
    PathNode currentNode = endNode;
    while(currentNode!=null)
    {
         //pathNodeQueue.Enqueue(currentNode);
         pathNodeStack.Push(currentNode);
         currentNode = currentNode.GetCameFromPathNode();
    }
    return pathNodeStack;
}

Of course., this would mean pasing the Stack back to the calling function, which changes some of the ways in which the code will interface with the collection.

I considered inserting the resulting positions in front but that would likely be a bigger performance hit on a plain list that might have to shuffle all entries around to make room, unless it’s clever enough (which it might well be) to just do some pointer arithmetic so it can easily put an element anywhere into the list…

A Queue on the other hand would be a FIFO, so not really what would help turning the path upside down. That would be more something for a LIFO, or a stack… :wink:

I did the next lesson now and my path goes around obstacle cells just fine, so I suppose no need to change anything… :slight_smile:

LOL, you’re right, I meant a stack.

1 Like

Privacy & Terms