# Option to export tile data with 0-based indexes?

**URL:** <https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672>\
**Category:** Questions\
**Created:** [August 20, 2017, 1:43pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672 "2017-08-20T13:43:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Richards](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@Eric\_Richards](https://discourse.mapeditor.org/u/Eric_Richards)\
**Post date:** [August 20, 2017, 1:43pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/1 "2017-08-20T13:43:04Z")

</div>

Is there some option to export tile layer data using a base of 0, instead of 1?  
I.e. In the exported data for a map, currently the first tile is referenced by an ID of 1:  
“layers”:[  
{  
“data”:[1, 1, 1, 5, 7, 1, 1, 1, 1, 1, 1, 5, 7, 1, 2, 3, 1, 1, 1, 5, 7, 1, 5, 6, 1, 1, 1, 5, 7, 1, 5, 6, 1, 1, 2, 11, 7, 1, 5, 6, 1, 1, 5, 6, 7, 1, 8, 9, 1, 1, 5, 6, 7, 1, 1, 1],  
}

Working from most non-Lua languages, the first entry in an array/collection is 0, not 1, so I’ve got to fixup all these IDs when indexing into my texture atlases and other things.

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [August 20, 2017, 2:06pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/2 "2017-08-20T14:06:44Z")

</div>

> [@Eric\_Richards](#):
>
> Working from most non-Lua languages, the first entry in an array/collection is 0, not 1, so I’ve got to fixup all these IDs when indexing into my texture atlases and other things.

Actually this index also starts at 0, but 0 means “no tile” / “empty cell”. So if you’re going to subtract 1, you need to check for 0 before otherwise you’re going to use an invalid index.

There is no option for disabling support for empty cells.

---

<div class="post-metadata">

**Author:** ![Eric\_Richards](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@Eric\_Richards](https://discourse.mapeditor.org/u/Eric_Richards)\
**Post date:** [August 20, 2017, 2:35pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/3 "2017-08-20T14:35:45Z")

</div>

Thanks for the explanation.  
I still think it is a little confusing, especially since if I select a tile in Tiled, under the properties window for that tile, the ID field is 0-based, and doesn’t match up with the exported data.  
 ![tile-properties](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/0/0ad2efca77a3974debc4771f583749467740738b.PNG)

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [August 20, 2017, 3:04pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/4 "2017-08-20T15:04:10Z")

</div>

> [@Eric\_Richards](#):
>
> I still think it is a little confusing, especially since if I select a tile in Tiled, under the properties window for that tile, the ID field is 0-based, and doesn’t match up with the exported data.

Right, but that ID is local to the tileset. It’s an index into the array of tiles in the tileset image, where there is no “empty cell” and 0 is simply the first tile.

The tile IDs saved in the TMX and JSON formats not only start at 1 because 0 is the empty cell, but they might also be shifted by larger amounts in case there are multiple tilesets used by the map. These are [global IDs](http://doc.mapeditor.org/reference/tmx-map-format/#data). But global IDs are not shown anywhere in Tiled because they are specific to the way the map is stored.

---

<div class="post-metadata">

**Author:** ![deleo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/deleo/32/2516_2.png) [@deleo](https://discourse.mapeditor.org/u/deleo)\
**Post date:** [November 3, 2019, 3:10pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/5 "2019-11-03T15:10:40Z")

</div>

today it took my quite some time that the layers.data numbers array isn’t linked to the id’s in the tiles array but rather to the index of said array, which confused me a lot.  
Is there a setting to make sure that these id’s start at 1 instead of 0?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 5, 2019, 8:49am UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/6 "2019-11-05T08:49:18Z")

</div>

> [@deleo](#):
>
> Is there a setting to make sure that these id’s start at 1 instead of 0?

No. The global IDs don’t really exist in Tiled, they are only a mechanism used by the TMX and JSON formats to allow a map to use tiles from multiple tilesets.

---

<div class="post-metadata">

**Author:** ![deleo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/deleo/32/2516_2.png) [@deleo](https://discourse.mapeditor.org/u/deleo)\
**Post date:** [November 5, 2019, 2:26pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/7 "2019-11-05T14:26:13Z")

</div>

Hi Bjorn,

thank you for your quick reply!  
After using multiple layers, it dawned that the firstgid of every tilelayer is used in combination with the individual tile id to get a global id which is used in the layers.data array.  
This is why the first id starts at 1, because the firstgid starts at 1, to account for empty tiles I guess (see attached picture for future references)  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/1/10e8fb78cb428ab94b33acdd323ec4e89ccdda86.png)

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 5, 2019, 3:00pm UTC](https://discourse.mapeditor.org/t/option-to-export-tile-data-with-0-based-indexes/2672/8 "2019-11-05T15:00:22Z")

</div>

> [@deleo](#):
>
> After using multiple layers, it dawned that the firstgid of every tilelayer is used in combination with the individual tile id to get a global id which is used in the layers.data array.

Yes, except I’m sure you meant to say multiple _tilesets_ and not multiple _layers_. 🙂

> [@deleo](#):
>
> This is why the first id starts at 1, because the firstgid starts at 1, to account for empty tiles I guess (see attached picture for future references)

Indeed, global ID 0 is used for empty cells so the first tile in the first tileset always gets global ID 1.

In addition, if you’re going to rotate or flip your tiles, note that [certain bits in the global ID are used to store this information](https://doc.mapeditor.org/en/latest/reference/tmx-map-format/#tile-flipping).
