Is there a bulk way to re-save map files after editing tilesets

So a bit of context. Currently, we save the tilesets as separate files and we have a lot of map files using these tilesets. The problem is that when we modify the tilesets, especially when we add or remove a tile, the id of the tiles change. So we have to manually open each map files and save them one at a time to fix the gid on those map files.

Is there anything we’re doing or wrong? Or if this is the way it’s meant to be, is there a way to automate this process(like can we make a script to automate this)?

What kind of tilesets are these, and what exactly are you doing that changes the tile IDs?

Image Collection tile IDs shouldn’t change when you add/remove tiles.

Based on Image tilesets have stable IDs as long as you don’t move tiles around within the image and don’t change the image width, so you should remove tiles by erasing their pixels in the image but not moving the other tiles around, and add tiles by putting them in the available empty spaces or by extending the image vertically. If you do need to widen the image, Tiled will prompt you to adjust the tile IDs in all the open maps, so make sure you have all the maps open before you modify the image and Tiled will handle that for you. Of course, this will only mean anything if the positions of the tiles within the image stay the same.
If you move tiles around in the image, then Tiled has no way to know that tiles changes, and you’ll need to write a script that replaces the old IDs with new ones, since there’s no other way to tell Tiled which tiles are which now. Scripts can be written to work on all open maps by iterating tiled.openAssets (make sure to check that the asset isTileMap!), but there’s currently no way to automatically open all the maps in a project, you’ll need to do it yourself, or code a full list of maps for your script to open.

It sounds like your loader is somehow bothered by the fact that extending a tileset with more tiles will increase the GIDs of tiles from other tilesets, which are referenced after the tileset that got extended, upon saving a map file.

The fix for this is not to re-save all the maps with the increased GIDs, but to fix your loader such that it does not need this. If the tileset and the local tile ID are resolved according to the documentation, then re-saving of the maps is not necessary:

To figure out which tileset a tile belongs to, find the tileset that has the largest firstgid that is smaller than or equal to the tile’s GID. Once you have identified the tileset, subtract its firstgid from the tile’s GID to get the local ID of the tile within the tileset.

Without knowing anything else about your project, or about the actual problem you’re running into, I’m guessing your loader may be iterating the tilesets array front to back, whereas to find the tileset with the highest firstgid, you generally need to iterate it back to front.

Yes, this sounds like my issue. I’ve written my loader so that it creates a dictionary that merges and resolves the GIDs as the index. So if the firsts tilesets have added new tiles, their GIDs overflow to the subsequent tiles.

Will fix my loader. Thanks for the help.

1 Like