Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

NavigationServer2D

Inherits: Object

Server interface for low-level 2D navigation access.

Description

NavigationServer2D is the server responsible for all 2D navigation. It handles several objects, namely maps, regions and agents.

Maps are made up of regions, which are made of navigation polygons. Together, they define the navigable areas in the 2D world.

Note: Most NavigationServer changes take effect after the next physics frame and not immediately. This includes all changes made to maps, regions or agents by navigation related Nodes in the SceneTree or made through scripts.

For two regions to be connected to each other, they must share a similar edge. An edge is considered connected to another if both of its two vertices are at a distance less than edge_connection_margin to the respective other edge’s vertex.

You may assign navigation layers to regions with region_set_navigation_layers, which then can be checked upon when requesting a path with map_get_path. This allows allowing or forbidding some areas to 2D objects.

To use the collision avoidance system, you may use agents. You can set an agent’s target velocity, then the servers will emit a callback with a modified velocity.

Note: The collision avoidance system ignores regions. Using the modified velocity as-is might lead to pushing an agent outside of a navigable area. This is a limitation of the collision avoidance system, any more complex situation may require the use of the physics engine.

This server keeps tracks of any call and executes them during the sync phase. This means that you can request any change to the map, using any thread, without worrying.

Tutorials

Methods

RID

agent_create ( )

RID

agent_get_map ( RID agent ) const

bool

agent_is_map_changed ( RID agent ) const

void

agent_set_callback ( RID agent, Callable callback )

void

agent_set_map ( RID agent, RID map )

void

agent_set_max_neighbors ( RID agent, int count )

void

agent_set_max_speed ( RID agent, float max_speed )

void

agent_set_neighbor_distance ( RID agent, float distance )

void

agent_set_position ( RID agent, Vector2 position )

void

agent_set_radius ( RID agent, float radius )

void

agent_set_target_velocity ( RID agent, Vector2 target_velocity )

void

agent_set_time_horizon ( RID agent, float time )

void

agent_set_velocity ( RID agent, Vector2 velocity )

void

free_rid ( RID rid )

bool

get_debug_enabled ( ) const

RID[]

get_maps ( ) const

RID

link_create ( )

Vector2

link_get_end_position ( RID link ) const

float

link_get_enter_cost ( RID link ) const

RID

link_get_map ( RID link ) const

int

link_get_navigation_layers ( RID link ) const

int

link_get_owner_id ( RID link ) const

Vector2

link_get_start_position ( RID link ) const

float

link_get_travel_cost ( RID link ) const

bool

link_is_bidirectional ( RID link ) const

void

link_set_bidirectional ( RID link, bool bidirectional )

void

link_set_end_position ( RID link, Vector2 position )

void

link_set_enter_cost ( RID link, float enter_cost )

void

link_set_map ( RID link, RID map )

void

link_set_navigation_layers ( RID link, int navigation_layers )

void

link_set_owner_id ( RID link, int owner_id )

void

link_set_start_position ( RID link, Vector2 position )

void

link_set_travel_cost ( RID link, float travel_cost )

RID

map_create ( )

void

map_force_update ( RID map )

RID[]

map_get_agents ( RID map ) const

float

map_get_cell_size ( RID map ) const

Vector2

map_get_closest_point ( RID map, Vector2 to_point ) const

RID

map_get_closest_point_owner ( RID map, Vector2 to_point ) const

float

map_get_edge_connection_margin ( RID map ) const

float

map_get_link_connection_radius ( RID map ) const

RID[]

map_get_links ( RID map ) const

PackedVector2Array

map_get_path ( RID map, Vector2 origin, Vector2 destination, bool optimize, int navigation_layers=1 ) const

RID[]

map_get_regions ( RID map ) const

bool

map_is_active ( RID map ) const

void

map_set_active ( RID map, bool active )

void

map_set_cell_size ( RID map, float cell_size )

void

map_set_edge_connection_margin ( RID map, float margin )

void

map_set_link_connection_radius ( RID map, float radius )

void

query_path ( NavigationPathQueryParameters2D parameters, NavigationPathQueryResult2D result ) const

RID

region_create ( )

Vector2

region_get_connection_pathway_end ( RID region, int connection ) const

Vector2

region_get_connection_pathway_start ( RID region, int connection ) const

int

region_get_connections_count ( RID region ) const

float

region_get_enter_cost ( RID region ) const

RID

region_get_map ( RID region ) const

int

region_get_navigation_layers ( RID region ) const

int

region_get_owner_id ( RID region ) const

float

region_get_travel_cost ( RID region ) const

bool

region_owns_point ( RID region, Vector2 point ) const

void

region_set_enter_cost ( RID region, float enter_cost )

void

region_set_map ( RID region, RID map )

void

region_set_navigation_layers ( RID region, int navigation_layers )

void

region_set_navigation_polygon ( RID region, NavigationPolygon navigation_polygon )

void

