Updating single nodes to expand pathing in Grid Graph(2D) suddenly skips a node


#1

I’m creating a top-down 2D game where you can mark tiles(nodes) to dig your way out. I’m currently using this code to make it possible for the AI to walk once a tile has been dug:

   AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            var makeWalkableNode = AstarPath.active.GetNearest(cellPosition).node;
            makeWalkableNode.Walkable = true;
            var gg = AstarPath.active.data.gridGraph;
            gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));
            ctx.QueueFloodFill();
        }));

However, when they reach a certain distance from the center of the grid it skips a node.

The console prints the “makeWalkableNode” position once it has been dug, which you can see skips a step for some reason. Scanning the whole graph with AstarPath.active.Scan(); “fixes” this problem since the uncolored area connects, but is not good for performance.

If I change the Grid Graph’s width and height this “node skip” happens earlier. Doesn’t matter which way I dig, the skip forms a grid pattern.

I’ve tried reading the scripts and documentation to find if a single Grid Graph is divided into subgraphs with 1 node’s border, but wasn’t successful.


#2

Hi

Might it be that your cells don’t line up with the centers of the nodes? Could you show a screenshot of this?


#3

The cellPosition is a Vector3Int which is the bottom-left corner of a cell. I tried this:

 AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            Vector3 test = cellPosition + new Vector3(0.5f, 0.5f, 0);
            var node1 = AstarPath.active.GetNearest(test).node;
            Debug.Log(node1);
            Debug.Log(node1.position);
            node1.Walkable = true;
            var gg = AstarPath.active.data.gridGraph;
            gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));
            ctx.QueueFloodFill();
        }));

And it works now… So you are correct, I will have to adjust so that a cellPosition is in the center instead.

Thank you very much for this quick answer. The problem was deterministic so I just thought it would be something else. Do you know why?


#4

Hi

Nice!
The exact positions of the nodes depends on how you have configured the grid graph settings.
Note that you can also get the node using integer coordinates like

var x = 5;
var z = 7;
var node = AstarPath.active.data.gridGraph.GetNode(x,z);

where (x,z) = (0,0) is always the top-left node of the graph, and (width-1,depth-1) is always the bottom-right node.


#5

Oh, I might look into that.

Once again thank you very much!