Why Tilesets don't have ID?

I see that Maps and Tiles have “id” property.
Is there a reason for the Tileset to don’t have such?

Do you mean the gid/firstgid? If so, that’s because tilesets don’t know anything outside of themselves, so they only need to deal with the tiles within them. A tile’d ID is essentially its location within the tileset, and some properties may need to be tied to it, such as animations and collision data. The map assigns firstgids to each tileset it uses so that multiple tilesets can be used within the same map. The tileset itself has no need for any of this.

1 Like

Ok so if I understand correctly.

Tilesets don’t have ID because they can be used on any map.
And these IDs may get in a collision with other Tilesets.

When we add multiple Tilesets to a map (3 for example) each of the Tilesets receives a “firstgid” - which is the “gid” of the first tile in the tileset.

If so in the context of a map we could use these firstgid to differentiate between tilesets.

I’m making an export plugin and I needed a way to identify tileset by something and i didn’t want that something to be the “name” property.

After quick re-check:
It seems that this property is not exposed to the JS scripting API.
@bjorn could it be added?

Well, we can’t really add a firstgid property to the tileset, because a tileset can be part of multiple maps, and for each map it may get a different firstgid.

In addition, there does not exist a firstgid property outside of the TMX and JSON map formats. These values get assigned while saving the map and are used while loading, but there would be no point in keeping them around.

If you just need to be able to refer to each tileset by a number, I would suggest you simply use their index in the map’s tilesets array. Would that work? Then, instead of this “global ID” system that the TMX and JSON map formats use, you could store each tile reference as a pair of “tileset index + tile id”.

Yes that’s exactly what i did:

 for (let index = 0; index < this.map.tilesets.length; ++index) {
        const tileset = this.map.tilesets[index];
        const tilesetID = index + 1;

offtopic: @bjorn I’m almost ready with the JS export plugin so I’ll send you a message for a quick code review when you have the time.

Sure, I’m looking forward to see what you’re making!