# Factories

### Introduction

The term "Factories" comes from the [factory design pattern](http://en.wikipedia.org/wiki/Factory_method_pattern). Factories are objects for creating new instances of certain types of Entities. Factories provide the following benefits:

1. Entities created in a factory are automatically 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. For more information see the [PooledByFactory](/flatredball/glue-reference/entities/glue-reference-entities-pooledbyfactory.md) 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.

### Factories Enabled by Default

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

<figure><img src="/files/BIoqef5xIGGpDAjXeb4U" alt=""><figcaption><p>Create Factory is checked by default</p></figcaption></figure>

If you have unchecked this option, you can add factories after an entity is created. For more information, see the [CreatedByOtherEntities property page](/flatredball/glue-reference/entities/glue-reference-createdbyotherentities.md).

### Quick Usage Example

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:

1. ```csharp
   void CustomInitialize()
   {
       var enemy = Factories.EnemyFactory.CreateNew(x: 50, y: 100);
       // perform any additional modifications to the player here
   }
   ```

### Factories Generated by Glue

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**](/flatredball/glue-reference/entities/glue-reference-createdbyotherentities.md) page.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/glue-reference/factory.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
