Factories
Last updated
Last updated
The term "Factories" comes from the factory design pattern. Factories are objects for creating new instances of certain types of Entities. Factories provide the following benefits:
Entities created in a factory are automatically 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. For more information see the PooledByFactory property.
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 factories are usually the answer.
When adding a new entity, the Create Factory checkbox is automatically checked. In most cases this should be left as true since most entities should be created by factories so they can be properly added to their corresponding lists (usually in GameScreen).
If you have unchecked this option, you can add factories after an entity is created. For more information, see the CreatedByOtherEntities property page.
To use a factory in code, use the CreateNew method when you would like to create a new entity. For example, you can create an Enemy instance in the CustomInitialize of your screen as shown in the following code:
Glue automatically generates factories for any entity which has its Created by Other Entities property set to true. For information on this property, see the CreatedByOtherEntities page.