craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.41k stars 320 forks source link

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

Open Emersont1 opened 3 years ago

Emersont1 commented 3 years ago

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 commented 3 years ago

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

PixelDough commented 2 years ago

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?

Pizt0lmnk commented 2 years ago

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.