How to export as text value of block into 1D array?

Hi I’m a Java game developer and I just switched to Tiled instead of mappy, I have a few questions:

  1. Is there a way to export as text 1D array of ID of blocks in map?
    I want after drawing the map to export 1D array of all ID of blocks was drawed on map, of the form: “[” or “{” 0, 2, 2, 2, 1,… , 4 , 4, 1, 1,… “]” or “}”. 0, 2, 1, 4 … is the ID of block + 1, 0 mean this block is empty (not drawed, the corresponding default value of Tiled seems to be -1?!).
  2. Is it possible to set block properties as unpassable and output their positions (x, y) when exporting as text?
    I want to set the specified block’s property to impassable (i.e. the player cannot walk through it) and when I place those blocks on the map then export as text it export the x position and y of those blocks into a 1D array (two separate arrays), there will be an array of x positions and an array of y positions. It doesn’t matter which block has been specified as unpassable, I just need the array to list the entire unpassable position (x, y).
  3. Rotate block orientation and export as text block orientation value?
    I tried using Tiled, it’s great that it can directly orient blocks before drawing to the canvas without having to change the orientation of the original tiles.
    But after drawing the map, I want to export as text all the orientation of all the blocks that have been drawn on it into a 1D array (including blocks that don’t transform direction), with values ​​like below.
    Here is a list of directions and values ​​when exporting as text:
  • No transform is applied to the Sprite / Block (default). Export value of 0.
  • Causes the Sprite / Block to appear reflected about its vertical center and then rotated clockwise by 180 degrees. Export value of 1.
  • Causes the Sprite / Block to appear reflected about its vertical center. Export value of 2.
  • Causes the Sprite / Block to appear rotated clockwise by 180 degrees. Export value of 3.
  • Causes the Sprite / Block to appear reflected about its vertical center and then rotated clockwise by 270 degrees. Export value of 4.
  • Causes the Sprite / Block to appear rotated clockwise by 90 degrees. Export value of 5.
  • Causes the Sprite / Block to appear rotated clockwise by 270 degrees. Export value of 6.
  • Causes the Sprite / Block to appear reflected about its vertical center and then rotated clockwise by 90 degrees. Export value of 7.
    P/s: These values are similar to the Field Detail of the Sprite (MID Profile).

Thank you for reading and answering this question!

  1. I’m not sure what “blocks” you’re talking about (Objects? Tiles?), but it sounds like you might want to look into creating a custom export format.
  2. It’s up to your game/engine to parse the data into something you can use at run-time, so you can use any of a number of ways to denote collision, such as:
    • Assign collision shapes to the tiles in your Tileset using the Tile Collision Editor, and then build the overall collision shape in your engine after parsing the map. This is the intended way in Tiled and you can preview your collisions in the map editor, but has the disadvantage that a given tile must always have the same collision.
    • Use a separate Tile Layer for collisions, using a separate tileset or subset of tiles. You can build the collisions in-engine based on either collision data assigned in Tiled, or based on tile ID.
    • Use a separate Object Layer for collisions and draw the collision poylgons directly.
  3. Again, don’t know what “blocks” are. If you mean tiles, the default export formats already export tile IDs with flags to communicate their rotation/flip status. If you need them in a custom format, take a look at the link in (1).

The “blocks” I mean are tiles after they have been divided into tiles of the same size (sorry, the old tool called it block, I called it that too).
block
Can you explain how to do them?

If everything in your map is always made up of consistently-sized “blocks” and never of individual tiles, you could use Tiled’s metatile feature by creating a map with all the blocks on it and using that map as the source “image” of a new tileset. The result will be a tileset with larger metatiles that are made up of arrangements of your smaller tiles, and you can use these metatiles as you would other tiles. When parsing the map, you’ll need to translate these metatiles back into the constituent tiles to draw them.

If your maps need to mix multi-tile blocks with individual tiles, then you’ll have to place them as regular tiles, though you can save them as stamps to make selecting them easier.

The “Block” I mean are the tiles in the Tilesets.
Now, I want to export as text after drawing the map in the same format as in the question, what should I do?

For the tiles that I want to set as impassable, when I export as text, I simply export the position (x, y) of them already drawn on the map.
Can you show me how to export the values like in the question?

After a morning of studying the literature, I “seem” to be able to solve questions 1 and 2.
For question 2, I was thinking of adding a custom property called Unpass (bool) then read its value, if it is true then push its x, y value into the array.
This is the code I wrote:

const textPlainFormat = {
	name: "Text Plain",
	extension: "txt",
	write: function(map, fileName) {
		var tileID = [], unpassX = [], unpassY = [];
		for(let i = 0; i < map.layerCount; ++i) {
			var layer = map.layerAt(i);
			if(layer.isTileLayer) {
				for(let y = 0; y < layer.height; ++y) {
					for(let x = 0; x < layer.width; ++x) {
						var cell = layer.cellAt(x, y);
						tileID.push(++cell.tileId);
						var tile = layer.tileAt(x, y);
						if(tile != null) {
							var isUnpass = tile.property("Unpass");
							if(isUnpass) {
								unpassX.push(x);
								unpassY.push(y);
							}
						}
					}
				}
			}
		}
		var file = new TextFile(fileName, TextFile.WriteOnly);
		file.write('TileID = ' + JSON.stringify(tileID) + '\n');
		file.write('Unpass X = ' + JSON.stringify(unpassX) + '\n');
		file.write('Unpass Y = ' + JSON.stringify(unpassY));
		file.commit();
	},
}
tiled.registerMapFormat("txt", textPlainFormat)

