Skip to content

Game.Prefabs.VehicleCapacitySystem

Assembly:
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
VehicleCapacitySystem is an ECS system responsible for maintaining and updating the list of selectable delivery trucks (DeliveryTruckSelectData) used by UI/selection logic. It watches for relevant prefab/vehicle updates, collects matching delivery truck prefabs via an EntityQuery, computes selection requirements using VehicleSelectRequirementData, and schedules a job (UpdateDeliveryTruckSelectJob) that fills a persistent NativeList. The system supports post-deserialization initialization to trigger a full update after loading or starting a new game.


Fields

  • private CityConfigurationSystem m_CityConfigurationSystem
    Used to access city configuration; retrieved in OnCreate via World.GetOrCreateSystemManaged() and used when updating selection requirement data.

  • private EntityQuery m_UpdatedQuery
    An EntityQuery that finds entities with VehicleData and PrefabData and that have either the Updated or Deleted component. This is used to detect when vehicle/prefab data changes and trigger an update.

  • private EntityQuery m_DeliveryTruckQuery
    An EntityQuery that matches delivery truck prefabs: All of DeliveryTruckData, CarData, ObjectData and PrefabData, and excludes Locked. This query is enumerated asynchronously to produce archetype chunks for the scheduled job.

  • private NativeList<DeliveryTruckSelectItem> m_DeliveryTruckItems
    Persistent NativeList used to store select items produced by the scheduled job. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy. Read via GetDeliveryTruckSelectData() after ensuring job completion.

  • private JobHandle m_WriteDependency
    Stores the JobHandle returned by scheduling UpdateDeliveryTruckSelectJob. Used to complete the job before reading m_DeliveryTruckItems (e.g., in GetDeliveryTruckSelectData).

  • private VehicleSelectRequirementData m_VehicleSelectRequirementData
    Helper data structure used to determine which vehicles meet selection requirements; updated every time an update is triggered and passed into the scheduled job.

  • private bool m_RequireUpdate
    Flag set by PostDeserialize (for NewGame or LoadGame) to force a full update on the next OnUpdate. Also cleared by OnUpdate when an update is performed.

  • private TypeHandle __TypeHandle
    Compiler-generated nested struct instance that caches EntityTypeHandle and ComponentTypeHandle instances for efficient job access. It is initialized in OnCreateForCompiler / __AssignHandles.

Properties

  • (none declared in this class)
    This class does not expose public properties. All state is maintained via fields and methods.

Constructors

  • public VehicleCapacitySystem()
    Default constructor (preserved). No custom initialization beyond what OnCreate performs.

Methods

  • protected override void OnCreate()
    Initializes the system:
  • Calls base.OnCreate().
  • Obtains the CityConfigurationSystem instance.
  • Creates a VehicleSelectRequirementData instance bound to this system.
  • Builds two EntityQuery instances:
    • m_UpdatedQuery: matches entities with VehicleData & PrefabData and (Updated | Deleted).
    • m_DeliveryTruckQuery: matches DeliveryTruckData, CarData, ObjectData, PrefabData and excludes Locked.
  • Allocates m_DeliveryTruckItems as a persistent NativeList. This sets up the data required for OnUpdate to detect changes and schedule work.

  • protected override void OnDestroy()
    Disposes persistent resources:

  • Disposes m_DeliveryTruckItems.
  • Calls base.OnDestroy().

  • public void PostDeserialize(Context context)
    Implements IPostDeserialize. If context.purpose is Purpose.NewGame or Purpose.LoadGame, sets m_RequireUpdate = true so that a full rebuild of selectable delivery trucks happens on the next update.

  • protected override void OnUpdate()
    Main update loop:

  • If m_RequireUpdate is true or there are entities matching m_UpdatedQuery, an update is performed.
  • Resets m_RequireUpdate.
  • Calls m_DeliveryTruckQuery.ToArchetypeChunkListAsync(...) to collect prefab chunks and obtain a dependency JobHandle for that read.
  • Calls m_VehicleSelectRequirementData.Update(this, m_CityConfigurationSystem) to refresh requirement data.
  • Schedules UpdateDeliveryTruckSelectJob with component/entity type handles from __TypeHandle, passing:
    • the prefabChunks (ArchetypeChunk list)
    • m_RequirementData and m_DeliveryTruckItems to be filled
  • Combines dependencies (base.Dependency and the ToArchetypeChunkListAsync job), stores returned job handle into m_WriteDependency and also assigns it to base.Dependency.
  • Ensures prefabChunks.Dispose(jobHandle) is scheduled so the temporary chunk list is disposed once the job completes.

Note: The scheduled job fills m_DeliveryTruckItems asynchronously; callers must complete m_WriteDependency before reading the list.

  • public DeliveryTruckSelectData GetDeliveryTruckSelectData()
    Completes the outstanding write job (m_WriteDependency.Complete()) to ensure m_DeliveryTruckItems is ready, then returns a new DeliveryTruckSelectData constructed from m_DeliveryTruckItems.AsArray(). Use this to obtain a safe, managed snapshot of the current delivery truck selection items.

  • private void __AssignQueries(ref SystemState state)
    Compiler / tooling helper; currently empty (creates and disposes an EntityQueryBuilder). Called by OnCreateForCompiler. Not intended for manual use.

  • protected override void OnCreateForCompiler()
    Compiler helper that calls __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to initialize cached type handles used by jobs. Ensures the internal TypeHandle fields are set up for job scheduling.

  • private struct TypeHandle (nested, compiler-generated)

  • Fields: EntityTypeHandle __Unity_Entities_Entity_TypeHandle; ComponentTypeHandle __Game_Prefabs_DeliveryTruckData_RO_ComponentTypeHandle; ComponentTypeHandle __Game_Prefabs_CarTrailerData_RO_ComponentTypeHandle; ComponentTypeHandle __Game_Prefabs_CarTractorData_RO_ComponentTypeHandle.
  • Method: public void __AssignHandles(ref SystemState state) — sets the above handles using state.GetEntityTypeHandle() and state.GetComponentTypeHandle(isReadOnly: true). These cached handles are later converted for use with InternalCompilerInterface.Get... in the job scheduling.

Usage Example

// Example: reading the current delivery truck selection from a different system or UI code.
// Ensure this runs after systems have updated (e.g., in a later system's OnUpdate or via Game loop).

[Preserve]
protected override void OnUpdate()
{
    base.OnUpdate();

    var vehicleCapacitySystem = World.GetExistingSystemManaged<Game.Prefabs.VehicleCapacitySystem>();
    if (vehicleCapacitySystem != null)
    {
        // This method completes the internal job if needed and returns a managed snapshot.
        DeliveryTruckSelectData selectData = vehicleCapacitySystem.GetDeliveryTruckSelectData();

        // Use selectData for UI or selection logic...
    }
}

Notes and tips: - The system schedules work that writes into a persistent NativeList; always call GetDeliveryTruckSelectData (or otherwise complete m_WriteDependency) before accessing the list to avoid race conditions. - PostDeserialize is used to mark the system to rebuild its data after loading or starting a new game. - The update path uses asynchronous chunk enumeration (ToArchetypeChunkListAsync) and schedules a custom job (UpdateDeliveryTruckSelectJob). If you need to depend on this job in other systems, inspect base.Dependency or synchronize explicitly by completing the job handle.