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? ) 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…