Game.Simulation.DeliveryTruckAISystem
Assembly:
Game (assembly inferred from namespace and typical project layout)
Namespace:
Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
DeliveryTruckAISystem is the ECS system that updates delivery truck vehicles. It handles per-frame delivery-truck logic including:
- ticking delivery truck entities (DeliveryTruckTickJob),
- handling completed deliveries and money/resource updates (DeliverJob),
- enqueuing pathfind requests for trucks,
- removing guest vehicles and canceling transactions when needed,
- interacting with ResourceSystem, CityStatisticsSystem, PathfindSetupSystem and using an EndFrameBarrier to coordinate jobs.
The system uses NativeQueues to pass "delivered" events and guest-vehicle removal requests between the parallel chunk job and the single-threaded deliver job.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Reference to EndFrameBarrier used to create command buffers and coordinate end-of-frame job dependencies. -
private SimulationSystem m_SimulationSystem
Reference to SimulationSystem (used for frameIndex and schedule filtering). -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to PathfindSetupSystem used to enqueue pathfinding setup requests produced by vehicles that need paths. -
private Actions m_Actions
Managed helper (nested) that runs the DeliverJob; holds queues and job dependency info. -
private EntityQuery m_DeliveryTruckQuery
EntityQuery used to select delivery truck entities to process (DeliveryTruck component + CarCurrentLane + UpdateFrame etc). -
private TypeHandle __TypeHandle
Internal generated type handle grouping used to get component/lookup handles for chunk job scheduling and execution.
Properties
- (none)
Constructors
public DeliveryTruckAISystem()
Default constructor. System is created/registered by the DOTS world; marked with [Preserve] in the source to avoid stripping. No custom construction logic beyond the base GameSystemBase.
Methods
-
protected override void OnCreate()
Initializes references to other systems (EndFrameBarrier, SimulationSystem, PathfindSetupSystem, Actions), and creates the EntityQuery m_DeliveryTruckQuery used to select which delivery trucks to update. -
protected override void OnUpdate()
Main update: - Picks the UpdateFrame index (frameIndex % 16) and filters the delivery truck query to only process the chosen subset of vehicles.
- Creates two NativeQueue instances (m_DeliveredQueue and m_RemoveGuestVehicleQueue) for inter-job communication.
- Schedules DeliveryTruckTickJob (IJobChunk) in parallel to tick delivery trucks, produce pathfind requests and enqueue delivered/remove events.
- Registers the job with PathfindSetupSystem and EndFrameBarrier; stores the job handle on the Actions helper so the DeliverJob runs after the tick job completes.
-
The nested Actions.OnUpdate schedules the DeliverJob which dequeues the delivered events and performs resource/money updates and statistic events.
-
protected override void OnCreateForCompiler()
Internal setup used by code generation (assigns queries and handles). -
private void __AssignQueries(ref SystemState state)
Generated helper that sets up any additional EntityQueryBuilder usage required by compiled code (no custom queries in source beyond the explicit m_DeliveryTruckQuery).
Nested types of primary interest (summarized here; see source for full details): - DeliveredStack (struct): data representing a completed delivery or return (vehicle, target, location, resource, amount, costPayer, distance, flags for storageTransfer and moneyRefund). Enqueued by the tick job and consumed by the deliver job. - RemoveGuestVehicle (struct): simple pair (vehicle, target) for removing guest vehicles from building buffers and deleting the vehicle entity. - Actions (nested class): a managed helper system that schedules and runs the DeliverJob. It holds references to ResourceSystem and CityStatisticsSystem and performs the single-threaded finalization work. - DeliverJob (IJob, BurstCompile): dequeues DeliveredStack items and applies resource addition/removal, money transactions, updates storage transfer requests, and enqueues city statistics events. Runs after the chunk job finishes. - DeliveryTruckTickJob (IJobChunk, BurstCompile): per-chunk logic that updates vehicle state, handles pathfind decisions, delivers cargo on path end, cancels transactions when path fails/stuck, toggles flags on DeliveryTruck components, enqueues Deliveries and RemoveGuestVehicle entries, and enqueues pathfind setup requests.
Usage Example
// Typical way to get this system from a World and ensure it runs in the DOTS update loop:
var deliverySystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<Game.Simulation.DeliveryTruckAISystem>();
// The system itself schedules jobs internally in OnUpdate and interacts with other systems
// (ResourceSystem, PathfindSetupSystem, CityStatisticsSystem). Modders typically won't call
// its internal methods directly; instead they can interact with the data the system reads/writes:
// - Modify DeliveryTruck components on entities to change behavior.
// - Add/modify StorageTransferRequest buffers on storage buildings.
// - Observe scheduled pathfind requests via PathfindSetupSystem.
Notes and modding tips: - The system splits work into a parallel IJobChunk (DeliveryTruckTickJob) and a single-threaded IJob (DeliverJob) communicating via NativeQueue. When changing data layout or adding bookkeeping, ensure thread-safety and use the provided ComponentLookup/BufferLookup patterns. - Money/resource accounting is performed in the DeliverJob and relies on ResourcePrefabs and ResourceData lookup. If adding new resource types or changing cost logic, update EconomyUtils usage accordingly. - Pathfind requests are enqueued through PathfindSetupSystem. If you need to inject custom pathfind weights/methods for delivery trucks, consider extending the FindPathIfNeeded setup parameters in DeliveryTruckTickJob. - The system honors UpdateFrame filtering to spread processing across frames — be careful when querying delivery trucks directly so you don't assume all trucks are processed each frame.