Game.Tools.GenerateRoutesSystem
Assembly: Assembly-CSharp
Namespace: Game.Tools
Type: class
Base: GameSystemBase
Summary:
GenerateRoutesSystem is an ECS system used during route creation for the game. It listens for temporary CreationDefinition + WaypointDefinition entities and spawns Route entities built from waypoint and segment sub-elements. The system collects sub-element archetype chunks (waypoints/segments) asynchronously, then schedules a Burst-compiled IJobChunk (CreateRoutesJob) to create route entities in parallel using an EntityCommandBuffer. It sets up route buffers (RouteWaypoint, RouteSegment), route flags (Complete), Temp/Owner/PrefabRef/Color components, and copies TransportLine data when available. The system uses a ModificationBarrier2 to produce structural changes safely from jobs and relies on read-only ComponentLookup/ComponentTypeHandles for thread-safe reading.
Fields
-
private ModificationBarrier2 m_ModificationBarrier
Holds the modification barrier system used to create an EntityCommandBuffer (parallel writer) for making structural and component changes from jobs. The system registers the job handle with this barrier so structural changes are applied safely. -
private EntityQuery m_DefinitionQuery
Query selecting CreationDefinition + WaypointDefinition + Updated. This query is required for the system to run (RequireForUpdate). It provides the chunks passed to the CreateRoutesJob. -
private EntityQuery m_SubElementQuery
Query selecting temporary sub-elements (waypoints/segments) with Temp + Updated (and excluding Deleted). This query is converted to a NativeListasynchronously and passed into the job to allow mapping from sub-element index to entity. -
private TypeHandle __TypeHandle
Container for the various Entity/Component/Buffer type handles and ComponentLookup instances used to access component data inside jobs. It has a helper method to assign handles from a SystemState. -
private struct CreateRoutesJob
(nested)
Burst-compiled IJobChunk implementation that performs the bulk of route creation for each chunk of CreationDefinition entities: it reads CreationDefinition, optional ColorDefinition, waypoint definitions buffer, queries ComponentLookup data for prefabs/colors/route data, creates Route entities via the parallel EntityCommandBuffer, fills RouteWaypoint and RouteSegment buffers by mapping indices to sub-element Entity references, assigns components (Route, Temp, PrefabRef, Color, Owner, TransportLine), and determines route flags (Complete/Create/Delete/Select/Modify). -
private struct TypeHandle
(nested)
Stores read-only handles and lookups for Entity, CreationDefinition, Waypoint, Segment, WaypointDefinition buffer, ColorDefinition, and ComponentLookup instances for Color, PrefabRef, RouteData, and TransportLineData. Provides __AssignHandles to initialize them from SystemState.
Properties
- None.
Constructors
public GenerateRoutesSystem()
Default constructor. The system uses [Preserve] on lifecycle methods to ensure the methods are kept by code stripping; the constructor itself is empty.
Methods
protected override void OnCreate()
Initializes the system:- Calls base.OnCreate().
- Retrieves/creates the ModificationBarrier2 system used for safe structural changes.
- Creates m_DefinitionQuery for CreationDefinition + WaypointDefinition + Updated.
- Creates m_SubElementQuery selecting entities with Temp + Updated and either Waypoint or Segment, excluding Deleted.
-
Calls RequireForUpdate(m_DefinitionQuery) so the system only runs when there are route definitions to process.
-
protected override void OnUpdate()
Main update that schedules route creation: - Asynchronously converts m_SubElementQuery to a NativeList
(ToArchetypeChunkListAsync) and obtains an output JobHandle. - Schedules the Burst-compiled CreateRoutesJob in parallel using JobChunkExtensions.ScheduleParallel, passing:
- The subElement chunk list,
- Entity/Component/Buffer type handles and ComponentLookup instances obtained via InternalCompilerInterface.Get... using the stored __TypeHandle,
- A parallel EntityCommandBuffer from m_ModificationBarrier.
- Disposes the subElementChunks list with dependency on the scheduled job.
-
Registers the scheduled job with the modification barrier (AddJobHandleForProducer) and updates base.Dependency to the job handle.
-
protected override void OnCreateForCompiler()
Called by generated/compiled code paths; ensures queries and type handles are assigned for compiler-generated plumbing: -
Calls __AssignQueries and __TypeHandle.__AssignHandles with the system state.
-
private void __AssignQueries(ref SystemState state)
Helper invoked from OnCreateForCompiler to initialize any query builder plumbing required by generated code. In this implementation it performs a no-op EntityQueryBuilder creation+dispose (generated placeholder). -
private struct CreateRoutesJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Per-chunk work that: - Reads CreationDefinition array, optional ColorDefinition array, and WaypointDefinition buffer accessor from the chunk.
- Iterates each CreationDefinition in the chunk:
- Determines whether the route was created from an original route (m_Original != Entity.Null) or a new creation and sets Route/Temp flags accordingly.
- If original exists, marks the original as Hidden, copies prefab and color from the original prefab entity, and fetches routeData using PrefabRef lookup.
- Creates a new Route entity using routeData.m_RouteArchetype via the parallel command buffer.
- Allocates DynamicBuffers RouteWaypoint and RouteSegment on the newly created entity sized to the number of waypoint definitions (or one less if the route is closed and the last waypoint equals the first).
- Calls FindSubElements to fill the buffers by mapping sub-element indices to their Entity references.
- Adds components to the created route entity: Owner (if present), Route (flags), Temp, PrefabRef, Color, and TransportLine (if prefab has TransportLineData).
-
Uses CollectionUtils.ResizeInitialized to size dynamic buffers.
-
private void CreateRoutesJob.FindSubElements(DynamicBuffer<RouteWaypoint> routeWaypoints, DynamicBuffer<RouteSegment> routeSegments)
Given the NativeList of subElement chunks on the job, iterates each chunk and: - Reads Entity array, Waypoint array, and Segment array from the archetype chunk.
-
For each Waypoint/Segment, writes the corresponding RouteWaypoint/RouteSegment entry at the index stored in waypoint/segment components. This maps indices used in WaypointDefinition into actual sub-element Entity references.
-
void CreateRoutesJob.IJobChunk.Execute(...)
Explicit interface implementation that forwards to the typed Execute method above. -
public void TypeHandle.__AssignHandles(ref SystemState state)
Initializes the stored EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle instances and ComponentLookup instances using the provided SystemState. These are then used in OnUpdate to obtain job-safe handles via InternalCompilerInterface.Get... calls.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Retrieve the modification barrier used to create EntityCommandBuffer writers for jobs
m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier2>();
// Create queries (example mirrors how the system does it)
m_DefinitionQuery = GetEntityQuery(ComponentType.ReadOnly<CreationDefinition>(),
ComponentType.ReadOnly<WaypointDefinition>(),
ComponentType.ReadOnly<Updated>());
m_SubElementQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { ComponentType.ReadOnly<Temp>(), ComponentType.ReadOnly<Updated>() },
Any = new ComponentType[] { ComponentType.ReadOnly<Waypoint>(), ComponentType.ReadOnly<Segment>() },
None = new ComponentType[] { ComponentType.ReadOnly<Deleted>() }
});
RequireForUpdate(m_DefinitionQuery);
}
Notes and remarks: - The heavy work is done inside a Burst-compiled IJobChunk (CreateRoutesJob) and uses an EntityCommandBuffer.ParallelWriter for thread-safe structural changes. - The system expects sub-elements (waypoints/segments) to provide index fields that map into RouteWaypoint/RouteSegment buffers; FindSubElements resolves index -> Entity references. - The job reads many components via read-only ComponentTypeHandle/BufferTypeHandle/ComponentLookup; any change to those component types should be reflected in the TypeHandle assignments. - Because this system creates entities and performs structural changes from a job, it uses ModificationBarrier2 and properly registers the job handle to ensure correct ordering.