Skip to content

Game.HouseholdCitizenSystem

Assembly: Assembly-CSharp (game assembly; may appear under Game or Assembly-CSharp in the build)
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
HouseholdCitizenSystem is a GameSystemBase that collects citizen Entities into per-household buffers during deserialization. It schedules a Burst-compiled IJobChunk (HouseholdCitizenJob) that iterates over entities with HouseholdMember components, and for each member it either appends a HouseholdCitizen entry to the household's buffer or marks the citizen entity as Deleted (via an EntityCommandBuffer) if the household buffer does not exist. The system uses a DeserializationBarrier to create the command buffer and to register the produced job handle for correct dependency ordering during deserialization.


Fields

  • private DeserializationBarrier m_DeserializationBarrier
    Holds a reference to the world's DeserializationBarrier system. Used to create an EntityCommandBuffer for deferred structural changes and to register the system's job handle with the barrier.

  • private EntityQuery m_Query
    An EntityQuery that selects entities with the HouseholdMember component. The system requires this query for update.

  • private TypeHandle __TypeHandle
    Container for the entity/component type handles used by the job (see nested TypeHandle struct). Populated for compiler compatibility and to retrieve handles from SystemState.

  • private struct HouseholdCitizenJob (nested)
    Burst-compiled IJobChunk that performs the core work of collecting household citizens. Fields and behavior:

  • [ReadOnly] public EntityTypeHandle m_EntityType — entity handle for the chunk.
  • [ReadOnly] public ComponentTypeHandle<HouseholdMember> m_HouseholdMemberType — read-only handle to HouseholdMember component.
  • public BufferLookup<HouseholdCitizen> m_HouseholdCitizens — lookup to write into household buffers.
  • public EntityCommandBuffer m_CommandBuffer — used to add Deleted component when household buffer is missing.
  • Execute: iterates chunk entities + HouseholdMember components; for each member, if household buffer exists, adds a HouseholdCitizen element with m_Citizen set to the entity; otherwise adds Deleted to the entity.

  • private struct TypeHandle (nested)
    Holds component/entity type handles used by the job:

  • [ReadOnly] public EntityTypeHandle __Unity_Entities_Entity_TypeHandle
  • [ReadOnly] public ComponentTypeHandle<HouseholdMember> __Game_Citizens_HouseholdMember_RO_ComponentTypeHandle
  • public BufferLookup<HouseholdCitizen> __Game_Citizens_HouseholdCitizen_RW_BufferLookup
  • Method __AssignHandles(ref SystemState state) populates the above handles from the provided SystemState.

Properties

  • None (no public properties are declared on this system).

Constructors

  • public HouseholdCitizenSystem()
    Default public constructor. Marked with [Preserve] on other lifecycle methods but the constructor itself is straightforward and contains no custom initialization beyond default CLR construction.

Methods

  • protected override void OnCreate()
  • Called on system creation. Implementation:

    • Calls base.OnCreate().
    • Retrieves the DeserializationBarrier system with base.World.GetOrCreateSystemManaged() and stores it in m_DeserializationBarrier.
    • Creates an EntityQuery for ComponentType.ReadOnly() and assigns it to m_Query.
    • Calls RequireForUpdate(m_Query) so the system runs only when matching entities exist.
  • protected override void OnUpdate()

  • Builds an instance of HouseholdCitizenJob and schedules it over m_Query:

    • Obtains entity/component/buffer handles via InternalCompilerInterface.Get* methods using the stored __TypeHandle entries and base.CheckedStateRef.
    • Sets the job's EntityCommandBuffer via m_DeserializationBarrier.CreateCommandBuffer().
    • Schedules the job with JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency).
    • Registers the resulting dependency with the deserialization barrier (m_DeserializationBarrier.AddJobHandleForProducer(base.Dependency)).
  • protected override void OnCreateForCompiler()

  • Called for compiler-specific initialization:

    • Calls __AssignQueries(ref base.CheckedStateRef) and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to prepare the type handles and any query scaffolding required by generated code.
  • private void __AssignQueries(ref SystemState state)

  • A small helper invoked in OnCreateForCompiler. In the current implementation it creates and disposes a temporary EntityQueryBuilder (no-op placeholder) — kept for the generated-system pattern.

  • Nested/IJobChunk struct method: HouseholdCitizenJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)

  • Implementation details:
    • Retrieves NativeArray from the chunk via m_EntityType.
    • Retrieves NativeArray from the chunk via m_HouseholdMemberType.
    • Iterates elements; for each entry:
    • If m_HouseholdCitizens.HasBuffer(householdMember.m_Household) is true, appends a new HouseholdCitizen { m_Citizen = entity } to that buffer.
    • Otherwise, issues m_CommandBuffer.AddComponent(entity, default(Deleted)) to mark the citizen as deleted.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire the deserialization barrier and set up the query so this system runs
    m_DeserializationBarrier = base.World.GetOrCreateSystemManaged<DeserializationBarrier>();
    m_Query = GetEntityQuery(ComponentType.ReadOnly<HouseholdMember>());
    RequireForUpdate(m_Query);
}

Notes / Tips: - The system is intended to run during deserialization (hence the DeserializationBarrier usage) to rebuild household-to-citizen buffers. - The job is Burst-compiled and scheduled as a chunk job (IJobChunk), making it efficient for large batches of entities. - If a HouseholdMember references a non-existent household buffer, the citizen entity is marked with Deleted to ensure consistency; this means downstream code should handle Deleted appropriately during restore/cleanup phases.