Function PathUtilities.BFS

BFS (GraphNode seed, int depth, int tagMask=-1, System.Func<GraphNode, bool> filter=null)

Returns all nodes up to a given node-distance from the seed node.

Public Static
List<GraphNode> BFS (

GraphNode

seed

The node to start the search from.

int

depth

The maximum node-distance from the seed node.

int

tagMask=-1

Optional mask for tags. This is a bitmask.

System.Func<GraphNode, bool>

filter=null

Optional filter for which nodes to search. You can combine this with depth = int.MaxValue and tagMask = -1 to make the filter determine everything. Only walkable nodes are searched regardless of the filter. If the filter function returns false the node will be treated as unwalkable.

)

Returns all nodes up to a given node-distance from the seed node.

This function performs a BFS (breadth-first search) or flood fill of the graph and returns all nodes within a specified node distance which can be reached from the seed node. In almost all cases when depth is large enough this will be identical to returning all nodes which have the same area as the seed node. In the editor areas are displayed as different colors of the nodes. The only case where it will not be so is when there is a one way path from some part of the area to the seed node but no path from the seed node to that part of the graph.

The returned list is sorted by node distance from the seed node i.e distance is measured in the number of nodes the shortest path from seed to that node would pass through. Note that the distance measurement does not take heuristics, penalties or tag penalties.

Depending on the number of nodes, this function can take quite some time to calculate so don't use it too often or it might affect the framerate of your game.

Return

A List<GraphNode> containing all nodes reachable up to a specified node distance from the seed node. For better memory management the returned list should be pooled, see Pathfinding.Pooling.ListPool

Warning

This method is not thread safe. Only use it from the Unity thread (i.e normal game code).

The video below shows the BFS result with varying values of depth. Points are sampled on the nodes using GetPointsOnNodes.

var seed = AstarPath.active.GetNearest(transform.position, NNConstraint.Walkable).node;
var nodes = PathUtilities.BFS(seed, 10);
foreach (var node in nodes) {
Debug.DrawRay((Vector3)node.position, Vector3.up, Color.red, 10);
}