Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NavigationServer map_force_update() function #62300

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/classes/NavigationServer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
Create a new map.
</description>
</method>
<method name="map_force_update">
<return type="void" />
<argument index="0" name="map" type="RID" />
<description>
This function immediately forces synchronization of the specified navigation [code]map[/code] [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 [code]safe_velocity[/code] signals is untouched by this function and continues to happen for all maps and agents at the end of the physics frame.
[b]Note:[/b] 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.
</description>
</method>
<method name="map_get_agents" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
Expand Down
10 changes: 10 additions & 0 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
Create a new map.
</description>
</method>
<method name="map_force_update">
<return type="void" />
<argument index="0" name="map" type="RID" />
<description>
This function immediately forces synchronization of the specified navigation [code]map[/code] [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 [code]safe_velocity[/code] signals is untouched by this function and continues to happen for all maps and agents at the end of the physics frame.
[b]Note:[/b] 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.
</description>
</method>
<method name="map_get_agents" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
Expand Down
9 changes: 9 additions & 0 deletions modules/navigation/godot_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ void GodotNavigationServer::flush_queries() {
commands.clear();
}

void GodotNavigationServer::map_force_update(RID p_map) {
NavMap *map = map_owner.get_or_null(p_map);
ERR_FAIL_COND(map == nullptr);

flush_queries();

map->sync();
}

void GodotNavigationServer::process(real_t p_delta_time) {
flush_queries();

Expand Down
2 changes: 2 additions & 0 deletions modules/navigation/godot_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class GodotNavigationServer : public NavigationServer3D {
virtual Array map_get_regions(RID p_map) const override;
virtual Array map_get_agents(RID p_map) const override;

virtual void map_force_update(RID p_map) override;

virtual RID region_create() const override;

COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
Expand Down
6 changes: 6 additions & 0 deletions servers/navigation_server_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void NavigationServer2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer2D::map_get_regions);
ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer2D::map_get_agents);

ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer2D::map_force_update);

ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create);
ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer2D::region_set_enter_cost);
ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer2D::region_get_enter_cost);
Expand Down Expand Up @@ -231,6 +233,10 @@ void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to

bool FORWARD_1_C(map_is_active, RID, p_map, rid_to_rid);

void NavigationServer2D::map_force_update(RID p_map) {
NavigationServer3D::get_singleton_mut()->map_force_update(p_map);
}

void FORWARD_2_C(map_set_cell_size, RID, p_map, real_t, p_cell_size, rid_to_rid, real_to_real);
real_t FORWARD_1_C(map_get_cell_size, RID, p_map, rid_to_rid);

Expand Down
2 changes: 2 additions & 0 deletions servers/navigation_server_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class NavigationServer2D : public Object {
virtual Array map_get_regions(RID p_map) const;
virtual Array map_get_agents(RID p_map) const;

virtual void map_force_update(RID p_map);

/// Creates a new region.
virtual RID region_create() const;

Expand Down
2 changes: 2 additions & 0 deletions servers/navigation_server_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void NavigationServer3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer3D::map_get_regions);
ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer3D::map_get_agents);

ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer3D::map_force_update);

ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer3D::region_create);
ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer3D::region_set_enter_cost);
ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer3D::region_get_enter_cost);
Expand Down
2 changes: 2 additions & 0 deletions servers/navigation_server_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class NavigationServer3D : public Object {
virtual Array map_get_regions(RID p_map) const = 0;
virtual Array map_get_agents(RID p_map) const = 0;

virtual void map_force_update(RID p_map) = 0;

/// Creates a new region.
virtual RID region_create() const = 0;

Expand Down