Best practice to change node walkability in script

I’m not using any of A*'s fancy features, I just have a regular grid for path-finding on a flat tile-based game with randomised content. I’m going to want to add walkability changes at level creation and in real time. Not that many, but I’d still like it to be fast.

I can see two ways to handle this. 1) GraphUpdateObject(bounds);
2) Directly modifying the graph data. AstarPath.active.astarData.gridGraph.nodes[…].Walkable

Is option 2 possible? Is the list of nodes a simple linear arrangement of the x,y grid in rows (node [x,y] pos in array = node[width*y+x])? What are the pros and cons of the methods?

Thanks

Hi

Yes, option 2 is possible.

`
AstarPath.active.RegisterSafeUpdate(delegate () {
var graph = AstarPath.active.astarData.gridGraph;
var nodes = graph.nodes;
var width = graph.width;
var depth = graph.depth;

nodes[z*width + x].Walkable = false;

// Recalculate the connections of all nodes (actually only needed around the nodes that you update)
for ( int z = 0; z < depth; z++ ) {
for ( int x = 0; x < width; x++ ) {
graph.CalculateConnections (nodes, x, z, nodes[z*width+x]);
}
}

// Required to make sure paths are calculated correctly
// Needed if walkability or connections change
AstarPath.active.FloodFill ();
});`

GraphUpdateObjects do a lot of stuff for you, for example the above code does not handle erosion similar, but it’s probably good enough for most cases.
Note that you can subclass the GraphUpdateObject and override the Apply method to customise what happens when the GraphUpdateObject is applied.

Option 2 is faster, but likely not by a very large margin. Test both approaches and see what works best.

The safe update thing is required to make sure that pathfinding threads are stopped when the update happens. You do not want to update the graph at the same time as pathfinding is running.