Layer data base64

Hi, why does tiled encode the layer data in base64?
How I can avoid this?

Captura

This is done for compressed data, because otherwise the binary data could contain characters which would be invalid in XML and JSON.

If you want to use uncompressed, unencoded data, set the Tile Layer Format format to CSV in the Map Properties. This will make your files much larger though, so I recommend sticking with the compressed formats unless your engine is for some reason lacking any ability to decompress the data. If you do decide to use compression, I regommend gzip or zlib rather than the Zstandard you currently have selected, as they’re more widely supported.

1 Like

Ah, but exporting as CSV does not save the state of the tiles (flipped horizontal or vertical).

So how do I select the type of compression? If I want to use zlib, how do I do it?

If you select CSV as the Tile Layer Format in Map Properties, as I said, it’ll include flipped tiles. It’s only the CSV format export that does not, but that is an entirely separate thing.

You can access the Map Properties via Map > Map Properties.... You’ll find the Tile Layer Format field there, with the options I mentioned in my previous post.

1 Like

Thanks for the help, ok I am using base64 for the data.

Now I get the data correctly but … how do I find out if the tile is rotated or flipped? Where do I get that information?

That data is included in the tileID.

The tileID consists of the global tile ID and the flip flags. I recently explained how to extract them here:

1 Like

Well i do not understand.
The values I get in CSV shouldn’t they be different? since the tile is in various shapes flipped horizontally and vertically but still I get the same value “452”.
Captura Captura2 Captura3

I want to export for CSV and obtain the values that indicate the state of each tile.
It’s not possible?

It looks like you exported as CSV, which does not keep the flips. The CSV export is completely separate from the Tile Layer Format CSV. If you want CSV with the flip data impact, you need to export as TMX with Tile Layer Format CSV and copy the data from there, or write a custom CSV format that keeps the flip data.

Ok but does it make any sense that the CSV export doesn’t save the state of the tiles?
That is to say:
-If you export in CSV the state of the tiles is not obtained
-If you use CSV to export tile layers then the state of the tiles is obtained

Presumably the CSV export was designed to solve a specific export problem rather than to be a generic file format, just like any other custom file format. It’s possible the CSV export was designed for some engine that doesn’t flipping tiles. Just like CSV doesn’t include object layers and map metadata, it doesn’t include tile flipping.

The CSV tile data format, on the other hand, is not an export format, it’s simply the way the tile data is stored within the TMX, the file format native to Tiled. Because it’s still TMX, it has to support all the features of TMX, and this includes flipping.

CSV export and CSV data format are just using the same data representation (CSV) to achieve different goals. That’s also why, for example, TMX with CSV format is still just one file, while CSV has to export multiple files (one per layer). They’re different in what they do and how they do it, they just happen to both use comma-separated values (CSV).

That said, if the current CSV format is included with Tiled, I don’t see why a version of it that includes flip flags cannot be. @bjorn Any chance of this being added?

In the meantime, @The_Mnk, what are you actually trying to achieve? You started this thread asking what the base64 data is, now you’re asking about CSV. What do you actually want to do? Are you looking into parsing TMX? Do you want just the layer data without all the other stuff TMX includes? I suspect we can get your problems resolved much faster if you explain what your end goal is.

1 Like

Ok I understand.
I need to get the state data of each tile with the export in CSV, that’s all.
“File>Export as CSV”

I tried with JSON but JSON includes a lot of data that I don’t need. Also the issue got complicated when unzipping base64 due to a datatype issue.

I am trying to modify this code to get what I want but I don’t know how to do it.
Apparently the code is already saving the ID of each tile but that ID does not include the state of the tile (flipped horizontally or vertically etc …)

When you decode the base64 data, you get a compressed bunch of binary data. If you decompress that, you get a binary bunch of ints. In short, a ready-made array of tile IDs that you can put directly into memory, without ever parsing it as a string beyond the initial base64 part. IMO it’s actually the easiest option for using Tiled layer data in a program, as it requires the least manual parsing, you get your data in a form your program can use directly.

If you require a CSV with flip data and without any extra data, you’ll need to write a custom export format. If all your maps have only one layer, this is very simple. If you have multiple layers, you’ll need to decide whether you want to store them in one file or multiple, and how to tell your program what the layers are.

I’d rather do it in CSV. I don’t know javascript or python, the only thing I could do is modify the python example script that I put before so that it exports the information I need. I think it will be easy to modify it but I do not know python. Hope bjorn can help out.

I’m more familiar with JavaScript than Python, so here’s a JavaScript CSV exporter that includes the flip flags:

var customMapFormat = {
    name = "CSV with Flip Data",
    extension = "csv",
    
    write: function(map, fileName) {
		var prefix = fileName.search(".csv");
		if(prefix >= 0) prefix = fileName.substr(0, prefix);
		else prefix = fileName;
		var numLayers = 0;
		for (var i = 0; i < map.layerCount; ++i) {
            var layer = map.layerAt(i);
            if (layer.isTileLayer) {
				var file = new TextFile(prefix+"_"+numLayers+"_"+layer.name+".csv", TextFile.WriteOnly);
				var output;
                for (y = 0; y < layer.height; ++y) {
                    output = "";
                    for (x = 0; x < layer.width; ++x) {
						if(x > 0) output += ",";
						var cell = layer.cellAt(x, y);
						var tile = cell.tileId;
						if(cell.flippedHorizontally) tile = tile | 0x80000000;
						if(cell.flippedVertically) tile = tile | 0x40000000;
						if(cell.flippedAntiDiagonally) tile = tile | 0x20000000;
						if(cell.rotatedHexagonal120) tile = tile | 0x10000000;
                        output += tile;
					}
					file.writeLine(output);
                }
				file.commit();
				numLayers++;
            }
        }
	}
}

tiled.registerMapFormat("CSV with Flip Data", customMapFormat);

Just like the included CSV exporter, it exports a separate CSV file per tile layer, and uses 0-based indexing for tiles, i.e. the same index they’d have in a tileset, and -1 denotes empty cells.
Unlike the included CSV exporter, it includes the layer number in the filenames so that the layer order can be preserved when you load the files, and it includes the flip flags. Extract them and the tile ID the way shown in the post I linked earlier in the thread.
This code only includes a “write” method, it does not include any way to read the data back into Tiled. So, only use this as an export format, don’t use this as your main format for WIP maps.

To use this, save the code above to a file with the extension .js, and put it in your extensions directory. The location of that depends on your OS, the documentation will tell you where to look: https://doc.mapeditor.org/en/stable/reference/scripting/#scripted-extensions

2 Likes

Ok I have it! Thanks for this, now I can continue my little project.
There is no place to publish this extension ?.
I mean, the Tiled website doesn’t have a section to publish these user extensions?

There’s the Tiled extensions Github repo, but that’s a hassle to submit to.

Is it really? Is there something I could do to make it easier? I think your custom CSV format example would be a great addition!

The CSV export was included originally for somebody who wanted to export maps to Excel I believe, and it supports setting a name on each tile so that you can export using those rather than tile IDs. Indeed support for flipping did not make sense in that use-case.

But I guess when we’re exporting using the tile IDs adding flipping bits might be more useful than stripping them out, even if it results in big numbers that will confuse some people.

So this will be included in Tiled 1.5, but using a custom script for this is probably a better idea.

2 Likes

In my case it was preferable to use the custom eishiya script because I needed to make the file extension txt (not CSV) for Godot.

Hi, I think you forgot to include one of the transformations? specifically “RotatedHexagonal”.
Although it is easy to include it manually I think.
Captura
Captura