mapeditor / tiled

Flexible level editor
https://www.mapeditor.org/
Other
11.26k stars 1.76k forks source link

Scripting: Make it easier to modify an Asset in scripted exporters #3986

Open eishiya opened 5 months ago

eishiya commented 5 months ago

The Asset you get as an argument to MapFormat.write() and TilesetFormap.write() is read-only, which makes it difficult to make temporary last-second changes to the map/tileset. Theoretically one could try to use the assetAboutToBeSaved signals to trigger such changes instead, and undo them after the asset is saved, but no such signals fire on export (see #3984), and a script may need to do substantial changes that might not be easily Undoable, all this makes for unwieldy, overcomplicated scripts.

There are two possible solutions I see:

  1. Make the argument Asset editable. When any export options are applied, it's already a copy, so it could safely be made editable. However, this means a copy would need to be made even when otherwise the original would be passed in, even if the export format doesn't need to make changes to it. Most likely only a minority of custom formats need to make changes to their Asset, so this seems wasteful.
  2. Add Asset.clone(), which creates a deep clone of the Asset. This isn't as intuitive, but it would have wider applications than just export formats, and avoids making unnecessary copies of the saved Asset. If this approach is taken, the write() methods' documentation should mention it as a way to get a modifiable copy of the Asset if one is needed.