# How to know which tileset uses a tile in a tilemap

**URL:** <https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869>\
**Category:** Uncategorized\
**Created:** [December 15, 2020, 11:38am UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869 "2020-12-15T11:38:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [December 15, 2020, 11:38am UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869/1 "2020-12-15T11:38:14Z")

</div>

Hi, I have a tiled file with two tilesets:  
 ![2](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/f/fb23a9090a1d663ec4cdf4c45aafc42d73fae601.png)

I have two layers, in each layer I use one of the tilesets:  
 ![2](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/2/20093db3d769880329aca9d13823bad459a452e3.png)

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?

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [December 15, 2020, 1:16pm UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869/2 "2020-12-15T13:16:56Z")

</div>

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

```xml
 <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.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [December 16, 2020, 2:56pm UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869/3 "2020-12-16T14:56:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [December 16, 2020, 3:02pm UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869/4 "2020-12-16T15:02:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [December 18, 2020, 10:29am UTC](https://discourse.mapeditor.org/t/how-to-know-which-tileset-uses-a-tile-in-a-tilemap/4869/5 "2020-12-18T10:29:39Z")

</div>

Ok I understand, thanks as always.
