Why call HasPath and GetPathLength?

In the Move action, we’re invoking FindPath twice on each square, once for HasPath and another for GetPathLength. This seems redundant. If it has no path then it returns pathlength 0. Why not do one check if the path length is inside the move distance and is not 0? Is there something down the line I’d be costing myself?

Quite the opposite, one operation that can literally cut your AI calculations in 1/2 is to cache GetPathLength() and test it to make sure it’s not zero and not greater than the move distance. (Cache it, though, because if(GetPathLength()>0 && GetPathLength()<MoveDistance) will calculate the path twice.

Thanks, caching the checks when they’re first done was another question in my mind. I must have missed them doing it in the course but I was considering a Dictionary<GridPosition, Path> and clear it everytime the unit completes the action.

1 Like

Also clear it when the selected unit changes.

This would also represent a great improvement in speed.

Right, good call.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms