How to know which tileset uses a tile in a tilemap

Hi, I have a tiled file with two tilesets:

I have two layers, in each layer I use one of the tilesets:

When I parse the TMX file in my game engine, I need to know which tileset each layer / tile uses. That is, if I have to put the tile number 300, does that tile belong to tileset A or B? How should i know this?

Each tileset has a firstgid property in the TMX, e.g.:

 <tileset firstgid="1" source="tilesetA.tsx"/>
 <tileset firstgid="512" source="tilesetB.tsx"/>

The tileset with the smallest firstgid equal to or greater than the tile gid you’re looking for is the tileset you want. In this example, if you have a tile with gid 300, it’ll be part of tileset A because 300 is greater than 1 but less than 512, the next smallest firstgid. A tile with gid 600 will be part of tileset B, because 600 is greater than 512. You will need to do this for each tile.

When parsing, remember to first read and strip the flipping flags before you do these comparisons, as the flags can make the gid look like a much larger number.

1 Like

Ok!, and why the first gid is 1 and not 0?.
I suppose that this value symbolizes the first tile of each of the tilemaps

gid 0 is the empty tile, so the first gid that represents an actual tile is 1, so that’s why the smallest possible firstgid is 1. The local tile ID (the ID within the tileset) which you get by subtracting the firstgid from the tile’s gid will start at 0.


Ok I understand, thanks as always.