From 61f6005ca4501a7ad5c2db112ef9f46c490e364c Mon Sep 17 00:00:00 2001 From: Michael Alexsander Date: Fri, 20 Feb 2026 15:00:20 -0300 Subject: [PATCH] Fix undoing `GridMap` move operations resulting in deleted cells --- .../gridmap/editor/grid_map_editor_plugin.cpp | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/gridmap/editor/grid_map_editor_plugin.cpp b/modules/gridmap/editor/grid_map_editor_plugin.cpp index 7c30633702..0d45203ffe 100644 --- a/modules/gridmap/editor/grid_map_editor_plugin.cpp +++ b/modules/gridmap/editor/grid_map_editor_plugin.cpp @@ -705,7 +705,26 @@ void GridMapEditor::_do_paste() { orm = rot * orm; undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, node->get_orthogonal_index_from_basis(orm)); - undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position)); + + int prev_idx = node->get_cell_item(position); + bool used_for_preview = false; + + if (clipboard_is_move && prev_idx == GridMap::INVALID_CELL_ITEM) { + // If no cell is present, it could be because it was removed to show it in the preview. + // Search through the clipboard to check if one of them was in that same position. + for (const ClipboardItem &prev_item : clipboard_items) { + Vector3 prev_position = paste_indicator.begin + prev_item.grid_offset; + if (position == prev_position) { + used_for_preview = true; + undo_redo->add_undo_method(node, "set_cell_item", position, prev_item.cell_item, prev_item.orientation); + break; + } + } + } + + if (!used_for_preview) { + undo_redo->add_undo_method(node, "set_cell_item", position, prev_idx, node->get_cell_item_orientation(position)); + } } if (reselect) {