Custom JSON Export?

#1

Hey guys,

Just curious if it’s possible to edit a configuration file on what and how the data should be exported?

For example, I want a new array for each new row horizontally… and want to remove all the other properties that are saved, and only wanting the map data.

Thanks for reading!

(Thorbjørn Lindeijer) #2

I’ve answered this at:

https://github.com/bjorn/tiled/issues/1140

#3

Yeah, great bjorn! I have followed your advice to just create my own array / data instead of trying to custom code a C++ or python plugin.

Here is my script I made to convert something like this; (Could be for any amount of layers)

{
    	"data": [
    		[
    			[6, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
    			[0, 0, 0, 1, 0, 0, 1, 0, 100, 101, 0],
    			[0, 0, 0, 1, 0, 0, 1, 0, 115, 116, 0],
    			[6, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0]
    		],
    		[
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    		],
    		[
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    		],
    		[
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    		],
    		[
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    		],
    		[
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    		]
    	]
    }

This is the same type of DataSet used for play.treasurearena.com as well if you’re looking for awesome custom maps with collision and the 3d effect.

Here is the code in JavaScript:

MapData = {TILEDMAPJSONEXPORTDATAHERE}
var NewMap = {};
	NewMap.data = []; // Create Data key and set it as an array;
	var num_layers = MapData.layers.length;
	var map_width = MapData.width;
	var map_height = MapData.height;
 
function listToMatrix(list, elementsPerSubArray) {
    var matrix = [], i, k;

    for (i = 0, k = -1; i < list.length; i++) {
        if (i % elementsPerSubArray === 0) {
            k++;
            matrix[k] = [];
        }

        matrix[k].push(list[i]);
    }

    return matrix;
}
 
	var newData = [];
	for(var i = 0; i < num_layers; i++){
       		 newData.push(listToMatrix(MapData.layers[i].data, map_width));
    }

NewMap.data = newData; // Set the data property to grab the array matrix we built.

 console.log(JSON.stringify(NewMap));

And here is the working demo with the tiled map export: https://jsfiddle.net/e5yowcap/3/

  1. Make sure you export as json format as well.

Thanks to bjorn with the idea, and also if anyone see’s this thread you’re welcome!!

1 Like
(Thorbjørn Lindeijer) #4

Reviving this topic to point out that recent Tiled development snapshots allow writing export formats in JavaScript, making it very easy to add a custom JSON export.

An example can be found at the documentation for tiled.registerMapFormat:

var customMapFormat = {
    name: "Custom map format",
    extension: "custom",

    toString: function(map, fileName) {
        var m = {
            width: map.width,
            height: map.height,
            layers: []
        };

        for (var i = 0; i < map.layerCount; ++i) {
            var layer = map.layerAt(i);
            if (layer.isTileLayer) {
                var rows = [];
                for (y = 0; y < layer.height; ++y) {
                    var row = [];
                    for (x = 0; x < layer.width; ++x)
                        row.push(layer.cellAt(x, y).tileId);
                    rows.push(row);
                }
                m.layers.push(rows);
            }
        }

        return JSON.stringify(m);
    },
}

tiled.registerMapFormat("custom", customMapFormat)

The exact API is subject to change at least until the release of Tiled 1.3.