Skip to content

Game.Serialization.HouseholdAnimalSystem

Assembly:
Game

Namespace:
Game.Serialization

Type:
class (public, CompilerGenerated)

Base:
GameSystemBase

Summary:
HouseholdAnimalSystem is a serialization-time ECS system that collects HouseholdPet components and registers them with their corresponding household entities' HouseholdAnimal dynamic buffers. It runs as a Burst-compiled IJobChunk (HouseholdAnimalJob) and uses a DeserializationBarrier to produce EntityCommandBuffer operations safely during deserialization. If a HouseholdPet refers to a household entity that does not have a HouseholdAnimal buffer, the system marks the pet entity with a Deleted component so it can be removed/cleaned up later. The system is required-for-update only when HouseholdPet components exist.


Fields

  • private DeserializationBarrier m_DeserializationBarrier
    Used to create an EntityCommandBuffer for structural changes produced by the job and to register the job handle with the barrier so the DeserializationBarrier can manage dependencies and replay/complete structural changes at the correct point in the frame.

  • private EntityQuery m_Query
    EntityQuery filtering entities that have a read-only HouseholdPet component. The system uses this query to schedule the chunk job only when HouseholdPet components are present (RequireForUpdate).

  • private TypeHandle __TypeHandle
    Internal container for the EntityTypeHandle, ComponentTypeHandle (read-only), and BufferLookup (write). Populated via __AssignHandles and used to build the job's type handles in OnUpdate.

  • private struct HouseholdAnimalJob (nested)
    A Burst-compiled IJobChunk that:

  • Reads entity and HouseholdPet component arrays for each chunk.
  • Uses BufferLookup to add a HouseholdAnimal entry to the household's buffer when present.
  • Uses an EntityCommandBuffer to add a Deleted component to the pet entity when the household buffer is missing.
  • This struct contains fields: m_EntityType, m_HouseholdPetType, m_HouseholdAnimals, m_CommandBuffer and implements Execute on chunks.

  • private struct TypeHandle (nested)
    Holds the component/handle types required by the job (EntityTypeHandle, ComponentTypeHandle read-only, BufferLookup write) and provides __AssignHandles(ref SystemState) to obtain those handles from a SystemState.

Properties

  • None public.
    All required handles are stored in the private __TypeHandle and passed to the job via InternalCompilerInterface in OnUpdate. The system does not expose public properties.

Constructors

  • public HouseholdAnimalSystem()
    Default constructor; marked with [Preserve] attribute. The system relies on OnCreate to initialize runtime dependencies (DeserializationBarrier and query).

Methods

  • protected override void OnCreate()
    Initializes the system: obtains the DeserializationBarrier system instance from the World, creates an EntityQuery for entities with HouseholdPet (read-only), and calls RequireForUpdate(m_Query) so the system only runs if HouseholdPet entities exist. Marked with [Preserve].

  • protected override void OnUpdate()
    Builds and schedules the HouseholdAnimalJob:

  • Uses InternalCompilerInterface to turn __TypeHandle entries into runtime handles (EntityTypeHandle, ComponentTypeHandle, BufferLookup).
  • Sets m_CommandBuffer to m_DeserializationBarrier.CreateCommandBuffer() so the job can enqueue structural changes (Deleted component) safely.
  • Schedules the job with JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency) and attaches the returned dependency to base.Dependency.
  • Registers the job handle with m_DeserializationBarrier via AddJobHandleForProducer so the barrier knows when structural changes are produced.

  • protected override void OnCreateForCompiler()
    Called by the generated code path to set up handles for the compiler-time environment:

  • Calls __AssignQueries(ref base.CheckedStateRef) (which currently creates and immediately disposes an EntityQueryBuilder; present for IL generation/compatibility).
  • Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to initialize the type handles used by the job.

  • private void __AssignQueries(ref SystemState state)
    An aggressive-inlining helper used by OnCreateForCompiler that builds queries required by the system; currently creates a temporary EntityQueryBuilder and disposes it. This method exists to mirror the compiled system initialization pattern.

  • void HouseholdAnimalJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Implements the job logic: for each entity in the chunk it:

  • Reads the Entity and HouseholdPet arrays.
  • Checks if the referenced household entity has a HouseholdAnimal buffer via BufferLookup.HasBuffer.
  • If it has the buffer, appends a new HouseholdAnimal (wrapping the pet entity) to that buffer.
  • If it does not, enqueues a Deleted component addition to the pet entity through the EntityCommandBuffer.

  • TypeHandle.__AssignHandles(ref SystemState state)
    Fill in the TypeHandle fields from the SystemState: EntityTypeHandle, ComponentTypeHandle (read-only), and BufferLookup.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // ensure the system only runs when HouseholdPet components are present
    m_DeserializationBarrier = base.World.GetOrCreateSystemManaged<DeserializationBarrier>();
    m_Query = GetEntityQuery(ComponentType.ReadOnly<HouseholdPet>());
    RequireForUpdate(m_Query);
}

[Preserve]
protected override void OnUpdate()
{
    var jobData = new HouseholdAnimalJob
    {
        m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
        m_HouseholdPetType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Citizens_HouseholdPet_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_HouseholdAnimals = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Citizens_HouseholdAnimal_RW_BufferLookup, ref base.CheckedStateRef),
        m_CommandBuffer = m_DeserializationBarrier.CreateCommandBuffer()
    };

    // schedule the chunk job and register it with the deserialization barrier
    base.Dependency = JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency);
    m_DeserializationBarrier.AddJobHandleForProducer(base.Dependency);
}

Notes and tips: - This system is designed to run during deserialization/scene loading and relies on DeserializationBarrier to safely perform structural changes via an EntityCommandBuffer. - HouseholdAnimal buffer additions are done from jobs via BufferLookup. Ensure the Household entity archetypes that should receive animals have that buffer component present at the appropriate time, otherwise the pet entities are marked Deleted.