Alternative CreatePath()

I had paused the video and attempted writing the CreatePath() before Ben gave the instruction…I kept it in my project because it works and thought I would share. Difference between video and this code is all waypoints are added within the loop (great for refactoring) instead of adding the start and end waypoints outside of the while code block. Notice the comparison to null instead of the startWaypoint.

Love the course, BTW. I’m learning tons of stuff. Keep it coming…

private void CreatePath()
{
Waypoint waypointToAddNext = endWaypoint;
while (waypointToAddNext != null)
{
path.Add(waypointToAddNext);
waypointToAddNext = waypointToAddNext.exploredFrom;
}
path.Reverse();
}

3 Likes

Great solution! You can simplify it further by saying:

while (waypointToAddNext)

If it is null it will return false.

1 Like

Privacy & Terms