Unsure how to use OptimizeForSparseGraph at runtime

Hello, after a few weeks I have created the perfect custom node placer/remover and connection maker script for my game using Point Graphs. It works amazing. So I decided to purchase the Pro version mainly for the OptimizeForSparseGraph feature, but I can’t get it to work yet.

My script works by basically having a grid following the Mob and updating its nodes and connections every “x” seconds to account for the loading of new terrain due to the Mob’s movement as well as any potential terrain modifications made within this grid as my game is a procedurally generated 2D platformer sandbox.

I won’t post all the code here as it’s too extensive and irrelevant but I basically follow this structure:

AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
    var graph = AstarPath.active.data.pointGraph;
    // Add 2 nodes and connect them
    var node1 = graph.AddNode((Int3)transform.position);
    var node2 = graph.AddNode((Int3)(transform.position + Vector3.right));
    graph.RebuildNodeLookup();
    var cost = (uint)(node2.position - node1.position).costMagnitude;
    node1.AddConnection(node2, cost);
    node2.AddConnection(node1, cost);
    graph.RebuildConnectionDistanceLookup();
}));

I also tried:

AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
    var graph = AstarPath.active.data.pointGraph;
    // Add 2 nodes and connect them
    var node1 = graph.AddNode((Int3)transform.position);
    var node2 = graph.AddNode((Int3)(transform.position + Vector3.right));
    var cost = (uint)(node2.position - node1.position).costMagnitude;
    node1.AddConnection(node2, cost);
    node2.AddConnection(node1, cost);
    graph.RebuildNodeLookup();
    graph.RebuildConnectionDistanceLookup();
}));

Then the Mob doesn’t move at all and I get these errors:

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.PointGraph.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint, System.Single maxDistanceSqr) (at Packages/com.arongranberg.astar/Graphs/PointGraph.cs:204)
AstarPath.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint) (at Packages/com.arongranberg.astar/Core/AstarPath.cs:2158)
Pathfinding.Path.GetNearest (UnityEngine.Vector3 point) (at Packages/com.arongranberg.astar/Core/Pathfinding/Path.cs:901)
Pathfinding.ABPath.Prepare () (at Packages/com.arongranberg.astar/Pathfinders/ABPath.cs:400)
Pathfinding.Path.Pathfinding.IPathInternals.Prepare () (at Packages/com.arongranberg.astar/Core/Pathfinding/Path.cs:1129)
Pathfinding.PathProcessor.CalculatePathsThreaded (Pathfinding.PathHandler pathHandler, Pathfinding.BlockableChannel`1+Receiver[T] receiver) (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:315)
UnityEngine.Debug:LogException(Exception)
Pathfinding.PathProcessor:CalculatePathsThreaded(PathHandler, Receiver) (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:379)
Pathfinding.<>c__DisplayClass27_0:b__0() (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:121)
System.Threading.ThreadHelper:ThreadStart()

Unhandled exception during pathfinding. Terminating.
UnityEngine.Debug:LogError (object)
Pathfinding.PathProcessor:CalculatePathsThreaded (Pathfinding.PathHandler,Pathfinding.BlockableChannel`1/Receiver<Pathfinding.Path>) (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:380)
Pathfinding.PathProcessor/<>c__DisplayClass27_0:b__0 () (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:121)
System.Threading.ThreadHelper:ThreadStart ()

Error : This part should never be reached.
UnityEngine.Debug:LogError (object)
Pathfinding.PathProcessor:CalculatePathsThreaded (Pathfinding.PathHandler,Pathfinding.BlockableChannel`1/Receiver<Pathfinding.Path>) (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:387)
Pathfinding.PathProcessor/<>c__DisplayClass27_0:b__0 () (at Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:121)
System.Threading.ThreadHelper:ThreadStart ()

Hi

This looks like a bug. Possibly it’s been introduced in a recent update.
I’ll try to investigate, however I’m on holiday right now, so it might take a little while.

Definitely a bug. I’ll include a fix in the next update.