MonoGame.Extended icon indicating copy to clipboard operation
MonoGame.Extended copied to clipboard

[Tiled] TiledMapTileSet.TileCount != TiledMapTileSet.Tiles.Count

Open Emersont1 opened this issue 5 years ago • 3 comments

I'm not sure If I'm going about this in the wrong way, but I have some custom properties in tiles

Once Its compiled and Loaded via MCGB and Content.Load, looking at the tilesets in the VSCode debugger shows the following: image

This means that When I try to load these custom properties in this example code

foreach (TiledMapLayer l in map.Layers.ToArray().Reverse())
            {
                var layer = (TiledMapTileLayer)l; // Bad form but given the example map this is fine
                Console.WriteLine(layer.Name);
                TiledMapTile t = layer.Tiles[X + Y * map.Width];
                if (t.IsBlank) continue;
                var tileSet = map.GetTilesetByTileGlobalIdentifier(t.GlobalIdentifier);
                Console.WriteLine(tileSet.Name);
                Console.WriteLine(tileSet.TileCount);
                Console.WriteLine(tileSet.Tiles.Count);
                var localgid = t.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileSet);
                var tile = tileSet.Tiles[localgid]; // This will sometimes go OOB on the array causing a crash
                if(tile.Properties.ContainsKey("traverse_difficulty")){
                    int p = Convert.ToInt32(tile.Properties["traverse_difficulty"]);
                    return p;}
                else 
                    return 0;
            }

Emersont1 avatar Mar 28 '21 18:03 Emersont1

Okay, What seems to be the issue is that if a tile doesn't have a custom property, it will not not loaded into the tiles List for each tileset. This means I'll need to manually add the property to all other tiles, instead of defaulting to zero in code

Emersont1 avatar Apr 01 '21 07:04 Emersont1

if a tile doesn't have a custom property, it will not not loaded into the tiles List for each tileset

I noticed this issue as well. I'm trying to create my own tilemap rendering system, and I have to iterate over every tile and get the appropriate tile information from the tileset. But since the tileset data only includes tiles with custom properties, I have to add a dummy property to every single tile, which seems a bit silly to me.

Does anyone know if there's any particular reason why the only tiles included in a tileset are the ones with custom properties?

PixelDough avatar Dec 05 '21 00:12 PixelDough

This is related to how Tiled handles custom properties. Tiles are only exported to the .TSX file if they have explicitly been given custom properties.

If you create your tile set as a collection of images instead of a single source image all the tiles will be in the exported .TSX file.

Take a look here https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tileset.

A tileset can be either based on a single image, which is cut into tiles based on the given parameters, or a collection of images, in which case each tile defines its own image. In the first case there is a single child element. In the latter case, each child element contains an element.

Pizt0lmnk avatar Feb 03 '22 06:02 Pizt0lmnk