Another, more manual, way to do this is to copy (e.g. Save As) the map, and then do a map-wide Replace Tileset (either manually with the button, or via the scripting API’s map.replaceTileset. If the layers need to be in the original map (i.e. are extra layers in the same rules rather than another set of rules), their contents can be copied from the copy to the original - if you’re copying from multiple layers at once, Tiled will paste (or rather, stamp) into the same-named layers, favouring the selected layers in case of conflict (IIRC). When the goal is to make another set of rules rather than just modify a subset of layers in existing rules, this method is likely faster, but for modifying a subset of layers, your script is probably the way to go.
I noticed a minor problem in your code: you’re hard-coding the parameter for dialog.done()
, but
- you should be using
Dialog.Accepted
or Dialog.Rejected
for readability instead of hard-coding it
- you’re using the wrong values, you’re using 0 for accept when that’s equivalent to Rejected, and 1 for reject when that’s equivalent to Accepted.
This doesn’t affect the functionality, but it’s poor practice. You may also want to consider using dialog.reject()
and dialog.accept()
instead of dialog.done()
. done()
is meant for situations where the value comes from a variable.
Something else to consider for your specific need: since it sounds like it’s always the same tileset correspondences, you may want to do this work engine-side instead of Tiled-side. Instead of bloating your maps with a bunch of trivially computable tile data, you could have just the diffuse layer(s), and then create the other layers after loading the map in-engine - they should be the same meshes, just with a different tileset texture assigned. In fact, depending on your engine’s structure, you might even be able to avoid having separate meshes for each layer, by using the same mesh each time, with a different tileset’s texture. If you have multiple “sets” of tilesets (diffuse, normal, emissive, etc), you can either use the file name to figure out the tileset, or use custom properties on the main (probably diffuse) tileset that specify which other tilesets to use.
Of course, a downside to this method is that you won’t be able to preview your non-main tilesets in Tiled. This is unlikely to be an issue since in Tiled you’d only see the raw tiles and not their effects, so any real previewing would have to be done in-engine anyway. The major downside is that this approach requires some engine-side tweaks, but for some projects, they’re worthwhile.
If you want to do this Tiled-side, another option is to make a script that does these replacements instead of Automapping, since the logic is so simple. For example, you can make a script that runs every time a working map is saved, creates any missing layers, and adds/updates any incorrect tiles in them based on the diffuse layer. The additional layers can be created hidden so that they don’t get in the way until they’re needed. The downsides to this approach are there’s currently no good scripting alternative to Automap While Drawing so if you’re using that you should keep using it, and that it will do this work every time you save (or whenever you set it), which may be more frequent than you want it (if you’re just Automapping once at the end, for example) or less frequent (if you’re Automapping While Drawing or save very infrequently).