# Population

### Key Features

* **Manages Player Population:** Tracks and updates unit population and limits it with population capacity. This is done through Player events to ensure any changes to its units or other producible (like research) will be applied to the population module as well.
* **Population Consumption & Capacity:** Uses attributes on units to determine how much population they consume or provide. Unlike most other modules that use capabilities.
* **Dynamic Population Limits:** Supports increasing max population via buildings, upgrades (research), or faction settings.
* **Hard Cap for Population:** Ensures population cannot exceed a predefined limit regardless of capacity provided by units in game.
* **Event-Driven Updates:** Triggers population change events for UI or gameplay reactions.
* **Seamless Integration:** Works alongside production module to enforce unit limits.

***

### Structure

* **Population Module** – Core module handling population tracking, limits, and consumption.
* **PopulationConsumptionAttribute** – Determines how much population a unit consumes.
* **PopulationCapacityAttribute** – Determines how much population a unit provides.
* **OnPopulationUpdate** – Unity event that triggers whenever population changes.

***

### Usage Examples

#### Checking Current Population

```csharp
PopulationModule module = player.GetModule<PopulationModule>();
Debug.Log($"Current Population: {module.CurrentPopulation}/{module.MaxPopulation}");
```

#### Adding a Unit (Population Consumption)

This is manually managed by the module when listening to player's entity, unit and producible events. However, you can still manually modify population.

<pre class="language-csharp"><code class="lang-csharp"><strong>if (module.HasPopulationCapacity(5)
</strong>{
    module.AddPopulation(5);
}
else
{
    Debug.Log("Not enough population capacity!");
}
</code></pre>

#### Increasing Max Population (e.g., Building Construction)

```csharp
popModule.IncreaseMaxPopulation(10);
Debug.Log($"New Max Population: {popModule.MaxPopulation}");
```

#### Applying a Hard Cap

```csharp
popModule.SetPopulationHardCap(200);
Debug.Log("Population is now capped at 200.");
```


---

# 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://travljen.gitbook.io/unit-system/how-it-works/modules/population.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.
