Skip to content

Game.Simulation.GarbageCollectorDispatchSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: public class

Base: GameSystemBase

Summary:
Manages dispatching of garbage collection vehicles for the simulation. The system: - Processes GarbageCollectionRequest entities, validating targets and handling reversed requests. - Schedules pathfinding (via PathfindSetupSystem) for requests that need a path to a vehicle or to a garbage producer. - Enqueues vehicle dispatches that are consumed by a follow-up job to add ServiceDispatch buffers or update ServiceRequest flags. - Runs two Burst-compiled jobs: GarbageDispatchJob (IJobChunk) which contains most logic and state validation, and DispatchVehiclesJob (IJob) which processes the vehicle dispatch queue. - Uses an EndFrameBarrier command buffer to apply structural changes at end of frame, and cooperates with SimulationSystem and PathfindSetupSystem. - Runs every 16 simulation frames (GetUpdateInterval returns 16).


Fields

  • private EndFrameBarrier m_EndFrameBarrier
    Handles creation of command buffers and job producer handles used to play structural changes back to the entity manager at end of the frame.

  • private SimulationSystem m_SimulationSystem
    Reference to the main simulation system (used to get the current frame index).

  • private PathfindSetupSystem m_PathfindSetupSystem
    Reference to the pathfinding setup system used to enqueue pathfinding requests from garbage collection requests.

  • private EntityQuery m_RequestQuery
    Query selecting active GarbageCollectionRequest entities (and UpdateFrame shared component) that this system processes.

  • private EntityQuery m_GarbageSettingsQuery
    Query used to obtain GarbageParameterData singleton (garbage dispatch parameters/thresholds).

  • private TypeHandle __TypeHandle
    Internal struct holding Entity/Component/Buffer/SharedComponent handles that are assigned in OnCreateForCompiler to be used by jobs.

  • (Nested types) GarbageDispatchJob, DispatchVehiclesJob, TypeHandle, and VehicleDispatch struct declarations (private, used inside the system).
    GarbageDispatchJob is the primary IJobChunk doing validation, path setup and enqueueing dispatches. DispatchVehiclesJob drains a queue of VehicleDispatch entries and updates ServiceDispatch buffers or ServiceRequest flags.

Properties

  • None (no public properties declared on the system).

Constructors

  • public GarbageCollectorDispatchSystem()
    Default parameterless constructor. The system does its initialization in OnCreate (getting references to other systems and setting up entity queries).

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 16 — the system is scheduled every 16 simulation frames. Use this to understand how often garbage requests are processed.

  • protected override void OnCreate()
    Initializes the system:

  • Obtains EndFrameBarrier, SimulationSystem and PathfindSetupSystem via World.GetOrCreateSystemManaged.
  • Creates entity queries for GarbageCollectionRequest and for garbage parameter data.
  • Calls RequireForUpdate(m_RequestQuery) so the system only runs when there are requests.

  • protected override void OnUpdate()
    Main scheduling method:

  • Computes the update frame indices used by the shared UpdateFrame component to decide which requests to process now vs later.
  • Allocates a NativeQueue and constructs two Burst jobs:
    • GarbageDispatchJob (IJobChunk): performs validation and either schedules pathfinding, enqueues vehicle dispatches, resets failed requests, or marks requests dispatched. Uses many ComponentLookup/BufferLookup handles to read/write entity data safely.
    • DispatchVehiclesJob (IJob): consumes the VehicleDispatch queue and either appends ServiceDispatch entries to the vehicle/facility buffers or flips SkipCooldown on ServiceRequest.
  • Schedules the jobs in sequence (GarbageDispatchJob parallel chunk job, then DispatchVehiclesJob), wires up dependencies, adds the pathfinding queue writer and registers the producer jobhandle with the EndFrameBarrier.

  • protected override void OnCreateForCompiler()
    Internal helper called by the generated/compiled code path. Assigns component/handle references via __AssignQueries() and __TypeHandle.__AssignHandles() so that job-type handles are available.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper; in this generated code it allocates and disposes an EntityQueryBuilder. (Used by the codegen/OnCreateForCompiler pattern.)

  • (Nested: GarbageDispatchJob methods) Several private helper methods inside the job implement the per-request logic:

  • ValidateReversed(Entity entity, Entity source): Validates reversed requests that target a source (facility or truck); ensures the source has capacity and registers the request on the source.
  • ValidateHandler(Entity entity, Entity handler): Checks whether the handler (a dispatched vehicle/facility) still has the request in its ServiceDispatch buffer.
  • ValidateTarget(Entity entity, Entity target): Validates the garbage producer target (garbage amount above threshold, registers the request on the producer).
  • ResetReverseRequest(...): Enqueues a VehicleDispatch for reversed requests that found a valid destination path, resets service request flags and removes PathInformation.
  • ResetFailedRequest(...): Resets service request cooldown and increments dispatch index on the request; removes PathInformation/PathElement and Dispatched component if applicable.
  • DispatchVehicle(...): Creates a VehicleDispatch for an origin (resolving parked cars via Owner) and adds the Dispatched component to the request.
  • FindVehicleSource(...): Enqueues pathfinding from a garbage collector (origin = GarbageCollector) to a target producer (destination = CurrentLocation).
  • FindVehicleTarget(...): Enqueues pathfinding from a vehicle's current location to the GarbageCollectorRequest target.

  • (Nested: DispatchVehiclesJob.Execute)
    Dequeues VehicleDispatch entries and:

  • If the source has a ServiceDispatch buffer, appends a ServiceDispatch entry pointing to the request.
  • Otherwise, if the source has a ServiceRequest component, sets the SkipCooldown flag so the source will be reconsidered immediately.

Usage Example

// Retrieve the system from the default world (systems are created/managed by the world)
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var garbageDispatchSystem = world.GetExistingSystemManaged<Game.Simulation.GarbageCollectorDispatchSystem>();

// The system automatically runs on its schedule (every 16 sim frames).
// You can inspect its update interval:
int interval = garbageDispatchSystem.GetUpdateInterval(Unity.Entities.SystemUpdatePhase.Simulation);

// Typical mod usage is to create GarbageCollectionRequest entities so this system picks them up,
// or to watch/modify GarbageParameterData singleton to change thresholds used during dispatch.

Notes and tips for modders: - The main logic is implemented inside a Burst-compiled IJobChunk (GarbageDispatchJob). Changes to component layouts or to the relationships between GarbageProducer, GarbageFacility, GarbageTruck, ServiceRequest and ServiceDispatch should be done carefully, because the job accesses many ComponentLookup/BufferLookup types. - Pathfinding is enqueued via PathfindSetupSystem; the system adds PathInformation and PathElement buffers to requests when it submits a path setup item. Those path results are read back by the job in later updates to perform DispatchVehicle / ResetReverseRequest logic. - The system assumes GarbageParameterData is available as a singleton; altering that structure requires ensuring m_GarbageSettingsQuery still returns the correct data. - Because this system uses EndFrameBarrier command buffers and schedules parallel jobs, structural changes are batched and applied at frame barrier; debugging immediate structural changes may require syncing or inspecting produced commands at end of frame.