Game.Tools.GenerateWaypointsSystem
Assembly: Game (Assembly-CSharp)
Namespace: Game.Tools
Type: public class
Base: GameSystemBase
Summary:
GenerateWaypointsSystem is an ECS system responsible for creating waypoint and segment entities for route prefabs and route instances. It processes CreationDefinition entities that include WaypointDefinition buffers and either creates new waypoint/segment entities or reuses and updates existing segments when possible. Work is performed inside a Burst-compiled IJob (CreateWaypointsJob) and modifications are applied via a ModificationBarrier1 command buffer to avoid structural changes during job execution. The system also reconciles deleted segments (to reuse them) and copies path metadata (PathInformation, PathTargets, PathElement buffers) from original segments when applicable.
Fields
-
private ModificationBarrier1 m_ModificationBarrier
Holds a reference to the modification barrier system used to create an EntityCommandBuffer for safely applying structural changes produced by the job. -
private EntityQuery m_DefinitionQuery
Query selecting CreationDefinition entities that contain WaypointDefinition buffers and are marked Updated — these definitions drive waypoint/segment creation. -
private EntityQuery m_DeletedQuery
Query selecting deleted Segment entities along with Owner, Temp and PrefabRef components — used to gather segments that can be reused. -
private TypeHandle __TypeHandle
Generated helper struct containing Entity/Component/Buffer type & lookup handles used when scheduling the job. It maps component access to the SystemState.
Properties
- (none public)
This system exposes no public properties.
Constructors
public GenerateWaypointsSystem()
Default constructor (preserved). Typical Unity ECS system construction; initialization is done in OnCreate.
Methods
-
[Preserve] protected override void OnCreate()
Initializes the system: gets the ModificationBarrier1, constructs the two EntityQueries (m_DefinitionQuery and m_DeletedQuery) and calls RequireForUpdate(m_DefinitionQuery) so the system only runs when there are definition entities to process. -
[Preserve] protected override void OnUpdate()
Main update: collects archetype chunk lists for the definition and deleted queries (async), prepares and schedules the Burst IJob CreateWaypointsJob with all required type handles, component lookups, buffer lookups and a command buffer from the modification barrier. Disposes temporary lists after scheduling and registers the job handle with the modification barrier. -
protected override void OnCreateForCompiler()
Compiler-time helper used by generated code to assign query handles and type handles for the compiler environment. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Generated helper that can be used to assign/initialize queries for the compiler; in this implementation it contains a placeholder EntityQueryBuilder call.
Nested types (not exhaustive signatures):
private struct CreateWaypointsJob : IJob
Burst-compiled job that performs the heavy lifting. It:- Scans deleted segment chunks and fills a NativeParallelHashMap of old segments for potential reuse.
- Iterates definition chunks to create or update waypoint and segment entities.
- Reuses path metadata (PathInformation, PathTargets, PathElement buffers) from original segments when merging or re-creating segments.
- Uses EntityCommandBuffer (m_CommandBuffer) to perform all structural changes after job completes.
Important methods inside CreateWaypointsJob:
- public void Execute()
— main entry: builds oldSegments map, processes definitions, disposes map.
- private void FillOldSegments(ArchetypeChunk chunk, NativeParallelHashMap<SegmentKey, SegmentValue> oldSegments)
— collects existing segment entities (from deleted query chunks), maps them by prefab + original route and endpoint positions for reuse.
- private void FillOriginalSegments(Entity route, NativeParallelHashMap<SegmentKey, SegmentValue> originalSegments)
— gathers segments from an original route (CreationDefinition.m_Original) to match segments by positions when cloning from an existing route.
- private void CreateWaypointsAndSegments(ArchetypeChunk chunk, NativeParallelHashMap<SegmentKey, SegmentValue> oldSegments)
— for each CreationDefinition, creates waypoint entities and corresponding segment entities (or reuses/merges old ones).
- private Entity GetOriginalSegment(... )
— attempts to match a start/end pair to an original segment and removes matched entries from the originalSegments map.
- private void CreateWaypoint(RouteData prefabRouteData, Entity prefab, TempFlags tempFlags, WaypointDefinition definition, int index)
— creates a waypoint entity, either a connected waypoint or a regular waypoint, sets index, position, prefab ref and Temp component via the command buffer.
- private void CreateSegment(NativeParallelHashMap<SegmentKey, SegmentValue> oldSegments, RouteData prefabRouteData, Entity prefab, Entity original, Entity originalRoute, TempFlags tempFlags, WaypointDefinition startDefinition, WaypointDefinition endDefinition, int index)
— creates a new segment entity or reuses/merges existing ones; handles copying path-related components and buffers and updates Temp/Segment components. Merging attempts are made when two adjacent old segments can be combined into a single new segment; path data is combined using PathUtils helpers.
private struct TypeHandle
Generated container of component lookups & type handles. Contains __AssignHandles(ref SystemState state) which initializes all required handles used by the job (EntityTypeHandle, ComponentTypeHandles, BufferTypeHandles, ComponentLookup and BufferLookup entries).
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire the modification barrier used to apply entity/structural changes
m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier1>();
// Query for definitions that drive waypoint/segment creation
m_DefinitionQuery = GetEntityQuery(
ComponentType.ReadOnly<CreationDefinition>(),
ComponentType.ReadOnly<WaypointDefinition>(),
ComponentType.ReadOnly<Updated>());
// Query for deleted segments (potentially reusable)
m_DeletedQuery = GetEntityQuery(
ComponentType.ReadOnly<Deleted>(),
ComponentType.ReadOnly<Segment>(),
ComponentType.ReadOnly<Owner>(),
ComponentType.ReadOnly<Temp>(),
ComponentType.ReadOnly<PrefabRef>());
RequireForUpdate(m_DefinitionQuery);
}
Notes and tips: - The system expects CreationDefinition entities with WaypointDefinition buffers describing waypoints to create. If CreationDefinition.m_Original is set, it will try to reuse and copy data from the original route segments. - The system performs structural changes via a command buffer obtained from ModificationBarrier1; modifications are deferred and applied safely after the Burst job completes. - Path-related data (PathInformation, PathTargets, PathElement buffers) is preserved/combined where possible to avoid recomputing expensive path data. - Because CreateWaypointsJob is Burst-compiled and uses NativeParallelHashMap, ensure that any custom components used in conjunction with this system are compatible with Burst and the job's read-only access patterns.