region_set_owner_id ( RID region, int owner_id )

void

region_set_transform ( RID region, Transform2D transform )

void

region_set_travel_cost ( RID region, float travel_cost )

void

set_debug_enabled ( bool enabled )


Signals

map_changed ( RID map )

Emitted when a navigation map is updated, when a region moves or is modified.


navigation_debug_changed ( )

Emitted when navigation debug settings are changed. Only available in debug builds.


Method Descriptions

RID agent_create ( )

Creates the agent.


RID agent_get_map ( RID agent ) const

Returns the navigation map RID the requested agent is currently assigned to.


bool agent_is_map_changed ( RID agent ) const

Returns true if the map got changed the previous frame.


void agent_set_callback ( RID agent, Callable callback )

Sets the callback that gets called after each avoidance processing step for the agent. The calculated safe_velocity will be passed as the first parameter just before the physics calculations.

Note: Created callbacks are always processed independently of the SceneTree state as long as the agent is on a navigation map and not freed. To disable the dispatch of a callback from an agent use agent_set_callback again with an empty Callable.


void agent_set_map ( RID agent, RID map )

Puts the agent in the map.


void agent_set_max_neighbors ( RID agent, int count )

Sets the maximum number of other agents the agent takes into account in the navigation. The larger this number, the longer the running time of the simulation. If the number is too low, the simulation will not be safe.


void agent_set_max_speed ( RID agent, float max_speed )

Sets the maximum speed of the agent. Must be positive.


void agent_set_neighbor_distance ( RID agent, float distance )

Sets the maximum distance to other agents this agent takes into account in the navigation. The larger this number, the longer the running time of the simulation. If the number is too low, the simulation will not be safe.


void agent_set_position ( RID agent, Vector2 position )

Sets the position of the agent in world space.


void agent_set_radius ( RID agent, float radius )

Sets the radius of the agent.


void agent_set_target_velocity ( RID agent, Vector2 target_velocity )

Sets the new target velocity.


void agent_set_time_horizon ( RID agent, float time )

The minimal amount of time for which the agent’s velocities that are computed by the simulation are safe with respect to other agents. The larger this number, the sooner this agent will respond to the presence of other agents, but the less freedom this agent has in choosing its velocities. Must be positive.


void agent_set_velocity ( RID agent, Vector2 velocity )

Sets the current velocity of the agent.


void free_rid ( RID rid )

Destroys the given RID.


bool get_debug_enabled ( ) const

Returns true when the NavigationServer has debug enabled.


RID[] get_maps ( ) const

Returns all created navigation map RIDs on the NavigationServer. This returns both 2D and 3D created navigation maps as there is technically no distinction between them.


RID link_create ( )

Create a new link between two positions on a map.


Vector2 link_get_end_position ( RID link ) const

Returns the ending position of this link.


float link_get_enter_cost ( RID link ) const

Returns the enter cost of this link.


RID link_get_map ( RID link ) const

Returns the navigation map RID the requested link is currently assigned to.


int link_get_navigation_layers ( RID link ) const

Returns the navigation layers for this link.


int link_get_owner_id ( RID link ) const

Returns the ObjectID of the object which manages this link.


Vector2 link_get_start_position ( RID link ) const

Returns the starting position of this link.


float link_get_travel_cost ( RID link ) const

Returns the travel cost of this link.


bool link_is_bidirectional ( RID link ) const

Returns whether this link can be travelled in both directions.


void link_set_bidirectional ( RID link, bool bidirectional )

Sets whether this link can be travelled in both directions.


void link_set_end_position ( RID link, Vector2 position )

Sets the exit position for the link.


void link_set_enter_cost ( RID link, float enter_cost )

Sets the enter_cost for this link.


void link_set_map ( RID link, RID map )

Sets the navigation map RID for the link.


void link_set_navigation_layers ( RID link, int navigation_layers )

Set the links’s navigation layers. This allows selecting links from a path request (when using map_get_path).


void link_set_owner_id ( RID link, int owner_id )

Set the ObjectID of the object which manages this link.


void link_set_start_position ( RID link, Vector2 position )

Sets the entry position for this link.


void link_set_travel_cost ( RID link, float travel_cost )

Sets the travel_cost for this link.


RID map_create ( )

Create a new map.


void map_force_update ( RID map )

This function immediately forces synchronization of the specified navigation map RID. By default navigation maps are only synchronized at the end of each physics frame. This function can be used to immediately (re)calculate all the navigation meshes and region connections of the navigation map. This makes it possible to query a navigation path for a changed map immediately and in the same frame (multiple times if needed).

Due to technical restrictions the current NavigationServer command queue will be flushed. This means all already queued update commands for this physics frame will be executed, even those intended for other maps, regions and agents not part of the specified map. The expensive computation of the navigation meshes and region connections of a map will only be done for the specified map. Other maps will receive the normal synchronization at the end of the physics frame. Should the specified map receive changes after the forced update it will update again as well when the other maps receive their update.

