Game.Simulation.BirthSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
BirthSystem is a simulation system responsible for evaluating and spawning citizen births each simulation update. It schedules a parallel IJobChunk (CheckBirthJob) that iterates adult citizens, checks household composition and housing, computes birth probability using CitizenParametersData (including student adjustments and adult-female bonus), and spawns baby citizen entities using available citizen prefabs/archetypes. It uses an EndFrameBarrier's EntityCommandBuffer to create entities safely, reports birth statistics to CityStatisticsSystem, and keeps lightweight debug counters (NativeCounter / NativeValue) for births. A small SumBirthJob copies the native counter into a debug-accessible value. The class wires required queries for citizen prefabs and citizen parameter singletons and requires those queries for updating.
Fields
-
public static readonly int kUpdatesPerDay
This constant (value 16) defines how many update ticks are considered a "day" for birth probability normalization. -
private EndFrameBarrier m_EndFrameBarrier
Reference to EndFrameBarrier system used to create an EntityCommandBuffer for safe entity creation at end of frame. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem; used to compute update frame index via SimulationUtils. -
private CityStatisticsSystem m_CityStatisticsSystem
Reference to CityStatisticsSystem used to enqueue birth statistics events. -
private TriggerSystem m_TriggerSystem
Reference to TriggerSystem; the system adds an action buffer writer dependency for jobs. -
[DebugWatchValue] private NativeValue<int> m_DebugBirth
NativeValue exposing the counted number of births (copied by SumBirthJob) for debugging/watch purposes. -
private NativeCounter m_DebugBirthCounter
NativeCounter used by CheckBirthJob to increment when a spawn occurs; aggregated by SumBirthJob. -
private EntityQuery m_CitizenQuery
EntityQuery selecting citizens to evaluate for potential births (reads Citizen, HouseholdMember, UpdateFrame, CurrentBuilding; excludes Deleted/Temp). -
private EntityQuery m_CitizenPrefabQuery
Query used to collect citizen prefabs and archetype data (prefabs to instantiate when spawning babies). -
private EntityQuery m_CitizenParametersQuery
Query used to read the singleton CitizenParametersData that contains base birth rate, student adjustment, adult female bonus, etc. -
public int m_BirthChance
Public integer field present in the class (default 20). Note: the shown implementation does not reference this field for the birth calculations — births are driven by CitizenParametersData and random rolls in CheckBirthJob. -
private TypeHandle __TypeHandle
Internal structure caching various type/lookup handles used by jobs and scheduling. -
(Nested types)
CheckBirthJob
,SumBirthJob
,TypeHandle
Internal job structs. CheckBirthJob is a Burst-compiled IJobChunk that contains the main logic for evaluating and spawning births. SumBirthJob aggregates debug counter into m_DebugBirth.
Properties
- None (no public properties are exposed by this system).
Constructors
public BirthSystem()
Default constructor. Initialization of managed references and native containers happens in OnCreate; the constructor is preserved for the runtime.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval used by the system scheduler. The implementation computes an interval based on kUpdatesPerDay and a factor (262144 / (kUpdatesPerDay * 16)) to throttle how frequently the system runs relative to simulation phases. -
[Preserve] protected override void OnCreate()
Initializes native counters (m_DebugBirthCounter, m_DebugBirth), acquires references to dependent systems (EndFrameBarrier, SimulationSystem, CityStatisticsSystem, TriggerSystem), builds EntityQueries (citizens, citizen prefabs, citizen parameters), and marks queries required for update to ensure they exist before scheduling jobs. -
[Preserve] protected override void OnDestroy()
Disposes native containers (m_DebugBirthCounter, m_DebugBirth). -
[Preserve] protected override void OnUpdate()
Main scheduling method. Computes the current update frame index for birth checks, constructs and schedules CheckBirthJob (collecting prefab/archetype lists asynchronously), wires dependencies with CityStatisticsSystem and TriggerSystem, adds the job handle to EndFrameBarrier as a producer, and schedules SumBirthJob to copy the birth count to the debug NativeValue.
CheckBirthJob details: - Filters chunks by UpdateFrame shared component so each citizen is only processed in its assigned update frame. - Skips non-adults, tourists, commuters, and males (depending on citizen flags). - Ensures the household has a property (renter/property owner) as birthplace. - Examines household members to add adult-female bonus to birth rate if an adult male is present. - Applies student adjustment if the prospective mother is a student. - Uses RandomSeed to determine whether to spawn a baby using base birth rate normalized by kUpdatesPerDay. - Spawns a baby by creating an entity with a random citizen prefab/archetype, setting PrefabRef, HouseholdMember, Citizen, and CurrentBuilding components. - Increments m_DebugBirthCounter and enqueues a StatisticsEvent for births.
SumBirthJob details: - Copies the count from m_DebugBirthCounter to m_DebugBirth.value for debug inspection.
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler-generated helper used during OnCreateForCompiler to ensure queries are assigned; no runtime logic beyond setup. -
protected override void OnCreateForCompiler()
Compiler helper that calls __AssignQueries and assigns type handles via __TypeHandle.__AssignHandles. Used to support generated code for ECS type handles.
Usage Example
// Example: Adjust the birth chance (field present on the system). Note: the system's
// current code does not read m_BirthChance for calculations; use this pattern
// to access the system and change public fields or to extend behavior in your mod.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
var birthSystem = base.World.GetOrCreateSystemManaged<Game.Simulation.BirthSystem>();
if (birthSystem != null)
{
// This field exists on the system, but the built-in CheckBirthJob uses
// CitizenParametersData for probabilities. Changing this value alone
// will not affect births unless you modify the system's job logic.
birthSystem.m_BirthChance = 50;
}
}
Notes and modding tips: - To change actual birth probabilities at runtime, modify the CitizenParametersData singleton (read/written via the appropriate queries) or replace/patch the CheckBirthJob logic. The system reads CitizenParametersData.m_BaseBirthRate, m_AdultFemaleBirthRateBonus, and m_StudentBirthRateAdjust. - New entities are created via EndFrameBarrier.CreateCommandBuffer().AsParallelWriter(); if you alter spawn behavior ensure you use an ECB (EntityCommandBuffer) and preserve job dependencies by adding produced job handles to the barrier. - The system relies on citizen prefab lists (m_CitizenPrefabs and m_CitizenPrefabArchetypes) gathered from m_CitizenPrefabQuery; ensure your custom citizen prefabs are available if you intend to spawn custom citizens.