Connections or neighbors of a specific point

I’m trying to retrieve a list of only points that are directly connected (via Scan) to the current point in a PointGraph.

Thanks

Hi
You can do it in one of these ways

var node = AstarPath.active.GetNearest (somePoint) as PointNode;
var connections = node.connections;
for ( int i = 0; i < connections.Length; i++ ) {
    Debug.Log ("Found a connection between " + ((Vector3)node.Position) + " and " + ((Vector3)connections[i].Position));

or (works on all node types)

var node = AstarPath.active.GetNearest (somePoint);
node.GetConnections((other) => {
    Debug.Log ("Found a connection between " + ((Vector3)node.Position) + " and " + ((Vector3)other.Position));
});

Thank you, that works great. When I inspect the connections during debug, I can see the gameobjects for these points, but the only way I can see to access them is to run AstarPath.active.GetNearest again from inside the loop. Is this the best way to get a points gameobject?

this.closestNode = (PointNode)AstarPath.active.GetNearest(transform.position) as PointNode;
GraphNode[] connections = this.closestNode.connections;

for (int i=0; i<connections.Length; i++)
{
    this.node = (PointNode)AstarPath.active.GetNearest((Vector3)connections[i].position) as PointNode;
    this.node.gameObject.SendMessage("SetVisible", true);
}

Hi

You can cast those nodes to PointNodes.

this.closestNode = AstarPath.active.GetNearest(transform.position) as PointNode;
GraphNode[] connections = this.closestNode.connections;

for (int i=0; i<connections.Length; i++) {
    var node = connections[i] as PointNode;
    node.gameObject.SendMessage("SetVisible", true);
}
1 Like