Skip to content

Game.Serialization.RenterSystem

Assembly: Assembly-CSharp
Namespace: Game.Serialization

Type: public class RenterSystem

Base: GameSystemBase

Summary:
RenterSystem runs as part of the deserialization flow and populates building renter buffers after entities/components are deserialized. The system schedules a Burst-compiled IJobChunk (RenterJob) that: - Iterates entities that have any of PropertyRenter, TouristHousehold or HomelessHousehold components. - For PropertyRenter: looks up the building's Renter buffer and appends the renter Entity to it. If the renter entity is a company (has CompanyData) it checks for duplicate company renters already in the buffer; if a duplicate company is detected it marks the entity with a Deleted component instead of adding it. - If a PropertyRenter references a property that does not have a Renter buffer and the entity is not a service building (checked via CityServiceUpkeep), the PropertyRenter component is removed from the entity. - For TouristHousehold: appends the renter to the referenced hotel buffer. - For HomelessHousehold: appends the renter to the referenced temp-home buffer.

The system uses a DeserializationBarrier to obtain an EntityCommandBuffer for structural changes and registers its job handle with that barrier so changes are correctly ordered during deserialization.


Fields

  • private DeserializationBarrier m_DeserializationBarrier
    Used to coordinate structural changes for the deserialization phase. The system creates an EntityCommandBuffer from this barrier and registers the scheduled job as a producer so that the barrier can wait on/complete the job when appropriate.

  • private EntityQuery m_Query
    An EntityQuery that matches entities which have any of PropertyRenter, TouristHousehold or HomelessHousehold. The system calls RequireForUpdate(m_Query) so it runs only when such entities are present.

  • private TypeHandle __TypeHandle
    Internal helper struct that caches EntityTypeHandle, ComponentTypeHandles and Component/Buffer lookups used by the scheduled job. The handles are assigned for the current SystemState in OnCreateForCompiler/OnUpdate.

(There are also two nested types used by the system: the Burst-compiled RenterJob (IJobChunk) and the TypeHandle struct. They are implementation details and are described below.)

Properties

  • This system exposes no public properties.

Constructors

  • public RenterSystem()
    Default constructor. The system is [Preserve]d and otherwise has no custom constructor logic; initialization happens in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes the system for deserialization:
  • Retrieves/creates the DeserializationBarrier from the World.
  • Builds an EntityQuery that matches entities with any of PropertyRenter, TouristHousehold or HomelessHousehold.
  • Calls RequireForUpdate(m_Query) so the system only runs when matching entities exist.

  • protected override void OnUpdate()
    Prepares and schedules the Burst-compiled RenterJob:

  • Acquires EntityTypeHandle, ComponentTypeHandles and Component/Buffer lookups (via InternalCompilerInterface and the cached __TypeHandle).
  • Creates an EntityCommandBuffer from m_DeserializationBarrier for safe structural changes.
  • Schedules the job with JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency).
  • Adds the job handle to m_DeserializationBarrier (m_DeserializationBarrier.AddJobHandleForProducer) to ensure the barrier accounts for the scheduled work.

  • protected override void OnCreateForCompiler()
    Compiler plumbing: assigns queries and type handles for use by the generated code path. Calls __AssignQueries and __TypeHandle.__AssignHandles with the checked system state.

  • private void __AssignQueries(ref SystemState state)
    Internal helper used by the generated OnCreateForCompiler path. In this implementation it creates and immediately disposes an EntityQueryBuilder(Allocator.Temp) (a placeholder/no-op in this compiled form).

  • Nested: RenterJob (BurstCompile, implements IJobChunk)

  • Fields: EntityTypeHandle, ComponentTypeHandles for PropertyRenter, TouristHousehold, HomelessHousehold; ComponentLookups for CityServiceUpkeep and CompanyData; BufferLookup; EntityCommandBuffer.
  • Execute(in ArchetypeChunk chunk, ...) behavior:

    • Reads arrays for Entity, PropertyRenter, TouristHousehold, HomelessHousehold for the chunk.
    • For each PropertyRenter in the chunk:
    • If the referenced property has a Renter buffer, attempts to add the renter. If the renter entity has CompanyData, it scans the existing buffer for an existing company renter and, if found, logs a warning and adds a Deleted component to the duplicate company entity instead of adding it.
    • If the property does not have a Renter buffer and the entity is not a service building (no CityServiceUpkeep), the job issues a RemoveComponent via the command buffer.
    • For each TouristHousehold and HomelessHousehold row, it adds a Renter entry to the referenced hotel/temp-home buffers if present.
  • Nested: TypeHandle.__AssignHandles(ref SystemState state)
    Assigns and caches all EntityTypeHandle/ComponentTypeHandle/ComponentLookup/BufferLookup instances used by RenterJob for the provided SystemState. This reduces repeated handle allocation on each OnUpdate.

Usage Example

Example: reading the Renter buffer from another system after RenterSystem has run. This shows how to access the Renter buffer attached to a property entity and iterate the stored renter Entities.

protected override void OnUpdate()
{
    var rentersLookup = GetBufferLookup<Renter>(isReadOnly: true);
    Entity propertyEntity = /* obtain property entity reference */;

    if (rentersLookup.HasBuffer(propertyEntity))
    {
        var renters = rentersLookup[propertyEntity];
        for (int i = 0; i < renters.Length; i++)
        {
            Entity renter = renters[i].m_Renter;
            // Process renter entity (e.g., resolve to components like Citizen or Company)
        }
    }
}

Notes and tips: - RenterSystem is intended to run during deserialization; it relies on DeserializationBarrier to synchronize its scheduled job with the deserialization process. - Structural changes are performed via the EntityCommandBuffer produced from the barrier; they take effect after the job completes. - The system guards against adding duplicate company renters to the same property by checking CompanyData on both the candidate renter and existing renters in the buffer; duplicates are marked Deleted to avoid twice-registering the same company. - Because the job is Burst-compiled and runs as an IJobChunk, it works on chunks and uses ComponentLookups/BufferLookups for efficient access.