Pathfinding

I am surprised that you don’t combine the evaluation of HasPath and GetPathLength when determining moveActions valid cells. Having a single call to pathfinding from MoveAction (line 100ish) would be much faster on larger grids.

If I combine the calls, will i regret this for some reason in the future?

                float pathLength =
                    Pathfinding
                        .Instance
                        .GetPathLength(unitGridPosition, testGridPosition) /
                    Pathfinding.PathfindingDistanceMultiplier;
                if (pathLength == 0)
                {
                    // no path found
                    continue;
                }
                if (pathLength > maxMoveDistance)
                {
                    // point is too far away
                    continue;
                }

I’m not entirely convinced that this would dramatically speed up calculations even on a large grid, as the compiler applies a great deal of optimization in the CLR or IL2CPP code for just these circumstances. The actual operations of the comparisons take the same amount of time, and in this case, is still short circuited (the if(pathLength > maxMoveDistance) will never be evaluated if pathLength == 0, just like with an || operator. I suppose that’s something that could be tested with a profiler.

It does look nicer, however, and is one of the standard recommendation that editors like JetBrains Rider would recommend as a preferred style. Unless you need to perform some action besides continue in the event that pathLength==0, there are no downsides to combining the expressions

if(pathLength==0 || pathLength > maxMoveDistance) continue;

It will be short-circuited if there is no path, but we call FindPath twice in each iteration of the loop where it does find a path.

I don’t think so. I looked at my code and I actually did combine them.

(var pathList, var pathLength) = Pathfinding.Instance.FindPath(unitGridPosition, testGridPosition);
1 Like

doh, obviously it will be short-circuited. by bad.
i like the original code better after all.

(Edit)
Actually they only shortcircuit is some cases, ha.
Combat ning the methods is how I decided to go. Pathfinding the the most part tense part of the game so far. I even turn off gridvisual on nove to prevent stuttering.

It’s all a matter of which one is more readable to you. Short circuit evaluated compound if statements are great, but they can get confusing.

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

Privacy & Terms