# Problem with xml / tmx standard format

**URL:** <https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806>\
**Category:** Uncategorized\
**Created:** [November 2, 2020, 10:20am UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806 "2020-11-02T10:20:26Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 2, 2020, 10:20am UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/1 "2020-11-02T10:20:26Z")

</div>

Hi, I’ve noticed that the csv values in the XML file that tiled uses are initialized (and finalized) with a ‘\ n’ symbol.

In this capture you can see:  
 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/a/a5ca50062fff10bc99f454c51d22323ab47abcf4.png)

If I put the content of the file into an online xml indenter, it does this:  
 ![Captura3](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/1/11c018e7bba7c644f2b6089e0e00452dd8fc8f83.png)

It can be seen that the initial line break introduced by Tiled has been removed and it is now easier to obtain the values from an external program.

My question is why does Tiled introduce the initial line break ?.

This happens to me when I get an array with the string values from the CSV.

 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/0/0e186b78cd24e7fe6c591f6ee40beb911500b879.png)  
The first element ([0]) does not contain anything, it is empty, so I must use “-1” to discount that empty element.  
So maybe it’s because of that ‘\ n’ symbol?

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 2, 2020, 12:44pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/2 "2020-11-02T12:44:52Z")

</div>

I think you’re overcomplicating things in your code. You’re splitting the string by `\n` and getting a blank starting line as a result, even though the thing you’re splitting isn’t necessarily just your data. For example, some parsers require the first line to be _field names_, in which case that first empty line correctly tells them there are no names (though I don’t think that’s why it’s there, I think it’s there for legibility).

`trim()` `CSVData` before processing it this will get rid of any leading and trailing newlines, so you can be sure the string is just your data. Then you won’t even need to split it.

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 2, 2020, 2:16pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/3 "2020-11-02T14:16:37Z")

</div>

> [@The\_Mnk](#):
>
> My question is why does Tiled introduce the initial line break ?.

As @eishiya guessed, the line break was added for readability. This way the first line aligns with the rest.

Apart from trimming the data, you could also pass `StringSplitOptions.RemoveEmptyEntries` to the `String.Split` function.

But note that you should actually only split this data on commas. The newlines are not guaranteed to be there at all, and are left out when the “minimize export” option is enabled (and the file is exported). The newline per row is also only there for increasing readability.

Similarly, you shouldn’t derive `numCols` and `numFilas` (numRows) from this data. These values are available as the `width` and `height` attributes on the `layer` element.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 2, 2020, 4:00pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/4 "2020-11-02T16:00:17Z")

</div>

Thanks for all the info guys! but … why does each layer have width and height properties if all layers must be the same in the editor?  
There are width and height values for each layer and also in the \<map …\> element

I mean, just query the values of a single layer because the others will be the same.

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 2, 2020, 4:21pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/5 "2020-11-02T16:21:50Z")

</div>

Currently the data is meaningless/redundant (it has some relevance within Tiled for rendering), but it may become relevant in the future when/if support for differently sized layers or different tile sizes per layer is added.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 2, 2020, 8:24pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/6 "2020-11-02T20:24:47Z")

</div>

Ok I understand.  
I only have one problem to solve, I was able to optimize the code with the advice you both gave me.  
Now the CSV values for empty tile is 0, not -1.  
But if I export in CSV it is -1.  
I would prefer -1 for empty tiles.  
Can this be changed?

Here you can see, the empty tiles have the value 0 in XML format  
 ![Captura4](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/a/aa8831d4ce8a9cc8a2d8d94125bd868871e07f76.png)

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 2, 2020, 8:50pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/7 "2020-11-02T20:50:33Z")

</div>

No, this cannot be changed. The CSV export is designed for the scenario where you have exactly one tileset and none of the tiles are flipped, with -1 being the empty tile. The CSV format within TMX is designed with multiple tilesets in mind and with the ability to include flips. Due to how negative numbers are represented in computers, I think -1 for empty could get in the way of representing those flips depending on how the data is read in. Using unsigned numbers avoids any such issues with representation.

