Hi,
I’m using a Grid Graph w/ 8 neighbours and am setting the walkability of each node based on game-specific logic. After I set the walkability of a GraphNode to false, I call graphNode.ClearConnections(true). It correctly removes the connections with cardinal neighbours, but the diagonal neighbours keep the connection with the unwalkable node. Here’s a pic of what my graph looks like. Any help solving this would be greatly appreciated.
The relevant code I use is:
graphNode.Walkable = false;
graphNode.ClearConnections(true);
var guo = new GraphUpdateObject(grid.AABBBounds);
guo.updatePhysics = true;
AstarPath.active.UpdateGraphs(guo);
Hi
ClearConnections(true) on grid nodes doesn’t actually remove the connections in the other direction. I have added this to my todo list.
However looking at code you are using, it seems that you are recalculating the whole grid with updatePhysics=true after you have changed the nodes, this will reset the walkability values and connections. Are you sure you want to do that?
Generally if you want to set the walkability of nodes in a grid graph, this is how I recommend that you do it:
graphNode.Walkable = false;
// ... set walkability on a bunch of other nodes
// Update connections of all nodes
var gg = AstarPath.active.gridGraph;
for (int z = 0; z < gg.depth; z++) {
for (int x = 0; x < gg.width; x++) {
var node = gg.nodes[z*gg.width + x];
gg.CalculateConnections(x, z, node);
}
}
Or another approach that you could take is to subclass the GraphUpdateObject and add some custom code to the Apply method and then update the grid graph using it. (make sure to update it using updatePhysics=false otherwise the changed made by the Apply method will be overridden).
Setting updatePhysics to false and using your recommendation worked perfectly. I was also updating the connections while still modifying other node’s walkability, so I now I wait until all walkabilitys are set before calculating the connections.
Thanks Aron!
1 Like