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?
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.
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.