"Layers" exported as a Lua file => wrong tile IDs


As a new user, it looks like I can’t embed more than 3 medias in a same post… Too bad. I hope my message will be clear enough despite of that.

Exporting the tileset as a Lua file works as intended, the IDs and properties seem correct.

But after exporting the layers as a Lua file, I just noticed 2 weird things. Not sure if this is a bug or not… but it really looks like it (and maybe 2 different bugs), so… just in case I report them in the “bug” section.

1/ Wrong tile ID(s?).

My basic ground tile, used almost everywhere in the tilemap, is ID 129 (too bad I can’t display a screenshot of the tilemap in question… would have been more obvious - edit: but you can see it in my edited messages :smile: )

But for some reason, when exporting the layers as a Lua file, it is replaced by 130 (and maybe there are other wrong IDs? but this one is super easy to identify).



When exporting the tileset itself, it seems ok. ID 129 is the only “walkable” (for testing purpose).

The issue only exists when exporting the layers.

2/ Weird IDs in my exported layer data.


These IDs don’t exist in my tileset (and are not present in the tileset Lua export). IDs vary from 0 to 1023, as expected.

Overall, the tileset export seems to work as intended while there are “bugs” (I suppose?) when exporting the layers (wrong IDs).

PS: not sure at all, but I may have resized my tilemap (= removed a column), in this case it could have created a shift or something… This is something I’ve done with previous tilemaps, but not sure about this one.

I just shared a GitHub repository with the tilemap:

Hope it’ll help understand what’s wrong :slight_smile:

Wrote a function in Defold to convert my exported .lua files (layers & tiles) into a readable pathfinding grid :nerd_face:

My code seems to be functional, but most 0 should be 1 (“walkable” property - currently only assigned to ID 129 for testing purpose, as mentioned ealier) + the super weird IDs prevent me from having a usable grid.


Can’t wait to know if I messed up with the export or if this is a bug with Tiled :thinking: Maybe I’ll try with another map.

0 means empty/no tile, and other tile IDs are (firstgid + tileset-local ID + flip flags). This system is what allows you to distinguish tiles from multiple tilesets, and also efficiently stores tile flips/rotations.

The large numbers are tiles that have been flipped. You need to read the flags and then clear them before you can get the actual tile ID. You can read more about the flipping flags here: TMX Map Format — Tiled 1.7.2 documentation
(However, the documentation is a little wrong: you need to clear the largest four bits, 0x10000000 is only useful for hexagonal maps, but it is still a flag that you need to consider.)

After you’ve cleared the flags, you can check which tileset the tile is from by comparing it against the list of firstgids for the tilesets listed in the map. The largest firstgid that’s <= than the global tile id is the tileset you want. After you’ve identified the tileset, subtract its firstgid from the tile ID to obtain the tileset-local ID, which is what you’d use to actually get the tile properties from the tileset and to render the tile.

Thank you for your quick and precise answers!!

Ooooooook :slight_smile:

I think I’ll start with unflipped tiles only to learn the basics but I’ll get back to flipped tiles later, for sure! Thanks!

Ok… so just to be sure, I just started another map (for testing purposes, again) with only tiles id 0 & 1 (so are the ID in Tiled).

image => in Tiled, ID = 0 (same for ID = 1)
image => exported tiles (id= 0 & 1 - consistent with Tiled)

image => exported layer (id = 0/1 become 1/2)

image => defold

So just to be sure, I have to manually increment the tile ids (from the .lua tiles) to make sure they match with the data table, right? (if 0 is an “empty tile” then id = 0 becomes 1, id = 1 becomes 2 etc.)

Is this how it should be done? (no problem with that but just to be 100% sure I’m not doing it the wrong way)

So just to be sure, I have to manually increment the tile ids (from the .lua tiles) to make sure they match with the data table, right? (if 0 is an “empty tile” then id = 0 becomes 1, id = 1 becomes 2 etc.)

Sort of. You need to look at the firstgid as I described. Where the tilesets used by the map are listed, each one will have a firstgid property, this tells you what number you need to subtract from the global tile ID to get the ID of that tile within the tileset. If your map only has a single tileset, its firstgid will be 1, but it’s best to handle this properly, just in case you end up with multiple tilesets in a map.

The IDs 0/1 you see in Tiled and in the tileset data are the local tileset IDs. If you were to have multiple tilesets, they’d all start at 0, so you’d have a tile 0 in every tileset, maybe a tile 1 in every tileset, and so on.
The map, which may be using multiple tilesets, needs to distinguish between these different tiles from different tilesets, which is why it uses the global ID (gid).

Since what you need in the end are the local tile IDs within a tileset, you should be subtracting the firstgid from the global tile IDs in the map, rather than adding it to the local IDs. So, you’d go from 0/1/2 to (no tile)/Tile 0/Tile 1. Notice that I’m not just using plain numbers here - at this point you should probably no longer be dealing with numbers, but with however you’re representing the tiles in the engine (i.e. drawables and data structures).