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:
- In Glue, right-click on the Entities folder
- Select Add Entity
- Name the entity Monster
- Check the AxisAlignedRectangle check box
- Click OK
We need to modify the rectangle so it stands out relative to the collision rectangles:
- Select the newly-created rectangle (named AxisAlignedRectangleInstance)
- Change Width to 12
- Change Height to 12
- 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 (such as 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. Note that we add the Monster list to the GameScreen, not to any of the levels since we want the monster list to be available in all levels:
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:
- Select the Monster entity
- Click the “Properties” tab
- 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
We will be using the Type property to set the entity type. For a deeper dive on how this property works, see the Type documentation here: http://flatredball.com/documentation/tools/tiled-plugin/using-tiled-object-types/
The documentation linked above shows how to import an XML file created by Glue so that variables defined on Monster (or any other entity) automatically appear in Tiled. For simplicity we’ll skip this step, but you may want to perform that additional step for larger projects.
To set the Type property:
- Open whichever level is currently being loaded in your game in Tiled (such as Level2.tmx)
- If the tileset file (.tsx) file is not available to edit, click the Edit button
- Select a tile on your map to represent the monsters. I’ll use the blue slime
- Enter the entity name Monster as the Type for this tile. Note that the name needs to match the entity name exactly, including capitalization
Placing Monster Tiles
Any tiles placed with the Type 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:
- Open Level2Tmx.tmx (since this is currently the level that starts when we run our game)
- Click the Add Layer button
- Select Tile Layer
- 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:
- Open Level2.cs in Visual Studio
- Modify CustomInitialize to call
TileEntityInstantiator.CreateEntitiesFrom as shown in the following code:
If we run the game now, we’ll see that all slime tiles have been replaced by Monster instances:
Note that CreateEntitiesFrom needs to be called in every level or tiles won’t be replaced with entities.
If the slimes are still showing up instead of the yellow rectangles you can check the following:
- Make sure that Level2Tmx.tmx and dungeonTileSet.tsx files have both been saved
- Make sure you’re calling CreateEntitiesFrom in the screen that’s being loaded
- Verify that you entered the right name (including capitalization) in the Type property.