You should subtract the tileset’s firstgid from the tile ID before using it, to get the tile ID within the tileset. The firstgid is listed with the tileset in the map. If you only ever have 1 tileset per map, it’ll always be 1 I believe.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 2, 2020, 8:59pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/8 "2020-11-02T20:59:50Z")

</div>

Ok, you mean I have to subtract this value?  
 ![Captura3](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/1/13be7de7ce1979a93530648e462cdb6a2b1422e5.png)

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 2, 2020, 10:59pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/9 "2020-11-02T22:59:57Z")

</div>

No, that’s the next object ID, which is completely unrelated. You need to subtract the `firstgid`, which is a property of each tileset included in the map.

![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/1/10ae457234884d2b67f29e8036fc8e8dd24af48e.png)

This property is what allows you to identify which tileset a given tile is using, if your map has more than one tileset.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 3, 2020, 8:39am UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/10 "2020-11-03T08:39:29Z")

</div>

Ok, I understand, thanks as always!

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 3, 2020, 3:32pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/11 "2020-11-03T15:32:43Z")

</div>

Hello again, one more question.  
The values in CSV cannot be saved in int data, since they exceed the maximum value, is it true or do I have something wrong in my code that causes this in some way?

This value exceeds the maximum allowed value for int by 2 units.  
 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/b/baee1c5950b68ff52c54be4ec6b09812765ed7a7.png)

 ![Captura](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/7/72fa7b99cade9dfb29e44af98b229ecea4f8e2ae.png)

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 3, 2020, 4:39pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/12 "2020-11-03T16:39:17Z")

</div>

You should be using **unsigned** ints, then you’ll be able to store these values fully, as you’ll not be wasting a bit for the sign.

This sort of situation is what I meant earlier with the -1 empty value getting in the way - if you had to use signed ints just to be able to store -1 for “empty”, you’d lose use of 1 bit (for the sign), which can otherwise be used for flip flags while allowing many more tile IDs.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 3, 2020, 6:13pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/13 "2020-11-03T18:13:03Z")

</div>

Ok, solved using uint.  
Thank you.

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 4, 2020, 7:33pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/14 "2020-11-04T19:33:06Z")

</div>

Hmm now this of the layers of different sizes seems very useful to me, is there a plan for the implementation of this or is it just something that is separated?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [November 5, 2020, 10:10am UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/15 "2020-11-05T10:10:23Z")

</div>

It’s not really planned at the moment because it would only make the file a bit smaller while at the same time adding complexity to the editor. What makes it seem very useful to you?

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 5, 2020, 12:34pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/16 "2020-11-05T12:34:14Z")

</div>

It is a bit complicated to explain, and then I must translate with the google translator and see that everything is coherent … it is a burden to explain something a bit complicated !.  
But it would be useful for something I have made for Godot that reads tiled’s TMX files.

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [November 5, 2020, 2:23pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/17 "2020-11-05T14:23:37Z")

</div>

When combined with differing tile sizes per layer (another wishlist item for some users), I think the need for per-layer sizing is obvious ;D

Being able to set custom width/height in the editor would make it more convenient to edit parallax layers like I have in my game. Currently I have to draw borders with tiles to show the layer’s size to make it easier to compose them, and if I decide to change the layer size, I need to redraw those borders. Obviously I’ve got this workaround in place so I don’t _need_ the feature, but it would be nice.

@The_Mnk I have custom “width” and “height” properties, and when reading the map, I only load the tile data within that width and height. Perhaps something like that could work for you?

---

<div class="post-metadata">

**Author:** ![The\_Mnk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/the_mnk/32/2274_2.png) [@The\_Mnk](https://discourse.mapeditor.org/u/The_Mnk)\
**Post date:** [November 5, 2020, 3:49pm UTC](https://discourse.mapeditor.org/t/problem-with-xml-tmx-standard-format/4806/18 "2020-11-05T15:49:01Z")

</div>

Yes, that’s exactly what I thought.
