Get tile count from exported json file

Hi there,
I’m writing a script that needs to read how many tiles are in a tileset.

After I export the tileset, I notice that the “tiles” array only contains the tile if the tile has been given a property. Otherwise the tile isn’t included in the array.

The work around it to simply assign each new tile with a dummy custom property. Is there another way to get the tile properly exported?

Thank you!

You can probably use the tilecount property on the Tileset object. It stores the total number of tiles in the tileset.

Just note that in case of “image collection” tilesets, this does not indicate the largest local tile ID, since when tiles are removed from a tileset their IDs are not reused, creating gaps in the sequence of tile IDs.

Hi bjorn,
Thanks for the reply. The tilecount for some reason is 100. But I only have 9 actual tiles. I guess its counting all the empty tiles.

Is it possible I get up the file incorrectly when I created it?

Hmm, this property saves the number of tiles in the tileset, including any empty tiles if you have empty areas in your tileset image.

To tell whether you have set up something incorrectly I’d need to see your tileset and your setup.

Yes, I have empty areas in the associated png. I can trim the image so that there’s only 1 row of tiles, but as soon as I create another empty row, Tiled will count those empty rows as well, right?

I just created an account, so it doesn’t look like I can upload anything.

I just saw that the tilesheet itself can have some custom properties, so I may just use that to contain the actual tile count.

Right, Tiled doesn’t automatically filter out empty tiles. I don’t think it would be a good idea, because in some cases you may want to actually use an empty tile. Also, if the empty area is somewhere in the middle of your tileset and you later decide to draw a tile there, then this should not cause the tile IDs of subsequent tiles to change.

If you really need to know the number of non-empty tiles in your tileset then indeed setting a custom property for this would work.

The minimum trust level to upload images is set to 0 (new user), so at least that should work, up to a maximum of 3 images per post.

1 Like