TileEntityInstantiator
Introduction
FlatRedBall supports the creation of Entity instances by adding either tiles or objects in a Tiled Map (.tmx). This document shows how to do so.
Defining an Entity for Instantiation
Before adding an entity to your screen, you must first define an entity. Once an entity is defined, it can be created through Tiled. Any entity can be instantiated in a Tiled map - even entities which are larger than the size of the tiles in your game (usually 16x16). By default FlatRedBall sets the options necessary to instantiate entities through a Tiled Map in the new entity dialog as shown in the following image:
If these options have not been set and you already have an entity created, you can adjust the values manually. To create a factory, set the CreatedByOtherEntities to True.
Also, your entity type should have a corresponding list, typically in GameScreen. If your entity inherits from another entity, then a list of the base type in the GameScreen is adequate.
For more information on how to create lists, see the List page.
Adding an Enemy to Tiled using a Tile Layer
The easiest way to add an instance of an entity to a game is by dropping a Tile into your level map. First, we will mark which tile to use for the entity type.
Open your map in Tiled
Select the Tileset which has your enemy. Keep in mind that each layer in Tiled can only use one tileset.
Click the wrench icon to edit the tileset
Select which tile you would like to use for your entity type
Enter the name of your entity as the Type. Capitalization matters, so be sure to match the name as it is written in the FlatRedBall Editor
Save your TileSet
Now you can paint the tiles onto your map and they will automatically be converted into entities when you run your game.
Manually Instantiating Entities in Code
By default maps create entities without any manual code. This setting is controlled by the Create Entities From Tiles variable, which is checked by default.
This setting produces the following code:
Note that this setting is only applied in the base GameScreen, so to turn it off it must be turned off on the Map object in GameScreen (or the base Screen). If it is turned off, you can manually add the code to create entities in your CustomInitialize as shown in the following snippet.
Using either approach will result in the same thing - entities being created from tiles.
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:
Right-click on the Entites folder in Glue
Select Add Entity
Make sure no shapes are checked in the add window
Check the ICollidable checkbox
Leave the defaults for Tiled instantiation
Enter a name
Click OK
To create an instance of this shape in Tiled:
Open a TMX file in Tiled
Create or select an Object Layer
Select to insert a rectangle
Add a rectangle to your level
Set the type of the rectangle to CollidableEntity (or the name of whatever entity you wish to instantiate)
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.
Last updated