The TileEntityInstantiator class simplifies the creation of entities through Tiled. It is the most common way to add entities to your game.

Defining an Entity for Instantiation

Before adding an entity to your screen, you must first define an entity. Fortunately any entity can be created through Tiled, so we won’t go through the steps to create a new entity – only the steps required to modify an entity for Tile instantiation.

We’ll begin with an entity called Enemy:

First we’ll tell Glue to create a factory for our entity:

  1. Select the entity
  2. Click the Properties tab
  3. Set CreatedByOtherEntities to True

Next we’ll need a list of this entity type in our GameScreen. This is where the Entity instances will live once they are created:

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

Adding an Enemy to Tiled

First we’ll need to mark a particular tile in our tileset as being an enemy tile:

  1. Open Tiled
  2. Click the wrench icon to edit the tileset
  3. Select a tile which will be the enemy
  4. Enter the name of your entity (Enemy) as the Type

  5. Save your TileSet

Now you can add the enemy to your map by adding the tile to any layer:

Don’t forget to save your TMX file.

Adding Code to Instantiate Entities

Finally we need to add code to instantiate our Entities. To do this:

  1. Open Visual Studio
  2. Open GameScreen.cs (or the custom code file for your screen)
  3. Find CustomInitialize
  4. Modify the code so it calls CreateEntitiesFrom
    void CustomInitialize()

Entites will be created from the map. Tiles which represent Entities will be removed and replaced with instances of your entities.

Instantiating Collidable Entities with Tiled Shapes

Entities which implement the ICollidable interface can also be instantiated through Tiled. Any ICollidable entity can be used, but usually entities which will have their collision set by Tiled should not have any collision objects in Glue.

To create an empty ICollidable entity:

  1. Right-click on the Entites folder in Glue
  2. Select Add Entity
  3. Make sure no shapes are checked in the add window
  4. Check the ICollidable checkbox
  5. Leave the defaults for Tiled instantiation
  6. Enter a name
  7. Click OK

To create an instance of this shape in Tiled:

  1. Open a TMX file in Tiled
  2. Create or select an Object Layer

  3. Select to insert a rectangle

  4. Add a rectangle to your level

  5. Set the type of the rectangle to CollidableEntity (or the name of whatever entity you wish to instantiate)

  6. Save the map

CreateEntitiesFrom will now automatically create an instance of the CollidableEntity, and it will use a rectangle as its collision matching the size defined in Tiled.