Skip to content

Commit

Permalink
Merge pull request #8 from Taurenkey/cancelPathFind
Browse files Browse the repository at this point in the history
Current pathfind cancel
  • Loading branch information
awgil authored Mar 6, 2024
2 parents d9ffbbb + 49f6ffe commit ce6c5f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions vnavmesh/IPCProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public IPCProvider(NavmeshManager navmeshManager, FollowPath followPath, AsyncMo
RegisterFunc("Nav.Rebuild", () => navmeshManager.Reload(false));
RegisterFunc("Nav.Pathfind", (Vector3 from, Vector3 to, bool fly) => navmeshManager.QueryPath(from, to, fly));
RegisterFunc("Nav.PathfindCancelable", (Vector3 from, Vector3 to, bool fly, CancellationToken cancel) => navmeshManager.QueryPath(from, to, fly, cancel));
RegisterAction("Nav.PathfindCancelCurrent", () => navmeshManager.CancelCurrentPathfinding());
RegisterFunc("Nav.PathfindInProgress", () => navmeshManager.PathfindInProgress);
RegisterFunc("Nav.PathfindNumQueued", () => navmeshManager.NumQueuedPathfindRequests);
RegisterFunc("Nav.IsAutoLoad", () => navmeshManager.AutoLoad);
Expand Down
15 changes: 14 additions & 1 deletion vnavmesh/NavmeshManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void Update()
// at this point, we're not loading a mesh
if (_query != null)
{
if (_currentPathfindTask != null && _currentPathfindTask.IsCompleted)
if (_currentPathfindTask != null && (_currentPathfindTask.IsCompleted || _currentPathfindTask.IsCanceled))
{
_currentPathfindTask = null;
}
Expand Down Expand Up @@ -151,6 +151,19 @@ public bool Reload(bool allowLoadFromCache)
return task;
}

public void CancelCurrentPathfinding()
{
if (_queryCancelSource == null || _currentPathfindTask is null || _query == null)
{
Service.Log.Error($"Can't cancel query - navmesh is not loaded");
return;
}

_queryCancelSource.Cancel(); // this will cancel current pathfind task
_queryCancelSource = new(); // create new token source for future tasks

}

// if non-empty string is returned, active layout is ready
private unsafe string GetCurrentKey()
{
Expand Down

0 comments on commit ce6c5f8

Please sign in to comment.