07. Creating Entities from Object Layers


In the previous tutorial we covered how to create entities from tiles. This approach can simplify entity creation, and works great for entities which are uniform (each instance is identical aside from position).

Some entities require custom properties per-instance. For example, consider a game where players can walk through doors to enter other maps. In this case, each door entity needs to have a custom property for which level it links to.

This walk-through shows how to create object layers in Tiled and how to create entities using these objects.

Creating a New Entity

For this tutorial we will be creating a Door entity which can be used to move the player from one level to the next. This entity will need to have collision so that the player can collide with it and we can respond to the collision.

To create a Door entity:

  1. In Glue, right-click on Entities
  2. Select “Add Entity”
  3. Enter the name “Door”
  4. Right-click on the Objects foler under Door
  5. Select “Add Object”
  6. Verify that “FlatRedBall or Custom Type” is selected
  7. Select the Circle type
  8. Click OK

We also need to make the Door implement IColliable:

  1. Select the Door entity
  2. Click on the Properties tab
  3. Set “ImplementsICollidable” to True
  4. Set “CreatedByOtherEntities to True

We need to a variable to set which level to load when the Player collides with the Door:

  1. Right-click on the “Variables” folder under the Door entity
  2. Select “Add Variable”
  3. Select “Create a new variable”
  4. Select “string” as the Type
  5. Enter the Name “LevelToGoTo”

  6. Click OK

The name of the variable that we created (LevelToGoTo) needs to match the name of the property that we will be adding to the tile later in this guide.

Just like with the Monster entity, we need to create a list of Door entities in GameScreen:

  1. Right-click drag+drop the Door entity onto GameScreen
  2. Select “Add Entity List”

Creating an object layer

Tiled object layers allow placing geometric shapes (such as rectangles, circles, ad custom polygons) as well as images not bound to tiles. These images (also referred to as “tiles” in the Tiled program) can be used purely for non-interactive visuals or can be used to place entities.

To create an object layer:

  1. Open Level2 in Tiled
  2. Click the Add Layer button
  3. Select “Object Layer”

  4. Name the new layer EntityObjectLayer

To add an image tile:

  1. Verify that EntityObjectLayer is selected
  2. Select the tile to be placed in the tile set
  3. Click on the tile map to place the tile

Notice that the placed door may not align with the tiles. If you wish to make it align, you use the Select Objects command to select the tile and either move it visually or change its X an Y properties:

Now we can add the LevelToGoTo property to our placed tile:

  1. Verify that the door tile is selected
  2. Click the + button to add a new property
  3. Enter the name “EntityToCreate” and clck OK
  4. Enter the value “Door” for the EntityToCreate property
  5. Click the + button again
  6. Enter the name “LevelToGoTo”
  7. Enter the value “Level1” for the LevelToGoTo property

Don’t forget to save the tile map.

Verifying the Door Entity

If we run our game now, we should see the Door tile that we placed in Tile replaced by a white circle, which is the collision of the Door entity:

This entity was automatically created by calling CreateEntitiesFrom , which we added to GameScreen.cs  in the previous tutorial. As a reminder, the code is called in CustomInitialize :

Using the Door Entity

We can use the Door entity to navigate between levels. We’ll check collision between the Player  and all doors in the DoorList  object (our game only has one door, but it could be expanded to have more). If a collision occurs, we’ll set the GameScreen.LevelToLoad  property and reload the screen:

If the player touches the Door the GameScreen will restart itself on Level1 (or whichever level is referenced by the Door).