06. Creating Entities from Tiles

Introduction

So far we’ve looked at how to create tile maps and have them display in game. We’ve also used these tile maps to create collision. This guide shows how to instantiate entities by placing tiles in a tile map (no code required).

Creating an Entity

Any type of entity can be created through tile maps, so this feature provides a lot of flexibility. For this example we’ll create a simple entity which displays a yellow rectangle:

  1. In Glue, right-click on the Entities folder
  2. Select Add Entity
  3. Name the entity Monster
  4. Check the AxisAlignedRectangle check box
  5. Click OK

We need to modify the rectangle so it stands out relative to the collision rectangles:

  1. Select the newly-created rectangle (named AxisAlignedRectangleInstance)
  2. Change Width to 12
  3. Change Height to 12
  4. Change Color to Yellow

Setting Up for Tile Map Entity Creation

Typically entities which are created by tile maps (such as monsters, coins, or traps) need to be stored in a list for custom logic. If our game were developed into a full game, the Monsters may require custom logic for AI and collision. To support this, we will create a list of Monsters in our GameScreen.

To create a list of Monsters in our GameScreen, right-click drag+drop the Monster entity onto the GameScreen:

DragDropList2

When the GameScreen is active, the MonsterList that we just created should have reference to all monsters. We’ll set the Monster’s CreatedByOtherEntities property to true so the generated code creates a factory object for creating new monsters. In other words, setting this to true allows the tile map to add new Monster instances to the GameScreen’s MonsterList.

For more information, see the CreatedByOtherEntities page.

To set this property:

  1. Select the Monster entity
  2. Click the “Properties” tab
  3. Set the CreatedByOtherEntities value to True

Our Monster entity is ready to be used. Now we just need to update our tile maps.

Defining Monster Tiles

Note: entities can also be created through Tiled using Type and a generated XML file. Additional information about this feature can be found here: http://flatredball.com/documentation/tools/tiled-plugin/using-tiled-object-types/

To create Monster instances through our tile maps, we first need to assign properties on the tile set, much like we assigned properties when creating our Wall tiles. We will use the property EntityToCreate which has special functionality in the Tiled Plugin. The Tiled Plugin provides a method CreateEntitiesFrom which searches the tilemap for tiles with the EntityToCreate property. If found, this function will replace the tiles with entity instances.

To add the EntityToCreate property:

  1. Open whichever level is currently being loaded in your game in Tiled
  2. Select a tile on your map to represent the monsters. I’ll use the blue slime

  3. Click the + button to add a new property to the selected tile
  4. Enter the name EntityToCreate and click OK. This is a standard property which the tile map plugin uses to create entities.

  5. Enter a value of “Monster” for the EntityToCreate property. This defines the entity type that will be created wherever this tile is placed.

Placing Monster Tiles

Any tiles placed with the EntityToCreate of Monster will create Monster instances at runtime. Furthermore, these tiles will be removed from the tilemap altogether when the entity is created.

To avoid creating “holes” in the tile map, most games create a new layer specifically for entity tiles. To do this:

  1. Click the Add Layer button

  2. Select “Add Tile Layer”
  3. Enter the name EntityLayer

With the EntityLayer selected, we can paint some monster tiles on our map.

As always, don’t forget to save your changes.

If we run our game, we will see the blue slime tiles:

Of course, this isn’t exactly what we want – we want the slime tiles to be replaced by instances of our Monster entity. In other words, we should see yellow rectangles, not blue slimes.

The reason that these entities haven’t been replaced is we haven’t told our game to replace all entity tiles with the appropriate entities. To do this:

  1. Open GameScreen.cs  in Visual Studio
  2. Call TileEntityInstantiator.CreateEntitiesFrom  after assigning the currentLevel  variable:

If we run the game now, we’ll see that all slime tiles have been replaced by Monster instances:

It’s worth noting that the MonsterList is also populated, so we could write code in GameScreen accessing the monster instances. We can check this in the Visual Studio debugger: