Skip to content

Game.Simulation.HouseholdMoveAwaySystem

Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Handles households that are leaving the city ("moving away"). The system finds entities with a MovingAway component and processes them in a parallel IJobChunk (MoveAwayJob). Responsibilities include selecting an outside connection target (if none), removing Worker components from citizens in the household, creating rent update events for temporary homes, queuing city statistics changes (citizens moved away, tourist income), enqueueing trigger actions (CitizenMovedOutOfCity), and finally marking the household entity as Deleted so it will be cleaned up. The job uses the EndFrameBarrier command buffer to defer structural changes and coordinates with CityStatisticsSystem and TriggerSystem. The system is scheduled to run every 16 frames.


Fields

  • private EntityQuery m_MoveAwayGroup
    Query selecting households to process. Matches entities with Household, HouseholdCitizen buffers, MovingAway (read/write), Resources buffer, excluding Deleted and Temp. Required for update.

  • private EntityQuery m_OutsideConnectionQuery
    Query used to gather available outside connection entities (excluding electricity/water outside connections and Temp/Deleted). Its entity list is passed to the job so a random outside connection can be chosen as a target.

  • private EntityArchetype m_RentEventArchetype
    Archetype used to create RentersUpdated events (Event + RentersUpdated components) when a homeless household previously had a temporary home that must be notified/updated.

  • private EndFrameBarrier m_EndFrameBarrier
    Reference to the EndFrameBarrier system used to create a parallel EntityCommandBuffer for deferred structural changes (adding Deleted, removing Worker, creating event entities, etc.).

  • private CityStatisticsSystem m_CityStatisticsSystem
    Reference to the CityStatisticsSystem used to enqueue statistics events (as a parallel writer).

  • private TriggerSystem m_TriggerSystem
    Reference to the TriggerSystem used to create a TriggerAction buffer (as a parallel writer) for events such as CitizenMovedOutOfCity.

  • private TypeHandle __TypeHandle
    Internal struct holding ECS type/buffer/component handles used to build the job. Assigned in OnCreateForCompiler.

  • private struct TypeHandle (nested)
    Internal struct with the actual EntityTypeHandle, BufferTypeHandles, ComponentTypeHandles and ComponentLookup/BufferLookup fields and an __AssignHandles method to populate them from a SystemState. (Used by the generated job setup.)

Properties

  • (none public)

Constructors

  • public HouseholdMoveAwaySystem()
    Default constructor. The system is initialized by the framework; real setup is done in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 16. This system is configured to run every 16 frames (coarse update frequency).

  • protected override void OnCreate()
    Sets up dependencies and queries:

  • Acquires references to EndFrameBarrier, CityStatisticsSystem and TriggerSystem from the world.
  • Builds m_MoveAwayGroup and m_OutsideConnectionQuery.
  • Creates the m_RentEventArchetype (Event + RentersUpdated).
  • Calls RequireForUpdate(m_MoveAwayGroup) so the system only runs when there are matching entities.

  • protected override void OnUpdate()
    Creates and schedules MoveAwayJob as a parallel JobChunk:

  • Fills the job data with Entity/Buffer/Component handles, RandomSeed, outside connection entity list (ToEntityListAsync), and parallel writers for statistics and triggers, plus a parallel command buffer from EndFrameBarrier.
  • Schedules the job with JobChunkExtensions.ScheduleParallel and combines dependencies.
  • Disposes the temporary outside connection entity list after scheduling.
  • Registers the producer job handle with EndFrameBarrier and signals writers on CityStatisticsSystem and TriggerSystem.

  • protected override void OnCreateForCompiler()
    Compiler helper to assign type handles and queries for generated code paths. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignQueries(ref SystemState state)
    Internal helper (generated) for query assignment (no functional body here besides a placeholder).

  • private struct MoveAwayJob : IJobChunk (nested)
    The job that runs per archetype chunk. Key behavior:

  • Iterates households in the chunk.
  • Ensures MovingAway.m_Target is valid; if not, selects an outside connection entity based on available owned vehicles (Road vs. Train/Air/Ship).
  • Removes Worker component from all household citizens (via command buffer).
  • If HomelessHousehold.m_TempHome exists and that temp home has a renter buffer, creates a RentersUpdated event entity and clears HomelessHousehold.m_TempHome.
  • For TouristHousehold: deducts stored money (Resources buffer) from TouristIncome statistics.
  • If household had the MovedIn flag, enqueues a CitizensMovedAway statistic (count = household size).
  • If household is a property renter, enqueues TriggerAction(s) of type CitizenMovedOutOfCity with the renter property as context.
  • Finally, adds a Deleted component to the household entity via the command buffer to mark it for removal.
  • Uses RandomSeed.GetRandom(unfilteredChunkIndex) to pick random outside connections.
  • Operates with many read-only ComponentLookup/BufferLookup handles and uses parallel writers for statistics and triggers.

Notes on thread-safety and ordering: - Structural changes are done through EndFrameBarrier's parallel command buffer. - Statistics and trigger buffers are written via provided parallel writers and registered with the corresponding systems. - The job uses nested buffer accessors and ComponentLookup reads; ensure any additional custom code respects these access patterns.

Usage Example

// Mark a household entity as moving away. The HouseholdMoveAwaySystem will process it
// on its next scheduled run (every 16 frames) and perform the move-away flow.
Entity household = /* obtain household entity */;
if (!entityManager.HasComponent<MovingAway>(household))
{
    entityManager.AddComponentData(household, new MovingAway { m_Target = Entity.Null });
}

Additional example: if you need to simulate a specific outside connection target beforehand:

entityManager.SetComponentData(household, new MovingAway { m_Target = myOutsideConnectionEntity });

Notes for modders: - The system expects HouseholdCitizen buffer entries to reference valid citizen entities. It will attempt to remove Worker components from those citizens. - If you create or modify OutsideConnection entities, they must match the filters used by m_OutsideConnectionQuery to be selectable by the system (exclude electricity/water outside connections). - Because structural changes are deferred via EndFrameBarrier, changes will be applied at the end of the frame after the job completes.