Factory

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:

  1. Entities created in a factory will automatically be added to screen lists (by default).
  2. Factories provide a standard way to create entities which is especially useful for systems like Tiled to instantiate new entities.
  3. Factories can pool Entities which can greatly reduce allocated memory for Entities which are created and destroyed frequently.

Quick Usage Example

To use a factory in Glue:

  1. Create or select an existing entity
  2. Select the Quick Action tab

  3. Click the Add Player Factory button. Your button may have a different text matching your selected Entity’s name.

  4. 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.

  5. 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.