Game.GarbageTransferDispatchSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
GarbageTransferDispatchSystem is an ECS system responsible for processing garbage transfer requests (both deliver and receive) in the simulation. It:
- Scans entities that have GarbageTransferRequest + UpdateFrame and handles them on configured update intervals.
- Validates garbage transfer targets (facilities) and schedules pathfinding to find an appropriate vehicle/source.
- Enqueues dispatch actions for vehicles or trip requests and updates facility request bookkeeping.
- Uses two Burst-compiled jobs:
- GarbageTransferDispatchJob (IJobChunk) — evaluates request entities, validates handlers/targets, starts pathfinding, and enqueues dispatch actions.
- DispatchActionJob (IJob) — dequeues dispatch actions, writes ServiceDispatch / TripNeeded buffers, and updates GarbageFacility state.
- Integrates with PathfindSetupSystem to request pathfinding and with EndFrameBarrier to produce EntityCommandBuffer changes at end of frame.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Used to create an EntityCommandBuffer to apply structural changes (add/remove components, destroy entities) safely at end of frame. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to read the global frame index to determine which requests to process this update. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to the PathfindSetupSystem used to enqueue pathfinding setup items for requests that need a vehicle source or route. -
private EntityQuery m_RequestQuery
Query used to find entities with GarbageTransferRequest and UpdateFrame components. This query is required for the system to update. -
private TypeHandle __TypeHandle
Internal container of all Entity/Component/Buffer handles and component lookups used when scheduling jobs. Populated in OnCreateForCompiler and used in OnUpdate.
(Also contains several nested/compiled job struct types: DispatchAction, GarbageTransferDispatchJob, DispatchActionJob, and the compiler-generated TypeHandle structure.)
Properties
- None (no public properties exposed by this system)
Constructors
public GarbageTransferDispatchSystem()
Default constructor. System initialization happens in OnCreate; constructor itself is empty/preserved for IL2CPP.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval used by the system. This system overrides it to return 16 (the system runs / checks on that interval in relation to simulation frame indexing). -
protected override void OnCreate()
Initializes the system by: - Getting references to EndFrameBarrier, SimulationSystem, and PathfindSetupSystem from the World.
-
Creating the EntityQuery for GarbageTransferRequest + UpdateFrame and marking it as required for update (RequireForUpdate). This method is annotated with [Preserve] so it is kept for AOT builds.
-
protected override void OnUpdate()
Main update routine: - Computes the current (and next) update frame indices from SimulationSystem.frameIndex to spread work across frames.
- Allocates a temporary NativeQueue
to collect dispatch operations from the parallel chunk job. - Configures and schedules GarbageTransferDispatchJob (IJobChunk, Burst) to:
- Validate request targets (facility exists and accepts the requested action).
- Tick/advance ServiceRequest cooldowns and reset failed requests.
- Enqueue pathfinding requests for finding vehicle sources, add PathInformation/PathElement buffers, or mark dispatch actions when a path origin is known.
- Configures and schedules DispatchActionJob (IJob, Burst) to:
- Dequeue DispatchAction entries and write ServiceDispatch or TripNeeded buffer entries to dispatch vehicle agents or create trip tasks.
- Update GarbageFacility components (m_GarbageDeliverRequest / m_GarbageReceiveRequest) when appropriate.
- Ensures proper job dependencies:
- The pathfind setup queue writer and command buffer producers are registered with PathfindSetupSystem and EndFrameBarrier.
- NativeQueue is disposed with dependency chaining.
-
Updates the system's base.Dependency to the final scheduled job handle.
-
private void __AssignQueries(ref SystemState state)
Compiler helper used in OnCreateForCompiler to initialize any entity queries used by generated code. (In this implementation it creates/ disposes a temporary EntityQueryBuilder; real query handles are assigned in OnCreate.) -
protected override void OnCreateForCompiler()
Compiler-time initialization hook that assigns internal query/state handles and calls __TypeHandle.__AssignHandles to prepare component type/lookup handles used when scheduling the jobs.
Notes on the nested job logic: - GarbageTransferDispatchJob (IJobChunk) behavior: - Works on chunks filtered by UpdateFrame shared index to process requests on staggered frames. - If chunk has no Dispatched and no PathInformation, it will ValidateTarget() and, if applicable, call FindVehicleSource() (which enqueues a SetupQueueItem into the PathfindSetupSystem queue and adds PathInformation/PathElement buffers). - If chunk has Dispatched component entries, it validates that the handler is still assigned; otherwise it resets cooldowns or resets failed request state. - If chunk has PathInformation and a path origin is found, DispatchVehicle() is called to create a DispatchAction and add a Dispatched component to the request entity; otherwise the request is reset as failed. - ValidateHandler() checks ServiceDispatch and TripNeeded buffers on a handler entity to ensure it is still working on the given request. - ResetFailedRequest() removes PathInformation / PathElement (and optionally Dispatched) and calls SimulationUtils.ResetFailedRequest to update ServiceRequest state. - FindVehicleSource() constructs PathfindParameters and SetupQueueTarget(s) and enqueues to the PathfindSetupSystem. It also adds PathInformation and PathElement buffer components to the request entity.
- DispatchActionJob (IJob) behavior:
- Consumes DispatchAction entries:
- If dispatch source is an in-city facility (not an outside connection) and has a ServiceDispatch buffer: append a ServiceDispatch pointing at the request.
- Else, if the dispatch source has a TripNeeded buffer: append a TripNeeded entry configured with Purpose (Collect/Exporting/ReturnGarbage) and resource amounts; if exporting it clamps to available Resources and deducts them.
- Updates GarbageFacility component fields (m_GarbageDeliverRequest / m_GarbageReceiveRequest) when deliver/receive facility entities are present.
Usage Example
// Example: create a garbage transfer request entity so the GarbageTransferDispatchSystem will process it.
// Note: this is simplified pseudocode — actual construction depends on your mod's entity/component context.
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
// create the request entity with required components
Entity request = em.CreateEntity(
typeof(GarbageTransferRequest),
typeof(ServiceRequest),
typeof(UpdateFrame)
);
// set up a request to deliver 100 garbage from a facility (facilityEntity)
em.SetComponentData(request, new GarbageTransferRequest {
m_Facility = facilityEntity,
m_Amount = 100,
m_Priority = 1,
m_Flags = GarbageTransferRequestFlags.Deliver | GarbageTransferRequestFlags.RequireTransport
});
// initialize service request state (cooldown etc.)
em.SetComponentData(request, new ServiceRequest {
m_Cooldown = 0
});
// assign UpdateFrame index so the request is picked on the appropriate frame slice.
// The system computes the target index as: (simulationSystem.frameIndex >> 4) & 7
// For testing you can set m_Index to the current target index value:
em.SetSharedComponentData(request, new UpdateFrame { m_Index = (byte)(((simulationSystem.frameIndex >> 4) & 7)) });
// The GarbageTransferDispatchSystem will validate the target, enqueue pathfinding
// via PathfindSetupSystem and eventually produce ServiceDispatch / TripNeeded entries
// to spawn/route vehicles that perform the transfer.
If you need more detail on any of the component types used here (GarbageTransferRequest, ServiceRequest, GarbageFacility, PathInformation, etc.) or an example demonstrating how to observe DispatchActionJob results at runtime, tell me which component you'd like documented next.