Skip to content

Commit 63135ac

Browse files
committed
Merge pull request godotengine#89975 from AThousandShips/tile_fix_exit
[TileMap] Fix forcing cleanup on exiting tree/canvas
2 parents 0c71ba7 + 09edece commit 63135ac

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

scene/2d/tile_map_layer.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ Vector2i TileMapLayer::_coords_to_debug_quadrant_coords(const Vector2i &p_coords
5151
p_coords.y > 0 ? p_coords.y / TILE_MAP_DEBUG_QUADRANT_SIZE : (p_coords.y - (TILE_MAP_DEBUG_QUADRANT_SIZE - 1)) / TILE_MAP_DEBUG_QUADRANT_SIZE);
5252
}
5353

54-
void TileMapLayer::_debug_update() {
54+
void TileMapLayer::_debug_update(bool p_force_cleanup) {
5555
RenderingServer *rs = RenderingServer::get_singleton();
5656

5757
// Check if we should cleanup everything.
58-
bool forced_cleanup = in_destructor || !enabled || tile_set.is_null() || !is_visible_in_tree();
58+
bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
5959

6060
if (forced_cleanup) {
6161
for (KeyValue<Vector2i, Ref<DebugQuadrant>> &kv : debug_quadrant_map) {
@@ -178,11 +178,11 @@ void TileMapLayer::_debug_quadrants_update_cell(CellData &r_cell_data, SelfList<
178178
#endif // DEBUG_ENABLED
179179

180180
/////////////////////////////// Rendering //////////////////////////////////////
181-
void TileMapLayer::_rendering_update() {
181+
void TileMapLayer::_rendering_update(bool p_force_cleanup) {
182182
RenderingServer *rs = RenderingServer::get_singleton();
183183

184184
// Check if we should cleanup everything.
185-
bool forced_cleanup = in_destructor || !enabled || tile_set.is_null() || !is_visible_in_tree();
185+
bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
186186

187187
// ----------- Layer level processing -----------
188188
if (!forced_cleanup) {
@@ -673,9 +673,9 @@ void TileMapLayer::_rendering_draw_cell_debug(const RID &p_canvas_item, const Ve
673673

674674
/////////////////////////////// Physics //////////////////////////////////////
675675

676-
void TileMapLayer::_physics_update() {
676+
void TileMapLayer::_physics_update(bool p_force_cleanup) {
677677
// Check if we should cleanup everything.
678-
bool forced_cleanup = in_destructor || !enabled || !collision_enabled || !is_inside_tree() || tile_set.is_null();
678+
bool forced_cleanup = p_force_cleanup || !enabled || !collision_enabled || !is_inside_tree() || tile_set.is_null();
679679
if (forced_cleanup) {
680680
// Clean everything.
681681
for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
@@ -923,12 +923,12 @@ void TileMapLayer::_physics_draw_cell_debug(const RID &p_canvas_item, const Vect
923923

924924
/////////////////////////////// Navigation //////////////////////////////////////
925925

926-
void TileMapLayer::_navigation_update() {
926+
void TileMapLayer::_navigation_update(bool p_force_cleanup) {
927927
ERR_FAIL_NULL(NavigationServer2D::get_singleton());
928928
NavigationServer2D *ns = NavigationServer2D::get_singleton();
929929

930930
// Check if we should cleanup everything.
931-
bool forced_cleanup = in_destructor || !enabled || !navigation_enabled || !is_inside_tree() || tile_set.is_null();
931+
bool forced_cleanup = p_force_cleanup || !enabled || !navigation_enabled || !is_inside_tree() || tile_set.is_null();
932932

933933
// ----------- Layer level processing -----------
934934
// All this processing is kept for compatibility with the TileMap node.
@@ -1196,9 +1196,9 @@ void TileMapLayer::_navigation_draw_cell_debug(const RID &p_canvas_item, const V
11961196

11971197
/////////////////////////////// Scenes //////////////////////////////////////
11981198

1199-
void TileMapLayer::_scenes_update() {
1199+
void TileMapLayer::_scenes_update(bool p_force_cleanup) {
12001200
// Check if we should cleanup everything.
1201-
bool forced_cleanup = in_destructor || !enabled || !is_inside_tree() || tile_set.is_null();
1201+
bool forced_cleanup = p_force_cleanup || !enabled || !is_inside_tree() || tile_set.is_null();
12021202

12031203
if (forced_cleanup) {
12041204
// Clean everything.
@@ -1329,9 +1329,9 @@ void TileMapLayer::_scenes_draw_cell_debug(const RID &p_canvas_item, const Vecto
13291329

13301330
/////////////////////////////////////////////////////////////////////
13311331

1332-
void TileMapLayer::_build_runtime_update_tile_data() {
1332+
void TileMapLayer::_build_runtime_update_tile_data(bool p_force_cleanup) {
13331333
// Check if we should cleanup everything.
1334-
bool forced_cleanup = in_destructor || !enabled || tile_set.is_null() || !is_visible_in_tree();
1334+
bool forced_cleanup = p_force_cleanup || !enabled || tile_set.is_null() || !is_visible_in_tree();
13351335
if (!forced_cleanup) {
13361336
bool valid_runtime_update = GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update);
13371337
bool valid_runtime_update_for_tilemap = tile_map_node && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_use_tile_data_runtime_update) && tile_map_node->GDVIRTUAL_IS_OVERRIDDEN(_tile_data_runtime_update); // For keeping compatibility.
@@ -1635,23 +1635,21 @@ void TileMapLayer::_deferred_internal_update() {
16351635
}
16361636

16371637
// Update dirty quadrants on layers.
1638-
_internal_update();
1639-
1640-
pending_update = false;
1638+
_internal_update(false);
16411639
}
16421640

1643-
void TileMapLayer::_internal_update() {
1641+
void TileMapLayer::_internal_update(bool p_force_cleanup) {
16441642
// Find TileData that need a runtime modification.
16451643
// This may add cells to the dirty list if a runtime modification has been notified.
1646-
_build_runtime_update_tile_data();
1644+
_build_runtime_update_tile_data(p_force_cleanup);
16471645

16481646
// Update all subsystems.
1649-
_rendering_update();
1650-
_physics_update();
1651-
_navigation_update();
1652-
_scenes_update();
1647+
_rendering_update(p_force_cleanup);
1648+
_physics_update(p_force_cleanup);
1649+
_navigation_update(p_force_cleanup);
1650+
_scenes_update(p_force_cleanup);
16531651
#ifdef DEBUG_ENABLED
1654-
_debug_update();
1652+
_debug_update(p_force_cleanup);
16551653
#endif // DEBUG_ENABLED
16561654

16571655
_clear_runtime_update_tile_data();
@@ -1678,6 +1676,8 @@ void TileMapLayer::_internal_update() {
16781676

16791677
// Clear the dirty cells list.
16801678
dirty.cell_list.clear();
1679+
1680+
pending_update = false;
16811681
}
16821682

16831683
void TileMapLayer::_notification(int p_what) {
@@ -1694,8 +1694,8 @@ void TileMapLayer::_notification(int p_what) {
16941694

16951695
case NOTIFICATION_EXIT_TREE: {
16961696
dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] = true;
1697-
// Update immediately on exiting.
1698-
update_internals();
1697+
// Update immediately on exiting, and force cleanup.
1698+
_internal_update(true);
16991699
} break;
17001700

17011701
case TileMap::NOTIFICATION_ENTER_CANVAS: {
@@ -1705,8 +1705,8 @@ void TileMapLayer::_notification(int p_what) {
17051705

17061706
case TileMap::NOTIFICATION_EXIT_CANVAS: {
17071707
dirty.flags[DIRTY_FLAGS_LAYER_IN_CANVAS] = true;
1708-
// Update immediately on exiting.
1709-
update_internals();
1708+
// Update immediately on exiting, and force cleanup.
1709+
_internal_update(true);
17101710
} break;
17111711

17121712
case TileMap::NOTIFICATION_VISIBILITY_CHANGED: {
@@ -2498,8 +2498,7 @@ Vector2i TileMapLayer::get_coords_for_body_rid(RID p_physics_body) const {
24982498
}
24992499

25002500
void TileMapLayer::update_internals() {
2501-
pending_update = true;
2502-
_deferred_internal_update();
2501+
_internal_update(false);
25032502
}
25042503

25052504
void TileMapLayer::notify_runtime_tile_data_update() {
@@ -2825,9 +2824,8 @@ TileMapLayer::TileMapLayer() {
28252824
}
28262825

28272826
TileMapLayer::~TileMapLayer() {
2828-
in_destructor = true;
28292827
clear();
2830-
_internal_update();
2828+
_internal_update(true);
28312829
}
28322830

28332831
HashMap<Vector2i, TileSet::CellNeighbor> TerrainConstraint::get_overlapping_coords_and_peering_bits() const {

scene/2d/tile_map_layer.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ class TileMapLayer : public Node2D {
300300
bool flags[DIRTY_FLAGS_MAX] = { false };
301301
SelfList<CellData>::List cell_list;
302302
} dirty;
303-
bool in_destructor = false;
304303

305304
// Rect cache.
306305
mutable Rect2 rect_cache;
@@ -310,7 +309,7 @@ class TileMapLayer : public Node2D {
310309

311310
// Runtime tile data.
312311
bool _runtime_update_tile_data_was_cleaned_up = false;
313-
void _build_runtime_update_tile_data();
312+
void _build_runtime_update_tile_data(bool p_force_cleanup);
314313
void _build_runtime_update_tile_data_for_cell(CellData &r_cell_data, bool p_use_tilemap_for_runtime, bool p_auto_add_to_dirty_list = false);
315314
bool _runtime_update_needs_all_cells_cleaned_up = false;
316315
void _clear_runtime_update_tile_data();
@@ -321,13 +320,13 @@ class TileMapLayer : public Node2D {
321320
HashMap<Vector2i, Ref<DebugQuadrant>> debug_quadrant_map;
322321
Vector2i _coords_to_debug_quadrant_coords(const Vector2i &p_coords) const;
323322
bool _debug_was_cleaned_up = false;
324-
void _debug_update();
323+
void _debug_update(bool p_force_cleanup);
325324
void _debug_quadrants_update_cell(CellData &r_cell_data, SelfList<DebugQuadrant>::List &r_dirty_debug_quadrant_list);
326325
#endif // DEBUG_ENABLED
327326

328327
HashMap<Vector2i, Ref<RenderingQuadrant>> rendering_quadrant_map;
329328
bool _rendering_was_cleaned_up = false;
330-
void _rendering_update();
329+
void _rendering_update(bool p_force_cleanup);
331330
void _rendering_notification(int p_what);
332331
void _rendering_quadrants_update_cell(CellData &r_cell_data, SelfList<RenderingQuadrant>::List &r_dirty_rendering_quadrant_list);
333332
void _rendering_occluders_clear_cell(CellData &r_cell_data);
@@ -338,7 +337,7 @@ class TileMapLayer : public Node2D {
338337

339338
HashMap<RID, Vector2i> bodies_coords; // Mapping for RID to coords.
340339
bool _physics_was_cleaned_up = false;
341-
void _physics_update();
340+
void _physics_update(bool p_force_cleanup);
342341
void _physics_notification(int p_what);
343342
void _physics_clear_cell(CellData &r_cell_data);
344343
void _physics_update_cell(CellData &r_cell_data);
@@ -347,7 +346,7 @@ class TileMapLayer : public Node2D {
347346
#endif // DEBUG_ENABLED
348347

349348
bool _navigation_was_cleaned_up = false;
350-
void _navigation_update();
349+
void _navigation_update(bool p_force_cleanup);
351350
void _navigation_notification(int p_what);
352351
void _navigation_clear_cell(CellData &r_cell_data);
353352
void _navigation_update_cell(CellData &r_cell_data);
@@ -356,7 +355,7 @@ class TileMapLayer : public Node2D {
356355
#endif // DEBUG_ENABLED
357356

358357
bool _scenes_was_cleaned_up = false;
359-
void _scenes_update();
358+
void _scenes_update(bool p_force_cleanup);
360359
void _scenes_clear_cell(CellData &r_cell_data);
361360
void _scenes_update_cell(CellData &r_cell_data);
362361
#ifdef DEBUG_ENABLED
@@ -376,7 +375,7 @@ class TileMapLayer : public Node2D {
376375
// Internal updates.
377376
void _queue_internal_update();
378377
void _deferred_internal_update();
379-
void _internal_update();
378+
void _internal_update(bool p_force_cleanup);
380379

381380
protected:
382381
void _notification(int p_what);

0 commit comments

Comments
 (0)