Tileset .tmx and .tsx

(Niall Lewin) #1


I’m writing a c# reader for my Tiled output, and I’m just wondering how others work with the tileset property.

So there is the “Tileset” element in the actual .tmx, but then also the “Tileset” in the .tsx. This how thrown me off a little as I’ve resorted to having 2 separate properties, which doesn’t seem right

.tmx class

public Tileset_Short[] Tileset_Short;

public List<Tileset_Full> Tileset_Full;

public static TiledMap Load(string path, string fileName)
  // Deserialize it, and return the TmxMap instance.
  var xml = new XmlSerializer(typeof(TiledMap));
  using (var stream = new StreamReader($"{path}/{fileName}"))
    var instance = (TiledMap)xml.Deserialize(stream);

    instance.Tileset_Full = new List<Tileset_Full>();

    var tilesetXML = new XmlSerializer(typeof(Tileset_Full));

    foreach (var tileset in instance.Tileset_Short)
      using (var tilesetStream = new StreamReader($"{path}/{tileset.Source}"))
        var tilesetInstance = (Tileset_Full)tilesetXML.Deserialize(tilesetStream);


    return instance;

[XmlRoot(ElementName = "tileset")]
public class Tileset_Full
  public string Name;

  public int TileWidth;

  public int TileHeight;

  public int TileCount;

  public int Columns;

  public Image Image;

[XmlRoot(ElementName = "tileset")]
public class Tileset_Short
  public string FirstGId;

  public string Source;

So as you’ll be able to see in my “Load” method, I have to deserialize the “Tileset_Full”, and that leaves me with information split between my 2 “Tileset” properties.

Am I going about this the wrong way?


(Jonathan Bayle) #2


each tilesets (<tileset> root element) should be their own .tsx files, and are referenced from the map (.tmx) by a <tileset> element having a source="" and a firstgid="" attributes.

It is also possible to embed a tileset in the map (.tmx) file, therefore the embedded <tileset> elements in that file do not have the source="" attribute, instead they contain the complete definition of the embedded tileset.

Is it clear?

See the documentation for the <tileset> element: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tileset

(Niall Lewin) #3

Thank you, @baylej. That was my assumption.

I don’t have access to this right now, but I’ll have a look later, and see if I can configure it to save out like that

(Niall Lewin) #4

I got impatient and installed Tiled!

To be clear to whoever may need this in the future, there is an “Embed” button on the “Tilesets” section, which moves the info from the .tsx to the .tmx

(Thorbjørn Lindeijer) #5

Right, since the tileset element is used both for describing a reference to an external tileset as well as to describe the actual tileset, you’ll probably just need to use the same class in this case and just make sure it has both sets of properties.

This is essentially what Tiled does as well (though in Tiled, the tileset does not have a “firstgid” property, since that is merely an artifact of the TMX format and is not necessarily used at runtime). But it also does not use metadata for automating XML parsing.