Skip to content

Game.Serialization.ResidentSystem

Assembly: Game (runtime assembly: may appear in Assembly-CSharp / Game.dll depending on build)
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
ResidentSystem is a compiler-generated ECS system that synchronizes Creature Resident components with the Citizen entity that represents the resident's transport/citizen. It queries entities that have a CurrentTransport component and a Citizen marker, then schedules a Burst-compiled IJobChunk (ResidentJob) to iterate matching chunks. For each citizen entity it reads the CurrentTransport component, checks whether the referenced transport entity has a Resident component, and if so writes the resident's m_Citizen field to point to the citizen Entity. The system uses EntityTypeHandle and ComponentTypeHandle (read-only) plus a ComponentLookup (read/write) for efficient chunk processing and random-access writes.


Fields

  • private Unity.Entities.EntityQuery m_Query
    Holds the query used by the system. Constructed in OnCreate to select entities with a ReadOnly CurrentTransport and a ReadOnly Citizen component. Also passed to JobChunk scheduling and to RequireForUpdate so the system only runs when matching entities exist.

  • private ResidentSystem.TypeHandle __TypeHandle
    Compiler-generated container that stores the EntityTypeHandle, the ComponentTypeHandle (RO) and the ComponentLookup (RW). Populated by __AssignHandles(ref SystemState) in OnCreateForCompiler to produce the handles used when scheduling the ResidentJob.

Additional (nested) types (listed here for completeness):

  • private struct ResidentJob : IJobChunk
    Burst-compiled job that processes each chunk:
  • Fields:
    • EntityTypeHandle m_EntityType — used to get the NativeArray for the chunk.
    • ComponentTypeHandle<CurrentTransport> m_CurrentTransportType (ReadOnly) — used to get the CurrentTransport array.
    • ComponentLookup<Resident> m_ResidentData (writable) — used to test and write Resident components by Entity.
  • Behavior: iterates entries in the chunk; for each citizen entity reads its CurrentTransport.m_CurrentTransport (the transport entity). If that transport entity has a Resident component, the job loads the Resident, sets its m_Citizen to the citizen entity, and writes it back via the ComponentLookup.

  • private struct TypeHandle
    Contains:

  • EntityTypeHandle __Unity_Entities_Entity_TypeHandle
  • ComponentTypeHandle<CurrentTransport> __Game_Citizens_CurrentTransport_RO_ComponentTypeHandle
  • ComponentLookup<Resident> __Game_Creatures_Resident_RW_ComponentLookup
  • Method __AssignHandles(ref SystemState state) which initializes these handles from the provided SystemState (used by OnCreateForCompiler).

Properties

  • None (no public properties exposed by this system)

Constructors

  • [Preserve] public ResidentSystem()
    Default constructor. Marked with [Preserve] so it is kept by the runtime/linker. No custom initialization beyond base construction is performed here.

Methods

  • [Preserve] protected override void OnCreate()
    Creates and stores the EntityQuery:
  • m_Query = GetEntityQuery(ComponentType.ReadOnly(), ComponentType.ReadOnly());
  • Calls RequireForUpdate(m_Query) so the system runs only when matching entities exist.

  • [Preserve] protected override void OnUpdate()
    Builds and schedules the ResidentJob:

  • Uses InternalCompilerInterface to obtain runtime EntityTypeHandle, ComponentTypeHandle (RO) and ComponentLookup (RW) from the stored __TypeHandle and the system's CheckedStateRef.
  • Constructs ResidentJob with those handles and schedules it via JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency).
  • Stores the returned JobHandle in base.Dependency.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler-generated placeholder used in OnCreateForCompiler. In this build it creates and immediately disposes a temporary EntityQueryBuilder (likely a placeholder for query initialization).

  • protected override void OnCreateForCompiler()
    Compiler-inserted hook that:

  • Calls __AssignQueries(ref base.CheckedStateRef)
  • Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) This prepares the stored TypeHandle and (if needed) queries before runtime execution.

Nested/IJobChunk methods (in ResidentJob):

  • public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Primary job execution method. Reads chunk Entity array and CurrentTransport array, iterates entries and updates Resident component (if present) on the transport entity to set its m_Citizen to the current citizen Entity.

  • void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Explicit interface implementation that forwards to the job's Execute method.

Notes / Remarks: - The system uses BurstCompile on the job for performance and processes data by chunks for cache efficiency. - ComponentLookup is used for random-access writes to Resident components by Entity. The job checks HasComponent before reading/writing to avoid invalid accesses. - InternalCompilerInterface and OnCreateForCompiler methods are part of the generated wiring used by the source-gen/compilation pipeline; modders normally interact with the system through OnCreate/OnUpdate semantics.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // This mirrors what the system does: build the query and require it for updates
    m_Query = GetEntityQuery(ComponentType.ReadOnly<CurrentTransport>(), ComponentType.ReadOnly<Citizen>());
    RequireForUpdate(m_Query);
}

[Preserve]
protected override void OnUpdate()
{
    // The real system composes a ResidentJob and schedules it via JobChunkExtensions.Schedule.
    // This is a simplified illustration of the intended pattern.
    var job = new ResidentJob {
        m_EntityType = GetEntityTypeHandle(),
        m_CurrentTransportType = GetComponentTypeHandle<CurrentTransport>(isReadOnly: true),
        m_ResidentData = GetComponentLookup<Resident>()
    };
    base.Dependency = Unity.Entities.JobChunkExtensions.Schedule(job, m_Query, base.Dependency);
}