Creating new GridGraph areas by cutting connections

Hi there!

I’m trying to create a new area at runtime by cutting the connections between grid nodes. I can’t use normal unwalkable nodes because my current constraints require the areas to be right next to each other. I’ve extended the GraphUpdateObject to do the connection cutting for the nodes, using SetConnectionInternal on each node.

When I visualize the graph, the cut connections are displayed, but the nodes are still shown as part of the same area. I just want to understand the underlying process a bit better. Can someone answer a few questions for me?

  1. Do GridGraphs require connections to be cut in both directions (between neighbors) to guarantee a new area? Right now, I’m only cutting connections from the new area toward the old area.

  2. When/how do areas get recalculated in the GraphUpdateObject? Is there a particular part of the base Apply method that I need to pay attention to (like ModifyWalkability)? Or is there a particular thing I need to add to my extension of it (like flood filling) to ensure areas are recalculated?

  3. Is there a better way to do all of this than extending GraphUpdateObject?

Let me know if some code will be useful and I’ll post it. Thanks!

Yes. They need to be cut from both directions.
You can cut the connection in the other direction by doing

var other = node.GetNeighbourAlongDirection(i) as GridNode;
if (other != null) {
    // Remove reverse connection. See doc for GridGraph.neighbourOffsets to see which indices are used for what.
    other.SetConnectionInternal(i < 4 ? ((i + 2) % 4) : (((i-2) % 4) + 4), false);
}

I should probably add a helper method for that reverse index calculation…

  1. SetConnectionInternal calls AstarPath.active.hierarchicalGraph.AddDirtyNode(this); which handles that automatically. See HierarchicalGraph - A* Pathfinding Project for more info.

  2. Probably using a WorkItem instead of a GraphUpdateObject. Your solution does work, but it’s not quite what the GraphUpdateObject was built for. See A* Pathfinding Project

Ok, great! Thanks for the lightning response :slight_smile:

I should mention that I’m also setting the tags of all the nodes in the new area, which is why I grabbed GUO. But I guess I could just do the tag set in my work item as well.

1 Like

That method worked like a charm, thanks! I put everything in a work item and all is well.

For anyone who comes to this thread later, it’s worth noting that you need to perform the GetNeighborAlongDirection actions before cutting the connections from the original node. This is because GetNeighborAlongDirection relies on an existing connection to get the neighbor. Found this out the hard way :grin:

Anyway, thanks for the help and for a fantastic product.

1 Like