Skip to content

Game.Citizens.CitizenInitializeSystem

Assembly: Game
Namespace: Game.Citizens

Type: class

Base: GameSystemBase

Summary:
Initializes newly created citizen entities. This system runs a burst-compiled IJobChunk (InitializeCitizenJob) over entities that have been just Created and are not Temp, populating Citizen-related data (pseudo-random seed, health, well-being, gender flag, leisure counters, age / birthday, education level), assigning a concrete citizen prefab, adding the citizen to its household buffer, enabling/disabling various components (Arrived, MailSender, CrimeVictim, CarKeeper, HasJobSeeker), and enqueueing trigger actions for birth events. It reads global/time/demand singletons and writes via a ModificationBarrier and a TriggerSystem action buffer.


Fields

  • private Unity.Entities.EntityQuery m_NewCitizenQuery
    Used to select newly created citizen entities to initialize (requires Citizen, HouseholdMember, Created and excludes Temp). The job is scheduled against this query.

  • private Unity.Entities.EntityQuery m_TimeSettingQuery
    Query for retrieving the TimeSettingsData singleton (days per year, etc.) used when generating birthdays/ages.

  • private Unity.Entities.EntityQuery m_CitizenPrefabQuery
    Query used to collect available CitizenData prefabs; converted to a NativeList and passed into the job so the job can select an appropriate citizen prefab.

  • private Unity.Entities.EntityQuery m_TimeDataQuery
    Query for the TimeData singleton (used to compute current day for birthdays).

  • private Unity.Entities.EntityQuery m_DemandParameterQuery
    Query for the DemandParameterData singleton (used for education level and teen/adult spawn chances).

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem to read the current simulation frame (frameIndex) for birthday calculations.

  • private TriggerSystem m_TriggerSystem
    Reference to the TriggerSystem used to obtain an action buffer to enqueue birth-related triggers (e.g., CitizenCoupleMadeBaby).

  • private ModificationBarrier5 m_EndFrameBarrier
    Barrier (command buffer provider) used to add component changes (for example PrefabRef) safely from a job.

  • private TypeHandle __TypeHandle
    Internal struct holding Entity/Component handles lookups used by the job. Assigned in OnCreateForCompiler.

  • private const / compiler-generated and nested fields inside InitializeCitizenJob and TypeHandle are present (e.g., RandomSeed usage, parallel writers, component lookups), but are internal implementation details of the job.

Properties

  • None publicly exposed by this system.

Constructors

  • public CitizenInitializeSystem()
    Default constructor (preserved). System gets created/registered by the ECS world. Initialization of queries and system references happens in OnCreate.

Methods

  • protected override void OnCreate() : System.Void
    Creates required EntityQuery objects (m_NewCitizenQuery, m_CitizenPrefabQuery, m_TimeSettingQuery, m_TimeDataQuery, m_DemandParameterQuery). Gets references to SimulationSystem, TriggerSystem and the ModificationBarrier5 from the world. Calls RequireForUpdate for the queries that must exist for the system to run. Also prepares the system to be used by the ECS.

  • protected override void OnUpdate() : System.Void
    Builds and schedules InitializeCitizenJob. Steps performed:

  • Collects citizen prefab entities (m_CitizenPrefabQuery.ToEntityListAsync).
  • Prepares component/lookup handles and singleton data (DemandParameterData, TimeSettingsData, TimeData).
  • Uses RandomSeed.Next() and simulation frame info.
  • Creates an EntityCommandBuffer.ParallelWriter via the end-frame barrier and a parallel TriggerAction writer from TriggerSystem.
  • Schedules a JobChunk with combined dependencies and registers the job handle with the end-frame barrier.

  • protected override void OnCreateForCompiler() : System.Void
    Compiler-time helper calling __AssignQueries and assigning type handles. (Part of generated/setup code in this class.)

  • private void __AssignQueries(ref SystemState state) : System.Void
    Compiler/JIT helper present in the generated file (currently creating/disposing an EntityQueryBuilder). Used during compiler-focused initialization.

  • Nested: InitializeCitizenJob (private struct, [BurstCompile], IJobChunk)
    Job that performs the per-chunk initialization work. Key behavior inside Execute:

  • Disables several components on the citizen (Arrived, MailSender, CrimeVictim, CarKeeper, HasJobSeeker) so the citizen starts in a default non-arrived / non-seeking state.
  • Fills citizen fields: m_PseudoRandom (ushort), m_Health (byte 40..59), m_WellBeing (byte 40..59), m_LeisureCounter (different ranges for tourists vs non-tourists), random male flag.
  • Chooses a concrete citizen prefab via CitizenUtils.GetCitizenPrefabFromCitizen using citizen data and a random generator, and writes a PrefabRef component via the command buffer.
  • Adds the citizen to the HouseholdCitizen dynamic buffer of the household entity.
  • Computes age/birthday from citizen.m_BirthDay setting:
    • 0 = newborn: set age to Child and enqueue birth triggers (single or couple).
    • 1 = adult spawn: choose adult age in a range.
    • 2 = teen or child spawn depending on DemandParameterData.m_TeenSpawnPercentage.
    • 3 = elderly spawn.
    • otherwise = random adult age within a year offset.
  • Determines education probability ranges based on DemandParameterData.m_NewCitizenEducationParameters and assigns education level.
  • Sets citizen.m_BirthDay to current day minus computed age-in-days.
  • Writes back modified Citizen component data to the component lookup.

  • Other small private/generated methods for job interface compliance and handle assignment are present.

Usage Example

// Typical mod usage: create a citizen entity so CitizenInitializeSystem will pick it up on the next frame.
// Make sure the world has the data singletons (TimeData, TimeSettingsData, DemandParameterData) and a household entity.

var em = World.DefaultGameObjectInjectionWorld.EntityManager;

// create or obtain a household entity first
Entity household = ...; // existing household entity with a HouseholdCitizen dynamic buffer

// create a new citizen entity that the system will initialize
Entity citizen = em.CreateEntity(
    typeof(Game.Citizens.Citizen),
    typeof(Game.Citizens.HouseholdMember),
    typeof(Game.Common.Created) // system queries for Created to process new citizens
);

// set household reference so the initializer can add the citizen to it
em.SetComponentData(citizen, new Game.Citizens.HouseholdMember { m_Household = household });

// After creation, on the next system update CitizenInitializeSystem will run InitializeCitizenJob
// and populate the citizen data, assign a prefab, add it to the household buffer, and possibly enqueue birth triggers.

{{ Additional notes: - This system is performance-sensitive: initialization is burst-compiled and executed as a chunked job. Modifications should preserve its data-parallel design. - If adding or changing components that this system expects (Citizen, HouseholdMember, Created, Temp), update the EntityQuery accordingly. - To intercept or extend behavior (e.g., custom birth triggers or custom prefab selection), either replace or run before/after this system using system ordering, or create a complementary system that listens to TriggerSystem actions or inspects citizens after initialization. }}