Get nodes inside NavigationRegion2D

Godot Version

v4.3.stable.mono.official [77dcf97d8]

Question

I couldn’t find my answer in the documentation but hoping that someone might be able to help me here. Is there a way to get all the nodes that are inside a given NavigationPolygon that is attached to a NavigationRegion2D?
I’m trying to set up patrol points for my AI, but since there can be multiple navigation regions, I want each region to have their own points.

I know I can manually add them, but it would be a lot easier and cleaner if, during runtime, I could just get a list of all the patrol points for any given region.

You can run get_vertices() method on your NavigationPolygon. Would that solve your issue?

Not really, as that doesn’t return the Nodes contained within the area itself, it only returns an array of Vector2s.

No there is no build-in way for this. The navigation system does not keep record of any generic SceneTree objects like Nodes.

You would need to reconstruct the polygons from the navmesh data and check them with e.g. Geometry2D.is_point_in_polygon() if the point is inside the polygons.

To do this for a NavigationPolygon resources you get the vertices array and then loop over all the get_polygon_count() polygons, get the polygon indices with get_polygon() and recreate the polygon array for the Geometry2D function. If the NavigationRegion2D is also transformed you need to apply the global transform to the vertices as well.

1 Like