Why do you use TMX over JSON?

Hello,

I am a fairly new programmer and I’m currently working on a simple 2D RPG, written in c++. The maps are tile based and I love using the Tiled editor to create the maps!

At the moment, my maps save just very basic information: Position of tiles and objects (in any layer), collision, animated tiles (currently with a custom way I designed before realizing that one of the newer updates of Tiled added an animation feature - I will use the given feature now).
Without thinking about it, I saved the maps as *.tmx, because the tutorials I read also did that.

However, looking at the file size, TMX/XML seems to be relativly unefficient. My 100*100 TMX-Map is about 500kb large, while the same map ist only about 100kb large if saved as a JSON file.
I don’t know much about JSON. Is there any reason why maps are usually saved as TMX files? What are the advantages of TMX/XML compared to JSON?

Thanks in advance,
Smofe

I’m sorry if there are any big language mistakes, because I’m not a native English speaker. I hope you understand my question :slight_smile:

You must have set the “Tile layer format” to “XML” when you created the map for the file to get that large. When you leave it on the default “Base64 (zlib compressed)”, you should see that the TMX file is actually much smaller than your JSON file. The “CSV” setting should yield relatively the same size because the JSON file essentially also uses CSV for storing tile layer data.

You can change the tile layer format of existing maps via Map → Map Properties → “Tile Layer Format”. This property only affects the TMX file format.

Oh, you are right. I probably did this because of the tutorial I read and I didn’t really thought about that, thanks!

Base64 (zlib compressed) file is about 2.8kb large!

Sorry if this sounds dumb: I never workes with compressions and/or Base64, can you explain me the basic way to make these maps work in a program?
Is it made like this:

  1. Load file into the game
  2. Decompress zlib
  3. Decode Base64
  4. Use the file to load in stuff (just like an XML? )

…or am I completly wrong about that? And if I am right, do I just use a parser/decoder to make a XML from a Base64 (zlib compressed)?

Thank you,
Smofe :sunny:

You have step 2 and 3 in the wrong order. The base64 encoding encodes the binary data in a way that it can be embedded in the XML file. After decoding it, it needs to be decompressed.

Also, this process only applies to each tile layer. The rest of the file is still plain XML. The result of decompressing the tile layer data is not XML, but binary data where each 4 bytes make up one global tile ID.

You can read the full details at TMX Map Format · mapeditor/tiled Wiki · GitHub

Okay, I think I understand it know. I will try to rewrite my map parsing methods then, because the difference in file-size is just too big to be ignored :slight_smile:

Thank you very much! :slight_smile: