AddWorkItem -> wait until done

Hi,

I’m updating the graph with “AddWorkItem” and a “CalculateConnectionsForCellAndNeighbours” after that. The problem is that the next section of my code doesn’t reconise the changes.

For testing purpossed I made my code a “IEnumerator” and made it wait one frame after I updated the grid. Then it works fine.

My question, is there a way to make my code wait until Astar is done updating en recalculating the grid?

My code: (for a single position and a list op positions)

public void UpdateRoomGrid(List<Vector3> positions, bool walkable)
{
    for (int i = 0; i < positions.Count; i++)
    {
        UpdateRoomGrid(positions[i], walkable);
    }
}

public void UpdateRoomGrid(Vector3 position, bool walkable)
{
    AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
        var node = roomGrid.GetNearest(position).node;
        
        node.Walkable = walkable;
        var n = node as GridNodeBase;
        roomGrid.CalculateConnectionsForCellAndNeighbours(n.XCoordinateInGrid, n.ZCoordinateInGrid);
    }));
}

Thanks

Update:
I added a “AstarPath.active.FlushWorkItems();” for testing. This seems to work. I know this may couse preformance issues on larger updates.

So if there is a better way, like a callback from my original question, I would still like to try that :slight_smile:

Thanks

Hi

FlushWorkItems will as you say cause the items to be performed immediately.

Alternatively inside your work-item you can for example set a global boolean that indicates if your code has been executed.

AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
    workItemHasExecuted = true;
});