How does Tiled decided the orded of a tileset firstgid? can I change it?

Hello,
I am making a physics 2d engine for axmol. In my first Tilemap I only had one tile set which corresponds with my collision tiles. So that tile set begins with firstgid = 1

 <tileset firstgid="1" source="collision_tileset.tsx"/>

Then in my c++ code I detect which tile my sprite is one by checking the GID, which is something like this

if (metaLayer->getTileGIDAt(lowLeftCornerTileIdCoords) == 1)
    // do collision

Once I had my 2d engine working I was ready to create a map with game graphics tile sets. So I have many tile sets + my previous collision tileset. When I run the game
the collisions stopped working so that when I opened my new map and I saw this:

 <tileset firstgid="1" source="surface_tileset.tsx"/>
 <tileset firstgid="307" source="frontFiniteParallax_imgColl.tsx"/>
 <tileset firstgid="316" source="environment_imgColl.tsx"/>
 <tileset firstgid="329" source="surfaceSlopes_imgColl.tsx"/>
 <tileset firstgid="335" source="collision_tileset.tsx"/>

now my collision tile set id starts at 335, so no collisions are happening anymore.
My question is if I can force my collision_tileset to start at “1” in Tiled. Other wise I have to refactor the .tmx file. Or if you have any other advice, please let me know.
My collision GID tiles range is from 1 to 40. So when ever I do a new level, I want to make sure that the firstgid is assign to my collsion_tileset.

thx

1 Like

This is an implementation detail of the map format and there is no guaranteed way to force a specific tileset to have a particular GID. They’re assigned in the order that tilesets are in the map, which usually corresponds to the order the tilesets are added to the map, but it’s not guaranteed and relying on any particular order is not intended.

In general, you should use the firstgids as provided in the map, you should not be assuming a particular tileset will have a particular firstgid. The list of firstgids is right in the map, as you can see. You are presumably already using the list to deal with tiles from all the other tilesets, so your code that deals with your collision tileset should also use it.

2 Likes