Game.ServiceRequestSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: public class
Base: GameSystemBase
Summary:
ServiceRequestSystem is an ECS system that coordinates service requests in the simulation. It runs two main pieces of work:
- UpdateRequestGroupJob: converts RequestGroup markers on entities into UpdateFrame shared components with a chosen index (randomized) and removes the RequestGroup marker so entities will be processed according to the chosen group frame.
- HandleRequestJob: aggregates HandleRequest components across chunks to decide what to do per ServiceRequest entity (destroy completed requests, attach/update Dispatched handlers, reset failed requests, and remove path data when consumed). All structural changes are performed through a ModificationEndBarrier command buffer so jobs remain thread-safe.
The system uses entity queries to detect when work is required and schedules Burst-compiled jobs for parallel work where possible. It also caches component/handle access in an internal TypeHandle struct.
Fields
-
private ModificationEndBarrier m_ModificationBarrier
This barrier/system is used to create command buffers for safe structural changes (add/remove components, destroy entities) from jobs. The system adds job handles to this barrier so the barrier knows when producer jobs complete. -
private EntityQuery m_RequestGroupQuery
EntityQuery that matches entities which have RequestGroup components. Used to schedule UpdateRequestGroupJob. -
private EntityQuery m_HandleRequestQuery
EntityQuery that matches entities which have HandleRequest components. Used to gather archetype chunks and schedule HandleRequestJob. -
private TypeHandle __TypeHandle
Internal struct instance that caches EntityTypeHandle, ComponentTypeHandles (RequestGroup, HandleRequest) and ComponentLookup instances (Dispatched, ServiceRequest). __TypeHandle.__AssignHandles(ref state) is called to populate these handles from the SystemState.
Properties
None
There are no public properties exposed by this system. Internally the system uses the TypeHandle struct and component lookups to access component data in jobs.
Constructors
public ServiceRequestSystem()
Default constructor. Marked with [Preserve] attribute in the source; no custom initialization is performed beyond base construction. Real initialization occurs in OnCreate.
Methods
-
protected override void OnCreate()
Initializes the system: obtains/creates the ModificationEndBarrier, constructs the entity queries for RequestGroup and HandleRequest, and calls RequireAnyForUpdate so the system updates only when at least one of those queries has matching entities. -
protected override void OnUpdate()
Main system update. Behavior: - If m_RequestGroupQuery is not empty, schedules UpdateRequestGroupJob as a parallel IJobChunk. This job:
- Reads RequestGroup components and randomly selects an index per RequestGroup (using RandomSeed.Next()).
- Removes the RequestGroup component from the entity and adds a shared UpdateFrame(index) component so that the entity will be considered for that update frame.
- If m_HandleRequestQuery is not empty, collects the matching archetype chunks asynchronously and schedules HandleRequestJob. This job:
- Builds a NativeParallelHashMap keyed by the request entity to consolidate multiple HandleRequest entries (keeps Completed if any completed, and records PathConsumed).
- Iterates the consolidated entries:
- Skips if the ServiceRequest component no longer exists on the request entity.
- If Completed: destroys the request entity.
- Else if a handler entity is assigned: ensures a Dispatched component is added/updated on the request; resets ServiceRequest.m_Cooldown to 0; removes PathInformation/PathElement if the path was consumed.
- Else (no handler) but the request was previously dispatched: resets the ServiceRequest to a failed state (via SimulationUtils.ResetFailedRequest), removes path components and Dispatched component.
- Uses the system's command buffer for structural changes and may write to ComponentLookup data for in-place updates when safe.
-
Both scheduled jobs' handles are registered with the ModificationEndBarrier to coordinate subsequent structural changes.
-
private void __AssignQueries(ref SystemState state)
Compiler/boilerplate helper invoked in OnCreateForCompiler to ensure queries are assigned/validated. In the decompiled source this contains a no-op EntityQueryBuilder call; it exists for compiler-generated scaffolding. -
protected override void OnCreateForCompiler()
Compiler-invoked setup: calls __AssignQueries and __TypeHandle.__AssignHandles(ref state) to initialize cached type handles and lookups for job scheduling. -
(Nested)
UpdateRequestGroupJob : IJobChunk
Burst-compiled job that: - Reads RequestGroup and Entity data per chunk.
- Uses a RandomSeed to select a group index for each RequestGroup entity.
-
Removes RequestGroup and adds a shared UpdateFrame(index) via an EntityCommandBuffer.ParallelWriter.
-
(Nested)
HandleRequestJob : IJob
Burst-compiled job that: - Accepts a list of archetype chunks containing HandleRequest components.
- Consolidates multiple HandleRequest entries per request entity into a single decision using a NativeParallelHashMap.
- Uses ComponentLookup to read/write ServiceRequest and Dispatched where possible, and an EntityCommandBuffer for structural changes (destroy, add/remove components).
-
Removes path-related components when paths are consumed or when a request fails, and resets or destroys requests according to their state.
-
(Nested)
TypeHandle
Holds cached handles: - EntityTypeHandle
- ComponentTypeHandle
(ReadOnly) - ComponentTypeHandle
(ReadOnly) - ComponentLookup
(Read/Write) - ComponentLookup
(Read/Write) Provides __AssignHandles(ref SystemState state) to populate these handles from a SystemState.
Usage Example
// Example: creating a RequestGroup entity so ServiceRequestSystem will schedule it
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype archetype = em.CreateArchetype(typeof(RequestGroup));
Entity e = em.CreateEntity(archetype);
em.SetComponentData(e, new RequestGroup { m_GroupCount = 4 });
// The ServiceRequestSystem will pick a random index [0..3] and replace RequestGroup with a shared UpdateFrame(index)
// when the system updates. HandleRequest flows are driven by HandleRequest and ServiceRequest components added by other systems.
Additional notes for modders: - Key component types referenced by the system: RequestGroup, HandleRequest, ServiceRequest, Dispatched, PathInformation, PathElement, UpdateFrame. Familiarize yourself with these components if you want to influence request dispatching or lifecycle. - Structural changes are deferred via ModificationEndBarrier command buffers — do not attempt to perform structural changes directly from jobs without a proper command buffer. - The system uses RandomSeed.Next() for per-chunk randomness; results are non-deterministic unless you control the RandomSeed source.