# Tiled appears to be replacing certain tiles with nonexistent ids

**URL:** <https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785>\
**Category:** Questions\
**Created:** [October 24, 2017, 3:30pm UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785 "2017-10-24T15:30:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicohman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/nicohman/32/1502_2.png) [@nicohman](https://discourse.mapeditor.org/u/nicohman)\
**Post date:** [October 24, 2017, 3:30pm UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785/1 "2017-10-24T15:30:43Z")

</div>

Some of my tiles in my map appear to be working and fine in the editor, but when I export it to JSON, the definition for the tile id just doesn’t exist in the tileset file. Why is this? How do I fix this?  
Edit: This only happens when they are the tile before a blank space or tile. I’m subtracting firstgid to get the tile id right.

---

<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:** [October 26, 2017, 9:57am UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785/2 "2017-10-26T09:57:22Z")

</div>

Hmm, no idea what could be happening here. Can Tiled itself load back the JSON file properly? If so then there is more likely some problem with the way you are loading the data.

Could it be that you’ve flipped a tile, which is exported by using the high bits in the global tile IDs, leading to very large numbers? Though that wouldn’t explain why this would only happen before a black space.

---

<div class="post-metadata">

**Author:** ![faulknermano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/faulknermano/32/1037_2.png) [@faulknermano](https://discourse.mapeditor.org/u/faulknermano)\
**Post date:** [October 31, 2017, 6:38am UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785/3 "2017-10-31T06:38:39Z")

</div>

This reminds me of one my initial development stages where I was figuring out if I could depend on Tile IDs to reference a tilesheet externally. I don’t know if that’s the case here, but I’ll explain my experience just in case it proves useful.

Tiled’s Tile ID is ‘volatile’, in such a way that during the development of a map, through adding and deleting sprites, the tiles’ IDs will change and it breaks the original sequence. That means I couldn’t assume that referencing tile ID # 9 (ie tile\_id - firstgid) refers to the 9th image in the tileset. In fact, there could be _no_ ID # 9. There have been situations where I’ve gotten broken sequences, like 1, 3, 7, 10, etc.

So what I’ve learned is that there is no implicit mapping between Tile IDs and the integer sequence in which images appear in a tilesheet. You’d need to lookup a given Tile ID back in the TMX/JSON, and query the `source` attribute.

My game engine is C2, and I’m fortunate the 3rd-party TMX loader I’m using already allows me to populate a TMX Class object with the source image so that for any given tile, I know the source image path, and use that to instantiate my sprite.

---

<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:** [October 31, 2017, 7:07am UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785/4 "2017-10-31T07:07:59Z")

</div>

> [@faulknermano](#):
>
> In fact, there could be no ID # 9. There have been situations where I’ve gotten broken sequences, like 1, 3, 7, 10, etc.
> 
> So what I’ve learned is that there is no implicit mapping between Tile IDs and the integer sequence in which images appear in a tilesheet. You’d need to lookup a given Tile ID back in the TMX/JSON, and query the source attribute.

This is correct for _image collection_ tilesets, since when you remove an image it doesn’t change the IDs of any other tiles, affectively creating a gap in the ID sequence. This is to make sure references to the other tiles (which could be in maps that aren’t currently open) don’t break.

For tilesets based on a tileset image, the ID simply works as an index and there are no gaps.

---

<div class="post-metadata">

**Author:** ![faulknermano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/faulknermano/32/1037_2.png) [@faulknermano](https://discourse.mapeditor.org/u/faulknermano)\
**Post date:** [October 31, 2017, 9:10am UTC](https://discourse.mapeditor.org/t/tiled-appears-to-be-replacing-certain-tiles-with-nonexistent-ids/2785/5 "2017-10-31T09:10:50Z")

</div>

I see! Ok that makes sense. Thanks for that information and clarification. For my project, I have had to use image collection exclusively, so I didn’t appreciate the difference.
