Point Graph returns 0 nodes

Hi,
I have 2 graphs, where the first one is a navmesh graph and the second one is a point graph.
Im trying to initialize the point graph with nodes in runtime like this, but later end up with 0 nodes:

		AstarPath.RegisterSafeUpdate(delegate ()
		{
			// Safe to modify graph data here
			var graph = AstarPath.active.graphs[1] as PointGraph;

			Int3 pos1 = new Int3() { x = 0, y = 5, z = 0 };
			Int3 pos2 = new Int3() { x = 50,  y = 5, z = 50 };

			graph.AddNode(pos1);
			graph.AddNode(pos2);
			PointNode pn1 = graph.nodes[graph.CountNodes() - 2];
			PointNode pn2 = graph.nodes[graph.CountNodes() - 1];
			pn1.AddConnection(pn2, 0);
			pn2.AddConnection(pn1, 0);

			AstarPath.OnGraphPostScan += temp =>
			{
				if (temp.graphIndex == 1)
				{
					Debug.Log("Nodes in graph: " + graph.nodeCount);
				}
			};

			AstarPath.active.Scan();
		});

Also, how can i scan only the point graph?

Hi

Scanning the point graph will recalculate the nodes based on the point graph settings (usually using the children of some GameObject as nodes). Calling AddNode will modify the existing graph.
So you should simply not scan the graphs (or possibly scan it before you start to change it to make sure it is initialized properly).

Hi,
Yes you’re right, all i had to do was to skip the scan and the nodeCount display as expected. Thanks!

1 Like

Hm, i think i still need to clear something up, because given this scenario:

graph.AddNode(new Int3() { x = 0, y = 5, z = 0 });
graph.AddNode(new Int3() { x = 2, y = 5, z = 0 });
graph.AddNode(new Int3() { x = 4, y = 5, z = 0 });

I end up with 3 nodes, which is great. However, when i try to use this, it returns (4,5,0) as the nearest node:

var nearest = graph.GetNearest(new Vector3(2, 5, 0));

Also, when i try to use StartPath, from a seeker or from a custom script like this one, i end up with a target at (0,0,0). Note that i have added the graph mask which should result in the second (point graph) being used right?

var path = ABPath.Construct(new Vector3(0, 5, 0), new Vector3(5, 5, 0), null);
path.nnConstraint.graphMask = 1 << 1;
path.callback += result =>
{
	Debug.Log("#Path# End node: " + result.vectorPath[result.vectorPath.Count - 1]);
};
AstarPath.StartPath(path);

Could it have to do with how i have set up the graph in the editor? It has no root since i want to use it as an in-memory pathfinder without tracing any visual points/gameobjects. Everything else is also set to default values, max distance and penalties are both 0. Checkboxes are also unchecked.

Hi

Int3 coordinates are represented using integers. To be able to represent smaller distances than 1 world unit, they are multiplied by 1000. There is an explicit conversion from Vector3 that you can use.

Int3 p = (Int3)new Vector3(1, 2, 3); // Gives you Int3(1000, 2000, 3000)

Thanks for the quick reply. I was able to verify that GetNearest works with your suggestion.
Im trying out a StartPath scenario as well but havent gotten it to work yet.

Update: I got it working once i had setup the connections correctly. thumbs up

1 Like