Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image collection id gaps #3864

Open
duarm opened this issue Jan 2, 2024 · 3 comments
Open

Fix image collection id gaps #3864

duarm opened this issue Jan 2, 2024 · 3 comments
Labels
feature It's a feature, not a bug.

Comments

@duarm
Copy link

duarm commented Jan 2, 2024

Is your feature request related to a problem? Please describe.
Image collections are too bothersome to use because of tile id gaps, you need to remove them in order to index each tile. This must be done as build step (after tiled exports the tmj), or during startup.

Describe the solution you'd like
Tiled before 0.15 (according to the wiki), had consecutive ids. The feature was removed because there was no way to guarantee every map was loaded in order to update its tiles if I got it right. Discuss solutions/workarounds. I'm using a lua script which removes the gaps after exporting, I wouldn't mind a builtin solution along the lines. (Tiled script)

@duarm duarm added the feature It's a feature, not a bug. label Jan 2, 2024
@eishiya
Copy link
Contributor

eishiya commented Jan 2, 2024

Nitpick: You're talking about local tile IDs, not GIDs - the gaps in tile IDs just indirectly create gaps in GIDs as well. You would generally never use a GID as an index into an array of tiles (i.e. anywhere where the gaps would matter), because while multiple Maps may use the same Tileset, they may use different GIDs to refer to those tiles.

Why do you "need to" remove the gaps? Is it something to do with how you're storing and accessing the tiles in your code?

Regardless of need, I agree that it would be nice to be able to renumber the tiles in "image collection" Tilesets to remove gaps and "bake in" any tile reordering that's been done with the Rearrange Tiles tool. But Tiled would then need to provide some method to update all the maps using those tilesets, which is not as simple as it sounds, because Tiled can't know about the state of any maps that aren't currently open in the editor. Since Tiled has Projects now, perhaps it can prompt whether you'd like to adjust all the maps in the current Project when you choose to renumber tiles. Some discussion related to this is in #2866, which is about "based on tileset image" Tilesets, but perhaps a similar approach could be used for this - both involve some form of oldTile->newTile mapping that is only valid for a single moment in time, after all.

In the interim, I'd like to suggest a couple of workarounds you didn't mention in your post:

  1. If you need this due to your code, the most obvious is for you to use a data structure that can deal with gaps, such as a map. In practice, accessing these data structures should be so infrequent that the performance impact of using a map instead of an array should be negligible. In some languages, sparse arrays are maps under the hood, so if you're using one of those, you could even just treat it as an array.
  2. A Tiled script. Rather than doing the renumbering as a build step after exporting the TMJ file, you could add a scripted Action to Tiled that removes the gaps and updates all the maps in your project. Since you can't renumber existing tiles, you'd have to create a new Tileset with the renumbered tiles, but this isn't complicated to do (and you can save it over the old Tileset while keeping the old Tileset open in Tiled until you're done using it). After that, you'd iterate all the maps in your Project, performing the replacement, and removing the old Tileset; for these steps, my Mass Replace Tiles script may be a useful reference or could even be used directly if you also build the old-new mapping it expects using your script.

@duarm
Copy link
Author

duarm commented Jan 2, 2024

Why do you "need to" remove the gaps? Is it something to do with how you're storing and accessing the tiles in your code? [...] If you need this due to your code, the most obvious is for you to use a data structure that can deal with gaps, such as a map. In practice, accessing these data structures should be so infrequent that the performance impact of using a map instead of an array should be negligible.

Yes, the performance is not negligible since I'm rendering a huge chunked tilemap, even so it's wasted perfomance since it can be done offline, I need to access the array for every tile, so 1. is out for me. It is still a solution of course.

  1. A Tiled script.

This seems like a good solution, I saw your mass replace tiles but it's too manual, as I'm frequently removing and adding tiles to the collection, I find it much easier to just edit the exported tmj. And I don't want to mess with tiled scripts and explode everything.

@eishiya
Copy link
Contributor

eishiya commented Jan 2, 2024

The script as linked is indeed "too manual", but the action or the code can easily be incorporated into another script that automates the entire process, and the creation of the mapping from one group of tiles to another is simple to automate.
Any action built into Tiled would also have to be manually triggered as well, this sort of update could not be done automatically - opening all those maps to update them is just too heavy to perform every time you remove tiles, even if it's done on the C++ side rather than in a script. In practice, there would be very little difference between a script that does what you need, and that being built into Tiled - either way, it would be an action you manually trigger. It can be a little faster if done natively, but that's the only difference.
Edit: You would also be able to trigger either one automatically via script, e.g. when the image collection is saved.

@duarm duarm changed the title Fix image collection gid gaps Fix image collection id gaps Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature It's a feature, not a bug.
Projects
None yet
Development

No branches or pull requests

2 participants