When inserting or deleting tiles from a tileset, is there a way to re-number all of them so the IDs are always sequential without gaps? If there is a way then help would be appreciated.
Rearranging them is only visual and doesn’t renumber them. Let’s say you delete tile 37, the tiles now go …35,36,38,39,40…
I use enums in my code to name all the tile IDs, and it would be nice if I can list out all names in the enum without having to specify some of their ID values because there’s gaps in the ordering. I’m assuming this feature could be implemented since all objects on the map are referencing the tiles, so if the tile ID get changed, then all objects’ references would change too. I don’t understand why when you first add a tile, the ID needs to be set in stone for the rest of the project. I remember trying to change the values with a text editor in another project a while back, but that made a big mess of things.
Thanks for any help.
Sounds like you’re talking about Image Collections? Renumbering the tiles will break the existing references to those tiles in existing maps, which is why Tiled keeps gaps instead. Tiled maps use the tile ID to identify which tiles are used in the Tile Layers and Tile Objects, so if the IDs change, then the contents of the maps change, which is usually not what anyone wants. Keeping the IDs static for each tile means that you can remove and add tiles without worrying about messing up your maps.
If you need to renumber the tiles and avoid breaking maps, you could use a script such as this mass tile replacer to replace the old tileset with a new tileset that consists of the same tiles minus the deleted ones, which has been freshly made so it’s all in sequential order.
Perhaps it may be simpler to just have the enum and have the values for deleted tiles? You’d just never use those values, since they probably wouldn’t occur in your maps. At runtime, it wouldn’t make any performance difference.
Thanks for the reply. Yes, image collection. I understand how changing the IDs would mess up the map objects, but I’m assuming that this feature could be implemented if the devs wanted to where Tiled would just recalculate everything internally when tile IDs change. So if delete tile 37, then tile 38 now becomes tile 37 and Tiled could change all map objects that are linked to tile 38 to now link to 37. I’m assuming this feature wouldn’t be that hard to implement since we’re only talking about references. If the reference changes, those objects that reference to it should automatically change too, no? At least it sounds simple in principle, maybe adding this feature to Tiled’s existing engine would be cumbersome or impossible.
I also thought of the same thing, just adding blank entries to the enum where the deleted tile name used to be. I don’t mind doing that, it looks ugly but it doesn’t matter I guess.
I’ll take a look at that script you wrote, looks handy.
Thanks a lot
Tiled has a similar feature for image-based tilesets. Tiled attempts to fix any open maps when you change change the image such that the column count changes. This is often a source of confusion, as Tiled only fixes currently-open maps, and offers no way to change any maps that weren’t open at the time. The automatic fixing is a source of so many problems that
- bjorn considered removing image-based tilesets entirely in favour of tile atlases, so that tile IDs could always be stable and never change. (Issue #2863)
- When I convinced him to keep image-based tilesets as an option even if tile atlases are added, we agreed that this fix should not be applied to maps automatically, and should be something the user can do manually, which would require them to enter the old and new column counts (Issue #2866). The script I linked above was created in part as an interim way to update from an old tileset to a resized version with different tile IDs.
The main source of problems is that Tiled has no way to know whether a given map needs to be updated or whether it’s already up to date, since all it has is the tile IDs.
With Image Collections, because renumbering would change the tile IDs in a much less predictable way than resizing a tileset image does, there’s no simple way to make updating maps a manual action, it’s really something that would have to be done when the tileset is renumbered, which again raises of the problem of not always knowing whether a given map needs to have its IDs updated or whether it’s fixed already. Or it would have to spit out a JSON file with oldID->newID remappings, which one could then feed into an action or script that updates maps, which would feel like a needless complication in the simple but unusual scenario where the user has all the maps that would need fixing open prior to deleting tiles.
It’s not particularly complicated to iterate all the tiles and update them from an old ID to a new ID, the problem is making sure that the UX isn’t a confusing mess, and that this action is done only when it’s actually needed.