Introduction
The term “Factories” comes from the factory design pattern. Factories are objects which can create new instances of certain types of Entities. Factories provide the following benefits:
- Entities created in a factory will automatically be added to screen lists (by default).
- Factories provide a standard way to create entities which is especially useful for systems like Tiled to instantiate new entities.
- Factories can pool Entities which can greatly reduce allocated memory for Entities which are created and destroyed frequently.
If your game requires the creation of entities in code (such as the player shooting bullets or enemies appearing at an enemy spawn point), then you will want to use factories.
Quick Usage Example
To use a factory in Glue:
- Create or select an existing entity
- Select the Quick Action tab
- Click the Add Player Factory button. Your button may have a different text matching your selected Entity’s name.
- If you do not see this, your entity may already have a factory. To check this, look at the properties tab for your entity and find the Created by Other Entities property. If it’s true, you already have a factory.
- If you do not see this, your entity may already have a factory. To check this, look at the properties tab for your entity and find the Created by Other Entities property. If it’s true, you already have a factory.
- Click Add Player List to GameScreen. Note that if your main screen is not titled GameScreen, then you will need to manually add a list to the desired screen. A screen must exist in your current screen or the created entities will not have their CustomActivity methods called.
- In any code, such as in GameScreen or any other entity, create a Player instance using the CreateNew method as shown in the following code:
void CustomInitialize() { var player = Factories.PlayerFactory.CreateNew(x: 50, y: 100); // perform any additional modifications to the player here }
Tutorial
For a tutorial on how to use factories, see the Created by Other Entities tutorial page.
Factories Generated by Glue
Glue will automatically generate factories for any entity which has its Created by Other Entities property set to true.
For information on this property, see the CreatedByOtherEntities page.