Support Forum

NodeIndex values off by 1? Or a duplicate of the first node? Bug?

I’m doing a bunch of calculations on a GridGraph in editor mode. I recently made an array the same size as the gridGraph.CountNodes() return, but I kept getting an IndexOutOfRange error until I changed the array size to gridGraph.CountNodes() + 1.

When I run this line

Debug.Log(“Node at index 0. NodeIndex:” + gridGraph.nodes[0].NodeIndex + " has x " + gridGraph.nodes[0].XCoordinateInGrid + " Z: " + gridGraph.nodes[0].ZCoordinateInGrid);

I get this output:

Node at index 0. NodeIndex: 1 has x 0 Z: 0

When I do the same, but for nodes[1] I get:

Node at index 1. NodeIndex:2 has x 1 Z: 0

And when I query 0,0:

Debug.Log("Index of 0,0: " + gridGraph.GetNode(0, 0).NodeIndex);
I get a NodeIndex of 1 for that node…

When I use gridGraph.GetNodes(node => {…}) no node has a NodeIndex of 1.

So they’re off by 1. Is this a bug?

Hi

If you take a look at the documentation for NodeIndex (https://arongranberg.com/astar/docs/graphnode.html#NodeIndex) you will see that it is a global index, and it is not necessarily correlated with any property of the nodes (such as their coordinates in the graph). If you want the index in the grid you can use node.NodeInGridIndex (https://arongranberg.com/astar/docs/gridnodebase.html#NodeInGridIndex) which has a well defined value for nodes in a grid graph.