Skip to content

Game.Citizens.HouseholdPetInitializeSystem

Assembly:
Namespace: Game.Citizens

Type: class

Base: GameSystemBase

Summary: HouseholdPetInitializeSystem is an ECS system that initializes newly created HouseholdPet entities by registering them with their parent Household. It finds all entities with a HouseholdPet component and a Created tag (excluding Temp), and ensures the household has a HouseholdAnimal buffer containing references to its animals. The system uses a Burst-compiled IJob (InitializeHouseholdPetJob) to process archetype chunks in parallel and a ModificationBarrier5 (command buffer) to add HouseholdAnimal buffers for households that don't already expose a runtime buffer. The job uses temporary NativeParallelMultiHashMap/NativeList when needed to aggregate pets for households that require buffer creation via the command buffer. {{ This system runs automatically when there are HouseholdPet entities marked Created, and moves pet Entity references into the corresponding Household's HouseholdAnimal dynamic buffer. It carefully handles both households that already have the buffer (using BufferLookup.Add) and households that require buffer creation (using a command buffer produced by ModificationBarrier5). }}


Fields

  • private EntityQuery m_HouseholdPetQuery {{ Query used to locate entities that have a HouseholdPet component and the Created tag while excluding Temp. This query drives the system's update by calling RequireForUpdate in OnCreate. }}

  • private ModificationBarrier5 m_ModificationBarrier {{ Barrier system used to create an EntityCommandBuffer for structural changes (adding buffers) safely from a job. The system schedules the job and registers the job handle with this barrier. }}

  • private TypeHandle __TypeHandle {{ Internal container that stores EntityTypeHandle, ComponentTypeHandle, ComponentLookup and BufferLookup. The handles are assigned for proper access inside the job via __AssignHandles. This is a compiler-generated convenience wrapper. }}

Properties

  • This type exposes no public properties. {{ All required access to components inside the job is performed via the TypeHandle and direct job fields; no public properties are provided by the system. }}

Constructors

  • public HouseholdPetInitializeSystem() {{ Default constructor (preserved). Standard initialization is performed in OnCreate rather than in the constructor. }}

Methods

  • protected override void OnCreate() : System.Void {{ Sets up the system: obtains the ModificationBarrier5, constructs the query for HouseholdPet + Created (exclude Temp), and calls RequireForUpdate to ensure the system runs only when matching entities exist. }}

  • protected override void OnUpdate() : System.Void {{ Schedules the Burst-compiled InitializeHouseholdPetJob. It:

  • Converts the query to an ArchetypeChunk list asynchronously,
  • Resolves EntityTypeHandle, ComponentTypeHandle, ComponentLookup, and BufferLookup via InternalCompilerInterface and the TypeHandle,
  • Creates an EntityCommandBuffer from the ModificationBarrier5 to create buffers on households that don't yet have them,
  • Combines dependencies with the async chunk creation handle and schedules the job,
  • Registers the job handle with the modification barrier and assigns the system base dependency. }}

  • protected override void OnCreateForCompiler() : System.Void {{ Compiler helper used to assign queries and type handles during code-gen / IL2CPP scenarios. Calls __AssignQueries and __TypeHandle.__AssignHandles. }}

  • private void __AssignQueries(ref SystemState state) : System.Void {{ (Compiler-generated) Placeholder to ensure queries are created/validated. In this implementation it creates and disposes a temporary EntityQueryBuilder (no-op here). }}

  • InitializeHouseholdPetJob.Execute() : System.Void {{ The core job logic (Burst compiled). For each chunk:

  • Reads entities and their HouseholdPet component,
  • If the referenced Household already has a HouseholdAnimal buffer (BufferLookup.HasBuffer), it appends the pet directly,
  • Otherwise, if the household has a Household component (ComponentLookup.HasComponent) it collects the pet into a NativeParallelMultiHashMap keyed by the household entity. It also tracks unique households needing buffers in a NativeList. After scanning chunks, for every household in the list that needs a buffer, it creates a DynamicBuffer on that household via the EntityCommandBuffer and populates it with the collected pets. Temporary Native containers are allocated only when needed and are disposed before completion. }}

  • TypeHandle.__AssignHandles(ref SystemState state) {{ Assigns the internal EntityTypeHandle, ComponentTypeHandle (read-only), ComponentLookup (read-only), and BufferLookup for use inside the scheduled job. This is annotated with AggressiveInlining to minimize overhead. }}

Usage Example

// Example: create a household and a pet so the system will run and register the pet on the household's buffer.
using Unity.Entities;

// Create household entity with a Household component
Entity household = entityManager.CreateEntity();
entityManager.AddComponentData(household, new Household { /* ... household data ... */ });

// Mark the household so it can receive buffer components if needed (not required if buffer already exists)
if (!entityManager.HasComponent<HouseholdAnimal>(household))
{
    // The system will add the dynamic buffer for households that require it.
}

// Create a pet entity and mark it Created so the HouseholdPetInitializeSystem will process it
Entity pet = entityManager.CreateEntity();
entityManager.AddComponentData(pet, new HouseholdPet { m_Household = household });
entityManager.AddComponentData(pet, new Created()); // system's query looks for Created
// Ensure the temporary tag is not present: Temp is excluded by the system's query

// On the next frame/update the HouseholdPetInitializeSystem will schedule a job and either:
//  - add the pet to the existing HouseholdAnimal buffer on `household` (if present), or
//  - create the buffer via a command buffer and populate it with the pet (if required).

{{ Notes: - The system depends on the presence of a Created tag on HouseholdPet entities and excludes Temp-tagged entities. - It uses a ModificationBarrier5 to perform structural changes from the job (adding buffers). Keep in mind barrier naming may be implementation specific and used to coordinate structural writes. - The job is Burst-compiled and uses Native collections; ensure compatibility with your mod's burst/runtime configuration. - The system is designed to be safe in multithreaded contexts; it avoids structural changes directly inside the job by batching required buffer creations through the command buffer. }}