Search code examples
c#monogametiled

Accessing custom properties in 'Tiled' level map (C#, Monogame)


I've managed to read a file into my platform game made by the 'Tiled' level editor, using the TiledSharp library. I finally managed to get a level loaded and displaying properly in my game, but I can't work out how to access the custom properties I've set on tiles in the map. For example, I have a boolean property called 'Ignore' which specifies whether the tile should be ignored in collision calculations. I'd like to be able to read the 'Ignore' property of each tile as it's loaded. Something like this:

for (var i = 0; i < map.Layers[0].Tiles.Count; i++)
{
    Console.WriteLine(map.Layers[0].Tiles[i].Ignore);
}

However I can't find a way of accessing those properties. Can anyone help please? Thanks!

Edit: Here's a little of the contents of the file I'm reading in:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="16" height="9" tilewidth="80" tileheight="80" nextobjectid="1">
 <tileset firstgid="1" name="GroundTileSet" tilewidth="80" tileheight="80" tilecount="24" columns="6">
  <image source="GroundTileSet2.png" trans="df7126" width="480" height="320"/>
  <tile id="0">
   <properties>
    <property name="Ignore" type="bool" value="true"/>
    <property name="OneWay" type="bool" value="false"/>
   </properties>
  </tile>

Solution

  • I managed to solve the problem. I'm posting my entire Level class in case it helps anyone.

    using System;
    using TiledSharp;
    using System.Xml.Linq;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework;
    
    namespace Platformer
    {
        class Level
        {
            public Level(TmxMap map, Texture2D tileSet)
            {
                var tileMap = map;
                Texture2D levelTilesetTexture = tileSet;
    
                int tileWidth = tileMap.Tilesets[0].TileWidth;
                int tileHeight = tileMap.Tilesets[0].TileHeight;
                int tilesetTilesWide = levelTilesetTexture.Width / tileWidth;
                int tilesetTilesHigh = levelTilesetTexture.Height / tileHeight;
                GameInfo.gameInfo.bottomOfLevel = tileMap.Height * tileHeight;
                GameInfo.gameInfo.leftOfLevel = 0;
    
    
                int staticObjectSpriteWidth = tileMap.TileWidth;
                int staticObjectSpriteHeight = tileMap.TileHeight;
                Vector2 staticObjectBoundingBoxOffset = new Vector2(0, 0);
                int staticObjectBoundingBoxWidth = tileMap.TileWidth;
                int staticObjectBoundingBoxHeight = tileMap.TileHeight;
                bool oneWayPlatform = false;
                bool collisionObject = true;
                bool ignore = false;
                int drawLayer = 1;
    
                for (var i = 0; i < tileMap.Layers[0].Tiles.Count; i++)
                {
                    int tileNumber = tileMap.Layers[0].Tiles[i].Gid;
                    if (tileNumber != 0)   // If not empty tile.
                    {
                        tileNumber--;
                        var tileProperties = tileMap.Tilesets[0].Tiles[tileNumber].Properties;
                        string ignoreValue = "nothing";
                        string oneWayValue = "nothing";
                        tileProperties.TryGetValue("Ignore", out ignoreValue);
                        ignore = Convert.ToBoolean(ignoreValue);
                        tileProperties.TryGetValue("OneWay", out oneWayValue);
                        oneWayPlatform = Convert.ToBoolean(oneWayValue);
    
                        int column = tileNumber % tilesetTilesWide;
                        int row = (int)Math.Floor((double)tileNumber / (double)tilesetTilesWide);
    
                        float x = (i % tileMap.Width) * tileMap.TileWidth;
                        float y = (float)Math.Floor(i / (double)tileMap.Width) * tileMap.TileHeight;
    
                        PlatformerGame.game.CreateStaticObject(levelTilesetTexture, new Vector2(x, y), staticObjectSpriteWidth, staticObjectSpriteHeight,
                                staticObjectBoundingBoxOffset, staticObjectBoundingBoxWidth, staticObjectBoundingBoxHeight,
                                column, row, oneWayPlatform, collisionObject, drawLayer, ignore);
                    }
                }
            }
        }
    }