Skip to content

Fix "Detach templates" not working when export target is the same file#4466

Open
Hijanhv wants to merge 1 commit intomapeditor:masterfrom
Hijanhv:fix-detach-templates-same-file-export
Open

Fix "Detach templates" not working when export target is the same file#4466
Hijanhv wants to merge 1 commit intomapeditor:masterfrom
Hijanhv:fix-detach-templates-same-file-export

Conversation

@Hijanhv
Copy link

@Hijanhv Hijanhv commented Mar 25, 2026

Summary

Fixes #4456 — when Detach template instances and Repeat last export on save are both enabled and the export target is set to the same file as the map itself, templates were not being detached on subsequent saves.

Root cause (two interacting bugs):

  1. ExportHelper::prepareExportMap always clears exportFileName/exportFormat from the exported clone — so the file on disk loses the export settings. On the next reload triggered by the file watcher, those settings are gone, causing exportDocument to early-exit with an empty export filename.

  2. The export write updates the file's on-disk timestamp, but mLastSaved is only updated during the regular save. The file watcher therefore treats the export write as an external change and auto-reloads the document, which replaces the in-memory map (including its export settings) with the freshly-loaded one that has no export settings.

Fix:

  • In ExportHelper::prepareExportMap, preserve exportFileName and exportFormat in the exported file when the export target and the map file resolve to the same canonical path. This restores persistence across reloads and Tiled restarts.
  • Add Document::refreshLastSaved(fileName) and call it after a successful export to the same file (in exportDocument, exportMapAs, and exportTilesetAs). This prevents the file watcher from treating the export write as an external change and triggering an unnecessary auto-reload.

Test plan

  • Enable Detach template instances and Repeat last export on save in preferences
  • Open a map, do File → Export As… targeting the same .tmx file
  • Add template instances to the map, then press Ctrl+S
  • Open the saved file in a text editor — template objects should be fully detached (no template= attribute)
  • Press Ctrl+S again — templates should continue to be detached on each save
  • Close and reopen Tiled, reload the file — export-on-save should still work (export settings persisted in file)
  • Verify that exporting to a different file still clears export settings from the exported file (no regression)

When exporting to the same file as the map/tileset, two issues caused
the "Detach template instances" export option to stop working after the
first export:

1. ExportHelper::prepareExportMap would clear exportFileName and
   exportFormat from the exported clone, causing the file on disk to
   lose the export settings. On the next reload (triggered by the file
   watcher), those settings would be gone and export-on-save would
   silently skip the export step.

2. The export write would update the file's timestamp but not mLastSaved,
   causing the file watcher to treat the export as an external change
   and auto-reload the document (which cleared the in-memory export
   settings).

Fix both by:
- Preserving exportFileName/exportFormat in the exported file when the
  export target and the map file are the same (canonically equal paths).
- Calling refreshLastSaved() after a successful export to the same file
  so the file watcher ignores the export write and no unnecessary reload
  is triggered.

Fixes mapeditor#4456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Detach templates" does not work with "Repeat last export on save" when exporting to the same file.

1 participant