As for question 3, I was thinking of using flippedAntiDiagonally, flippedHorizontally, flippedVertically, rotatedHexagonal120 of cell. For each attribute that is true, it will add a certain value. But since their names and ways of working are different from my question, I hope you can help me solve this question!
P/s: I’m not sure if the code I wrote above is correct or could be more optimized. If you have any suggestions or have an API that can make it more convenient, please let me know!

I don’t have the time to figure out your desired format and whether you’re writing it correctly, but I’m sure with some iteration you can get it working :]

As for the rotations, you’ll need to check the flags and write out the combinations you support, e.g.

let flags = layer.flagsAt(x, y);
let direction = 0; //default direction value
if(flags == Tile.FlippedHorizontally)
    direction = 1;
else if(flags == Tile.FlippedVertically)
    direction = 2;
else if(flags == Tiled.FlippedVertically | Tile.FlippedHorizontally) //180 degree rotation
    direction = 3;
else if(flags == Tiled.FlippedHorizontally | Tile.FlippedVertically | Tile.FlippedAntiDiagonally) //flipped vertically and then rotated 270 degrees
    direction = 4;
//... and so on

To figure out which flags correspond to which direction, place the tile with that orientation in Tiled, and then hover over it. In the status bar, you’ll see the tile ID and in square brackets, the current flags:
image
This one has H (horizontal flip), V (vertical flip), and D (anti-diagonal flip) all set.

Edit: There might actually be a logical correlation between the Tiled flags and your desired values. In the Tiled scripting API, H = 1, V = 2, D = 4. Perhaps there’s some manipulation you can do to do the translation in fewer lines. But a big ol’ switch or series of else ifs will also do the trick just fine.

I am not able to calculate the respective values of Tiled with Sprite Transforms on Java.


With Java, the documentation they provide includes explanatory pictures, which makes it easier to understand!
According to the documentation, RotatedHexagonal120 has a value of 8, which is not on my desired return list.

Please see my previous post for instructions on how to figure out which combinations of Tiled flags translate to each transformation you need. Feel free to ignore what I wrote in the edit to the post since it just seems to be confusing you, you can implement this as a full enumeration of the 8 values (0-7) that you need.

Well… I did draw all the oriented shapes like the one on the Java website and get their Flags values.
Amazingly the values are almost identical, here is the list of values:

Transform

The only difference is that of TRANS_MIRROR_ROT180 and TRANS_MIRROR, the values of both seem to have swapped places.
Is there a way to change back the value of both? Or is it in the source code and can’t be changed? If so, can I make a request to change the value back in a new version on Github?

No, these values can’t be changed, and I highly doubt this’ll be changed for compatibility with one library, especially when you’re writing your own export script anyway and can write out whatever values you wish. Just write out the values you need. Looks like you only need to look for two special cases and can write out the rest directly.

Currently, there are only two constants of TRANS_MIRROR_ROT180 and TRANS_MIRROR, their values are interchanged so they are different from the value of Java Sprite.
I’ve tried creating an “issues” on Tiled’s Github with the hope that they change flagsAt code so that flagsAt returns the correct values of those two constants (at least in the next version).
I find Tiled itself uses Java, and Java Sprite is also heavily used so if both are the same that would be great, I really don’t want to have to write separate if-else statements for those two constants.
UPDATE: Too bad, as of writing this reply, Github is still down, so I can’t check the status of that “issues”!

Tiled is written in C++, not Java (though there was a Java version at one point), and even if it were, that doesn’t mean it would use the same libraries that you’re using.

I can pretty much guarantee that your suggestion will not be implemented. As I mentioned in my reply there, Tiled uses flags for different flips, and if the two flags you want changed are modified, then some of the other values will change. Even though currently 6 of the 8 values happen to correspond, this is just a coincidence, and the two systems are actually very different and it would not make sense to convert Tiled to use a completely different system for flips just for compatibility with the library you’re using.

Output the values you need in your code. You don’t need Tiled to change the way it handles transformations.

I agree with eishiya’s thoughts. Backwards compatibility shouldn’t be broken to align with one particular game engine. You shouldn’t think of Tiled as something that is tailored toward one engine. It outputs its own standard which different engines can then interpret.

Well, if it’s not acceptable to change the return value, it’s a loss for Java programmers as they will have to write a bit more code to convert it back to Java. Sprite is a class in the offical Java package, not a 3rd party package (and certainly not written by me). So changing it is not only beneficial for me but also for others (but if not, that’s okay).

I think what’s not coming across is that the values in tiled only coincidentally share some values with the Java library you are using. Changing the values might save you a small amount of code but it will break everyone else’s code. I don’t think Tiled is designed to directly output compatible code with your chosen engine.

As you said, it only requires a bit of code to convert. In exchange, the format Tiled uses is easier to parse since it’s flag-based, and supports hexagonal rotations in addition to orthogonal ones.

Just about everyone using any language has to find or write a parser to import Tiled maps into their engine, or an exporter to export Tiled maps into a format their engine understands. Incidentally, have you looked into the Java TMX parsers that are already available? Depending on what exactly you need, maybe you don’t even need to write a custom export format.