08. Accessing Tiled Properties


This walkthrough will show how to directly access tile and object-layer tile properties.

So far we have covered how to create collision from properties and how to use properties on objects to assign variables on Glue entities. All of these approaches rely on built-in methods to create collision and entities. This tutorial will perform a deeper dive on how to access tile properties for ultimate flexibility.

Note that material covered in this tutorial gives the developer the most flexibility, it is not necessary to create a functional game.

Creating a New Level

We’ll create a new level so our other two levels can keep working like they always have. To create a new level:

  1. Right-click on GameScreen in Glue
  2. Select Create Derived (Level) Screen
  3. Name the new level TilePropertiesScreen and click OK

This level will also need a TMX file, so copy the Level2Tmx.tmx file into this screen’s content folder (like was done in a previous tutorial when we created Level2) and name the file TilePropertiesTmx.tmx.

Once it’s copied, drag+drop it into the TilePropertiesScreen.

Finally, right-click on the TilePropertiesScreen and select Set as StartUp Screen so it is the first screen in the game.

If we run our game now we’ll see the same level as Level2Tmx.tmx, but it won’t have any collision or entities – the original tiles will be displayed instead:

Accessing Tiles by Name

Each layer in the the tile map can contain its own list of tiles. For performance reasons each tile is not an individual object. In other words, we can’t simply loop through a list of tile objects to check for certain properties.

Instead, we have to ask each layer for a list of indexes for a given name. Once we’ve obtained these indexes, we can get the tile positions and perform custom logic. For example, we can create green circles wherever we have a tile with the name "Wall" .

First we’ll need to modify our tileset to set one of our tiles to have the “Wall” name:

  1. Open TilePropertiesTmx.tmx
  2. Open the dungeonTileSet.tsx file
  3. Select a tile which will represent a wall tile
  4. Click the + button to add a new custom property
  5. Enter the name Name
  6. Verify the type is string
  7. Click OK

  8. Set the value for the Name property to Wall

Note that FlatRedBall treats the Name property as the unique identifier for the tile. Therefore, the same name cannot be used for two different tiles – doing so will crash your program.

If the named tile is one that is already used in the TilePropertiesTmx.tmx, then we can modify the TilePropertiesScreen.cs as shown in the following code:

If we run the game we will see green circles drawn on top of the wall tiles:

Warning: The purpose of the code above and much of the code in this guide is to show how to interact with tile properties at a low level. A realistic example would require additional code which would significantly increase the length of this tutorial. Keep in mind that the code above suffers from some serious problems. The created circles are not stored in a list so they cannot be used for any purpose aside from displaying on screen. Furthermore, since the circles are not stored in any list, they cannot be cleaned up. Exiting the screen or switching levels at this point would result in an exception.

Reading Custom Properties from Tiles

The Name property is built-in to the Tiled Plugin, but the plugin also supports reading custom properties. For example, we can modify the Wall tile to include a property for coloring the circle:

This property can be read through the level’s Properties . We’ll modify the code to define the color value and default it to Green . It will be set to Yellow  if the CircleColor  property is Yellow :

We obtain the circle color outside of any loops because the properties set on tiles in a tile set apply to the entire map, so the value will be the same regardless of layer or individual tile. For instance-specific properties we need to use objects on an Object Layer, as we’ll show later in this guide.

Now our game displays yellow circles:

Removing Tiles

Tiles can be removed dynamically removed using the RemoveQuads  method. For example, we can remove all of the wall tiles used to create circles:

Accessing Object Layer Properties

Tiles on “Object Layers” can contain instance-specific properties, as were used to create the Door instance in an earlier guide. We can also access these properties similar to how we access properties on tiles.

We’ll write code to look for any tiles that have their “Type” property set to “Door”, then manually create a door for every tile we find.

To do this, add the following function to TilePropertiesScreen.cs:

Like before, we need to call this method in CustomInitialize :

Now our door tile will be removed and replaced by a Door  instance. Since our code uses the DoorFactory  object, our Door  instance is automatically added to the DoorList in GameScreen :