Aight I’ll try to give as much detail as possible code wise.
- Initial scan of the graph at the start of a session (this code happens inside a coroutine)
// Pathfinding needs to be scanned once, otherwise it won't work on start
foreach (var _ in AstarPath.active.ScanAsync()) { yield return null; }
- Spawning the player in the already scanned environment (the target corresponds to where the player is being spawned on, i.e. same position)
// Need to search the path once, otherwise pathfinding does not work the first time around
aiDestinationSetter.target = target.transform;
movement.SearchPath();
- Requesting to move the player by checking if there is a valid path and if so setting the destination
private IEnumerator RequestMove(Transform target)
{
var path = Seeker.GetCurrentPath(); // Removing this and just using Seeker.StartPath() does not make a difference.
path = Seeker.StartPath(player.transform.position, target.position);
yield return StartCoroutine(path.WaitForPath());
if (path.error)
{
// Player does not move
}
else
{
aiLerp.CanMove(true);
aiDestinationSetter.SetDestination(target);
// Player does move
}
}
- This is a special case but I scan the graph again when a chunks are finished loading. This is usually called every 15 to 30 seconds depending on how fast the player moves. I simply wrapped this in a try catch as it was throwing an exception when I was already scanning the graph asynchronously (which happens once at the start of the game, after that the try catch is technically not needed).
private void OnChunksProcessed()
{
try
{
AstarPath.active.data.gridGraph.Scan();
}
catch
{
//
}
}
InvalidOperationException: Another async scan is already running
AstarPath+d__123.MoveNext () (at ./Library/PackageCache/com.arongranberg.astar@4.3.82/Core/AstarPath.cs:1621)
AstarPath.Scan (Pathfinding.NavGraph graphsToScan) (at ./Library/PackageCache/com.arongranberg.astar@4.3.82/Core/AstarPath.cs:1543)
AstarPath.Scan (Pathfinding.NavGraph graphToScan) (at ./Library/PackageCache/com.arongranberg.astar@4.3.82/Core/AstarPath.cs:1511)
Pathfinding.NavGraph.Scan () (at ./Library/PackageCache/com.arongranberg.astar@4.3.82/Graphs/NavGraph.cs:338)
- Lastly I perform local graph updates if an object is removed from the world, but I simply do this by defining the bounds and executing the following code.
AstarPath.active.UpdateGraphs (bounds);
These should be all occurrences where I directly interact with the pathfinding system.
€: Also here is a better stack trace of the error I’m getting in the version 4.3.64, maybe with that you can determine if the issue was actually fixed in subsequent versions. The ‘JobCopyRectangle’ exception points to the way I scan the graph, i.e. ‘AstarPath.active.data.gridGraph.Scan()’ and the ‘This path has not been started yet’ exception points to the ‘WaitForPath()’ method.
Error.txt (33.3 KB)