Game.Simulation.HouseholdPetBehaviorSystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that updates household pet entities on a fixed interval (every 16 simulation frames). It runs a Burst-compiled IJobChunk (HouseholdPetTickJob) to:
- Remove pets whose parent household no longer exists.
- Validate and correct pets' CurrentTransport / CurrentBuilding state when the pet has no Target component.
- Remove transports when they are no longer valid.
- Move pets back to their home building when appropriate (using PropertyRenter / TouristHousehold -> hotel -> property lookup).
Work is queued to an EndFrameBarrier (EntityCommandBuffer) so structural changes (Delete/Remove/Add component) occur safely at frame end.
Fields
-
private EntityQuery m_HouseholdPetQuery
Holds the query used to find HouseholdPet entities that should be updated. Query requires HouseholdPet + UpdateFrame and excludes Deleted and Temp. The query is filtered per-frame with a shared UpdateFrame component so only a subset of pets run each tick. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to read the global frame index when calculating which UpdateFrame shared component to schedule for this system. -
private EndFrameBarrier m_EndFrameBarrier
Barrier used to obtain an EntityCommandBuffer. The system uses the barrier's parallel writer to enqueue component additions/removals and entity deletions from within the job. -
private TypeHandle __TypeHandle
Container struct used by the generated code to store ComponentTypeHandle, ComponentLookup and BufferLookup instances for the job. Assigned in OnCreateForCompiler and used each OnUpdate to obtain type handles for the job scheduling.
Properties
- None (no public properties)
Constructors
public HouseholdPetBehaviorSystem()
Default constructor (preserved attribute present on the class). The system initialization happens in OnCreate rather than constructor.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 16 — the system's update interval in frames. The system only runs its workload on 1/16th of simulation frames (controlled via UpdateFrame shared component filtering). -
[Preserve] protected override void OnCreate()
OnCreate obtains references to SimulationSystem and EndFrameBarrier and builds the m_HouseholdPetQuery (HouseholdPet + UpdateFrame, excluding Deleted/Temp). It calls RequireForUpdate(m_HouseholdPetQuery) so the system only updates when matching entities exist. -
[Preserve] protected override void OnUpdate()
Main scheduling method. It: - Computes the current UpdateFrame value to use (SimulationUtils.GetUpdateFrameWithInterval).
- Applies that value as a shared component filter on m_HouseholdPetQuery.
- Creates and schedules the Burst-compiled HouseholdPetTickJob via JobChunkExtensions.ScheduleParallel, passing EntityTypeHandle/ComponentTypeHandle/ComponentLookup/BufferLookup instances and an EntityCommandBuffer.ParallelWriter from m_EndFrameBarrier.
- Adds the returned job handle to the EndFrameBarrier so the ECB is played back only after the job completes.
-
Stores the job handle on base.Dependency.
-
protected override void OnCreateForCompiler()
Called by generated code paths. It calls __AssignQueries and assigns handles via __TypeHandle.__AssignHandles(ref state). This prepares the TypeHandle members for use in scheduling. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Internal helper used by the generated OnCreateForCompiler. In this compilation it only creates/disposes an EntityQueryBuilder; effectively a placeholder used by the source generation/compiler integration.
Nested/Job-related behavior (HouseholdPetTickJob):
- The private Burst-compiled struct HouseholdPetTickJob : IJobChunk does chunk-based processing of HouseholdPet entities.
- For each pet it:
- Checks that the referenced household entity still exists (via ComponentLookup
Important implementation notes:
- Uses EntityStorageInfoLookup to check existence of referenced property entities.
- Uses ComponentLookup
Usage Example
// Typical usage: the system is created/managed by the world. You can get a reference to it:
var system = World.DefaultGameObjectInjectionWorld
.GetOrCreateSystemManaged<Game.Simulation.HouseholdPetBehaviorSystem>();
// You normally don't call OnUpdate manually — the game updates systems automatically.
// To create a HouseholdPet entity that this system will process, ensure it has the required components:
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var householdEntity = /* an existing Household entity */;
var pet = entityManager.CreateEntity(
typeof(Game.Citizens.HouseholdPet),
typeof(Game.Common.UpdateFrame) // update-frame shared component to schedule which tick will run this pet
);
// Set the HouseholdPet data (pseudo-fields; replace with actual component structure)
entityManager.SetComponentData(pet, new Game.Citizens.HouseholdPet { m_Household = householdEntity });
// The system will run every 16 frames and process the pet on the appropriate UpdateFrame.
If you are modding behavior, consider: - Ensuring HouseholdPet entities are created with the same component patterns (UpdateFrame shared component) used by the system. - If changing deletion or transport logic, note that structural changes are enqueued via EndFrameBarrier's ECB — modifying that flow will require handling job dependencies and barriers similarly.