Avoidance processing and dispatch of the safe_velocity signals is untouched by this function and continues to happen for all maps and agents at the end of the physics frame.

Note: With great power comes great responsibility. This function should only be used by users that really know what they are doing and have a good reason for it. Forcing an immediate update of a navigation map requires locking the NavigationServer and flushing the entire NavigationServer command queue. Not only can this severely impact the performance of a game but it can also introduce bugs if used inappropriately without much foresight.


RID[] map_get_agents ( RID map ) const

Returns all navigation agents RIDs that are currently assigned to the requested navigation map.


float map_get_cell_size ( RID map ) const

Returns the map cell size.


Vector2 map_get_closest_point ( RID map, Vector2 to_point ) const

Returns the point closest to the provided to_point on the navigation mesh surface.


RID map_get_closest_point_owner ( RID map, Vector2 to_point ) const

Returns the owner region RID for the point returned by map_get_closest_point.


float map_get_edge_connection_margin ( RID map ) const

Returns the edge connection margin of the map. The edge connection margin is a distance used to connect two regions.


float map_get_link_connection_radius ( RID map ) const

Returns the link connection radius of the map. This distance is the maximum range any link will search for navigation mesh polygons to connect to.


RID[] map_get_links ( RID map ) const

Returns all navigation link RIDs that are currently assigned to the requested navigation map.


PackedVector2Array map_get_path ( RID map, Vector2 origin, Vector2 destination, bool optimize, int navigation_layers=1 ) const

Returns the navigation path to reach the destination from the origin. navigation_layers is a bitmask of all region navigation layers that are allowed to be in the path.


RID[] map_get_regions ( RID map ) const

Returns all navigation regions RIDs that are currently assigned to the requested navigation map.


bool map_is_active ( RID map ) const

Returns true if the map is active.


void map_set_active ( RID map, bool active )

Sets the map active.


void map_set_cell_size ( RID map, float cell_size )

Set the map cell size used to weld the navigation mesh polygons.


void map_set_edge_connection_margin ( RID map, float margin )

Set the map edge connection margin used to weld the compatible region edges.


void map_set_link_connection_radius ( RID map, float radius )

Set the map’s link connection radius used to connect links to navigation polygons.


void query_path ( NavigationPathQueryParameters2D parameters, NavigationPathQueryResult2D result ) const

Queries a path in a given navigation map. Start and target position and other parameters are defined through NavigationPathQueryParameters2D. Updates the provided NavigationPathQueryResult2D result object with the path among other results requested by the query.


RID region_create ( )

Creates a new region.


Vector2 region_get_connection_pathway_end ( RID region, int connection ) const

Returns the ending point of a connection door. connection is an index between 0 and the return value of region_get_connections_count.


Vector2 region_get_connection_pathway_start ( RID region, int connection ) const

Returns the starting point of a connection door. connection is an index between 0 and the return value of region_get_connections_count.


int region_get_connections_count ( RID region ) const

Returns how many connections this region has with other regions in the map.


float region_get_enter_cost ( RID region ) const

Returns the enter cost of this region.


RID region_get_map ( RID region ) const

Returns the navigation map RID the requested region is currently assigned to.


int region_get_navigation_layers ( RID region ) const

Returns the region’s navigation layers.


int region_get_owner_id ( RID region ) const

Returns the ObjectID of the object which manages this region.


float region_get_travel_cost ( RID region ) const

Returns the travel cost of this region.


bool region_owns_point ( RID region, Vector2 point ) const

Returns true if the provided point in world space is currently owned by the provided navigation region. Owned in this context means that one of the region’s navigation mesh polygon faces has a possible position at the closest distance to this point compared to all other navigation meshes from other navigation regions that are also registered on the navigation map of the provided region.

If multiple navigation meshes have positions at equal distance the navigation region whose polygons are processed first wins the ownership. Polygons are processed in the same order that navigation regions were registered on the NavigationServer.

Note: If navigation meshes from different navigation regions overlap (which should be avoided in general) the result might not be what is expected.


void region_set_enter_cost ( RID region, float enter_cost )

Sets the enter_cost for this region.


void region_set_map ( RID region, RID map )

Sets the map for the region.


void region_set_navigation_layers ( RID region, int navigation_layers )

Set the region’s navigation layers. This allows selecting regions from a path request (when using map_get_path).


void region_set_navigation_polygon ( RID region, NavigationPolygon navigation_polygon )

Sets the navigation_polygon for the region.


void region_set_owner_id ( RID region, int owner_id )

Set the ObjectID of the object which manages this region.


void region_set_transform ( RID region, Transform2D transform )

Sets the global transformation for the region.


void region_set_travel_cost ( RID region, float travel_cost )

Sets the travel_cost for this region.


void set_debug_enabled ( bool enabled )

If true enables debug mode on the NavigationServer.