Game.LeaveHouseholdSystem
Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that handles citizens leaving their current household to form a new household. It schedules a Burst-compiled IJobChunk (LeaveHouseholdJob) that iterates over citizens marked with LeaveHouseholdTag and performs the logic to create a new household entity, transfer resources, update household membership buffers, and set flags/components for commuter households or property seeking. It uses CountResidentialPropertySystem to decide whether to enable property seeking and an EndFrameBarrier to record structural entity changes via a parallel command buffer. The job is scheduled with query results (household prefabs and outside-connection entities) produced asynchronously and combined into the system dependency.
Fields
-
public static readonly int kUpdatesPerDay
{{ YOUR_INFO }}
Number of update buckets per in-game day used by GetUpdateInterval. Value: 2. -
public static readonly int kNewHouseholdStartMoney
{{ YOUR_INFO }}
Initial money given to a newly created household. Value: 2000. -
private CountResidentialPropertySystem m_CountResidentialPropertySystem
{{ YOUR_INFO }}
Reference to the CountResidentialPropertySystem used to obtain ResidentialPropertyData, which the job uses to decide whether to enable PropertySeeker behavior for newly created households. -
private EndFrameBarrier m_EndFrameBarrier
{{ YOUR_INFO }}
Barrier used to get a parallel EntityCommandBuffer to safely create entities and modify components from the job; the barrier is also given the job handle so structural changes are applied after the job. -
private EntityQuery m_LeaveHouseholdQuery
{{ YOUR_INFO }}
Query used to find Citizen entities that have LeaveHouseholdTag and are not Deleted or Temp. This is the primary query the job runs over. -
private EntityQuery m_HouseholdPrefabQuery
{{ YOUR_INFO }}
Query that selects archetype prefabs for households (ArchetypeData + HouseholdData + DynamicHousehold). Results are collected to a NativeList used by the job to pick a prefab for new households. -
private EntityQuery m_OutsideConnectionQuery
{{ YOUR_INFO }}
Query for Outside Connection building entities (excludes electricity/water outside connections and building/Temp/Deleted) used to optionally spawn commuter households tied to outside connections. -
private EntityQuery m_DemandParameterQuery
{{ YOUR_INFO }}
Query used to fetch the DemandParameterData singleton, which contains parameters used when selecting an outside connection to spawn commuter households. -
private TypeHandle __TypeHandle
{{ YOUR_INFO }}
Generated struct bundling all Entity/Component type handles and lookups used by the job. Assigned during OnCreateForCompiler / __AssignHandles. -
(Nested)
private struct LeaveHouseholdJob : IJobChunk
{{ YOUR_INFO }}
Burst-compiled chunk job that contains the main logic. See Methods section for a summary of its behavior. -
(Nested)
private struct TypeHandle
{{ YOUR_INFO }}
Compiler-generated holder for all the type handles and buffer/component lookups used by the system and its job. Has an __AssignHandles method that requests the handles from the SystemState.
Properties
- This system exposes no public properties.
{{ YOUR_INFO }}
All required handles / data for scheduling are created inside OnUpdate and passed into the job. The system communicates structural changes through an EndFrameBarrier-provided command buffer.
Constructors
public LeaveHouseholdSystem()
{{ YOUR_INFO }}
Default constructor. Marked with Preserve attribute on lifecycle methods; system constructed by the World normally.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
{{ YOUR_INFO }}
Returns the update interval the system should be run at. Implementation returns 262144 / (kUpdatesPerDay * 16). This controls how frequently the system is scheduled relative to the simulation tick. -
[Preserve] protected override void OnCreate()
{{ YOUR_INFO }}
Initializes queries and acquires references to dependent systems (CountResidentialPropertySystem and EndFrameBarrier). Creates EntityQueries: - m_LeaveHouseholdQuery: Citizens with LeaveHouseholdTag (excludes Deleted/Temp) and required for update.
- m_OutsideConnectionQuery: outside connection building entities (filtered).
- m_HouseholdPrefabQuery: archetype prefabs for households.
-
m_DemandParameterQuery: for DemandParameterData singleton.
-
[Preserve] protected override void OnUpdate()
{{ YOUR_INFO }}
Constructs and schedules the LeaveHouseholdJob: - Collects EntityTypeHandle, ComponentTypeHandle and buffer/component lookups into a job instance.
- Retrieves ResidentialPropertyData from CountResidentialPropertySystem.
- Calls ToEntityListAsync on m_HouseholdPrefabQuery and m_OutsideConnectionQuery to produce NativeList
results and job handles. - Gets DemandParameterData singleton and RandomSeed.
- Creates a parallel EntityCommandBuffer via the EndFrameBarrier.
- Schedules the job with JobChunkExtensions.Schedule and combines dependencies with the async query jobs.
-
Adds the resulting dependency to the EndFrameBarrier.
-
protected override void OnCreateForCompiler()
{{ YOUR_INFO }}
Compiler-time helper that assigns queries and type handles. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
{{ YOUR_INFO }}
Helper used by generated code to initialize any query builders required by the compiler. Implementation in this compiled file only constructs/ disposes a temporary EntityQueryBuilder (no runtime queries are created here beyond OnCreate). -
(Nested Job)
LeaveHouseholdJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
{{ YOUR_INFO }}
Core per-chunk logic (Burst compiled): - Uses RandomSeed to create a Random instance.
- Iterates citizens in the chunk that match the m_EntityType and m_CitizenType arrays.
- For each citizen:
- Retrieves the household via HouseholdMember.
- Reads household Resources buffer and money amount.
- If household has MovingAway, simply removes LeaveHouseholdTag from the citizen (via command buffer).
- If the household lacks a HouseholdCitizen buffer, or household citizen count <= 0, or household money <= (kNewHouseholdStartMoney * 2), or citizen is not a Worker -> skip.
- Otherwise: pick a random household prefab from the provided m_HouseholdPrefabs list; get its ArchetypeData; create a new household entity via the prefab archetype.
- Attach PrefabRef to the new household and initialize its Resources buffer with kNewHouseholdStartMoney. Transfer the remaining money from the old household (EconomyUtils.AddResources logic).
- Remove the citizen from the old household's HouseholdCitizen buffer and add the citizen to the new household's HouseholdCitizen buffer. Update the citizen's HouseholdMember to point to the new household.
- Check residential property availability using m_ResidentialPropertyData: if free properties (sum) > 10, enable PropertySeeker on the new household. Otherwise, if there are outside connection entities and BuildingUtils.GetRandomOutsideConnectionByParameters finds a match using m_DemandParameterData.m_CommuterOCSpawnParameters:
- Mark the citizen as Commuter (set CitizenFlags.Commuter), update the citizen component.
- Set the new household's Household component flags to HouseholdFlags.Commuter and add a CommuterHousehold component with the selected outside-connection as original source.
- Remove LeaveHouseholdTag from the citizen (done via command buffer).
- Uses m_CommandBuffer (parallel writer) for all structural/component changes.
Notes on conditions and amounts: - New household receives kNewHouseholdStartMoney (2000) in its Resources buffer. - Existing household retains its money reduced down to kNewHouseholdStartMoney (so new household funds come from the original household). - Citizens only leave if they are Workers and their household has more than 2*kNewHouseholdStartMoney money (> 4000).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_CountResidentialPropertySystem = base.World.GetOrCreateSystemManaged<CountResidentialPropertySystem>();
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_LeaveHouseholdQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[2]
{
ComponentType.ReadOnly<Citizen>(),
ComponentType.ReadOnly<LeaveHouseholdTag>()
},
None = new ComponentType[2]
{
ComponentType.ReadOnly<Deleted>(),
ComponentType.ReadOnly<Temp>()
}
});
m_OutsideConnectionQuery = GetEntityQuery(ComponentType.ReadOnly<Game.Objects.OutsideConnection>(), ComponentType.Exclude<Game.Objects.ElectricityOutsideConnection>(), ComponentType.Exclude<Game.Objects.WaterPipeOutsideConnection>(), ComponentType.Exclude<Building>(), ComponentType.Exclude<Temp>(), ComponentType.Exclude<Deleted>());
m_HouseholdPrefabQuery = GetEntityQuery(ComponentType.ReadOnly<ArchetypeData>(), ComponentType.ReadOnly<HouseholdData>(), ComponentType.ReadOnly<DynamicHousehold>());
m_DemandParameterQuery = GetEntityQuery(ComponentType.ReadOnly<DemandParameterData>());
RequireForUpdate(m_LeaveHouseholdQuery);
}
Additional notes: - The job is Burst-compiled and designed to be high-performance: uses chunk iteration, buffer/component lookups, and an EntityCommandBuffer. Parallel structural changes are deferred to the EndFrameBarrier. - The logic ensures households cannot split unless there are sufficient funds and the citizen is a worker; it also models commuter households via outside-connection spawning logic and DemandParameterData. - Pay attention when modifying household prefabs, archetype data, or the HouseholdCitizen buffer layout — changes may require updating this system's assumptions.