Repository: godotengine/godot
File: modules/gridmap/grid_map.cpp (C++)

Code before:
		for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
			if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
				Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(cell_map[F.key].item);
				if (navigation_mesh.is_valid()) {
					RID region = NavigationServer3D::get_singleton()->region_create();
					NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
					NavigationServer3D::get_singleton()->region_set_navigation_layers(region, F.value.navigation_layers);
					NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
					NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
					if (map_override.is_valid()) {
						NavigationServer3D::get_singleton()->region_set_map(region, map_override);
					} else {
						NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
					}

					F.value.region = region;
				}
			}
		}

#ifdef DEBUG_ENABLED
		if (bake_navigation) {
			if (!g.navigation_debug_edge_connections_instance.is_valid()) {
				g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
			}
			if (!g.navigation_debug_edge_connections_mesh.is_valid()) {
				g.navigation_debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
			}

			_update_octant_navigation_debug_edge_connections_mesh(p_key);
		}
#endif // DEBUG_ENABLED
	}
}

void GridMap::_octant_exit_world(const OctantKey &p_key) {
	ERR_FAIL_NULL(RenderingServer::get_singleton());
	ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
	ERR_FAIL_NULL(NavigationServer3D::get_singleton());

	ERR_FAIL_COND(!octant_map.has(p_key));
	Octant &g = *octant_map[p_key];
	PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
	PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());

	if (g.collision_debug_instance.is_valid()) {
		RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
	}

	for (int i = 0; i < g.multimesh_instances.size(); i++) {
		RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());

Review comment (bug):
```suggestion
			if (g.navigation_debug_edge_connections_mesh.is_null()) {
```

Code after:
		for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
			if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
				Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(cell_map[F.key].item);
				if (navigation_mesh.is_valid()) {
					RID region = NavigationServer3D::get_singleton()->region_create();
					NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
					NavigationServer3D::get_singleton()->region_set_navigation_layers(region, F.value.navigation_layers);
					NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
					NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
					if (map_override.is_valid()) {
						NavigationServer3D::get_singleton()->region_set_map(region, map_override);
					} else {
						NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
					}

					F.value.region = region;
				}
			}
		}

#ifdef DEBUG_ENABLED
		if (bake_navigation) {
			if (!g.navigation_debug_edge_connections_instance.is_valid()) {
				g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
			}
			if (g.navigation_debug_edge_connections_mesh.is_null()) {
				g.navigation_debug_edge_connections_mesh.instantiate();
			}

			_update_octant_navigation_debug_edge_connections_mesh(p_key);
		}
#endif // DEBUG_ENABLED
	}
}

void GridMap::_octant_exit_world(const OctantKey &p_key) {
	ERR_FAIL_NULL(RenderingServer::get_singleton());
	ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
	ERR_FAIL_NULL(NavigationServer3D::get_singleton());

	ERR_FAIL_COND(!octant_map.has(p_key));
	Octant &g = *octant_map[p_key];
	PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
	PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());

	if (g.collision_debug_instance.is_valid()) {
		RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
	}

	for (int i = 0; i < g.multimesh_instances.size(); i++) {
		RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());