Game.City.Population
Assembly: Assembly-CSharp
Namespace: Game.City
Type: public struct Population : IComponentData, IQueryTypeParameter, IDefaultSerializable, ISerializable
Base: System.ValueType
Summary:
Represents aggregate population data for a city region (used as an ECS component). Stores current population, population including move-ins, and average citizen metrics (happiness and health). Implements serialization interfaces used by Colossal's save/load pipeline; it handles backward compatibility for the average health field when reading older save versions by defaulting to 50.
Fields
-
public int m_Population
Total current population count. -
public int m_PopulationWithMoveIn
Population value that includes recently moved-in citizens (used for tracking move-in events / transient counts). -
public int m_AverageHappiness
Average happiness value for the population (0–100 scale is implied by defaults). -
public int m_AverageHealth
Average health value for the population. When deserializing older data that lacks this field, it defaults to 50.
Properties
- This type does not declare any managed properties (only public fields). All state is exposed via fields.
Constructors
public Population()
Default value-type constructor. Note: meaningful defaults are applied by calling SetDefaults(Context) (or by initializing fields manually).
Methods
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads fields from the provided reader in the same order they are written:- Reads m_Population, m_PopulationWithMoveIn, m_AverageHappiness.
-
If reader.context.version >= Version.averageHealth, reads m_AverageHealth; otherwise sets m_AverageHealth = 50 for backward compatibility. This ensures older save files that predate the averageHealth field still load with a sensible default.
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes fields to the provided writer in order: m_Population, m_PopulationWithMoveIn, m_AverageHappiness, m_AverageHealth. This order must match Deserialize for save/load compatibility. -
public void SetDefaults(Context context)
Initializes fields to default starting values: - m_Population = 0
- m_PopulationWithMoveIn = 0
- m_AverageHappiness = 50
- m_AverageHealth = 50
Call this when creating a new Population component to ensure expected initial values (the Context parameter may contain version or environment info used by other components).
Usage Example
// Add the Population component to an entity with default or custom values.
using Unity.Entities;
[Preserve]
public void CreatePopulationComponent()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var e = entityManager.CreateEntity(typeof(Game.City.Population));
// Option A: Use SetDefaults (requires a Context instance)
var pop = new Game.City.Population();
// Assuming you have a Context 'ctx' available from the serialization/system setup:
// pop.SetDefaults(ctx);
// Option B: Manually initialize fields
pop.m_Population = 1000;
pop.m_PopulationWithMoveIn = 25;
pop.m_AverageHappiness = 60;
pop.m_AverageHealth = 70;
entityManager.SetComponentData(e, pop);
}
Additional notes for modders: - When implementing custom serialization or interacting with saves, respect the read/write order used here. The Deserialize method guards for older save versions lacking m_AverageHealth by defaulting it to 50. - Because fields are public, you can read/write them directly from systems, but prefer using SetDefaults when creating a component for consistency. - This struct implements IComponentData and is intended for use in Unity's ECS; take care with threading and job safety when accessing/modifying it in jobs.