Game.Routes.RoutePathReadySystem
Assembly:
Namespace: Game.Routes
Type: class
Base: GameSystemBase
Summary:
RoutePathReadySystem watches for path updates and route data and ensures that PathTargets are marked "ready" with correct start/end positions. It runs a Burst-compiled IJobChunk (RoutePathReadyJob) over entities with route-related buffers and PathUpdated events. The job updates PathTargets components (m_ReadyStartPosition / m_ReadyEndPosition) from PathUpdated events or from route waypoint/segment buffers, and it manages pathfinding notification icons via an IconCommandSystem (adding or removing icons based on PathInformation distances). The system also supports an initial full-route pass when the game is loaded (m_Loaded flag).
Fields
-
private IconCommandSystem m_IconCommandSystem
Holds a reference to the IconCommandSystem used to create IconCommandBuffer instances for adding/removing UI icons (pathfind notifications) on entities/waypoints. -
private EntityQuery m_PathReadyQuery
Query that selects entities with a PathUpdated event to process incremental path readiness. -
private EntityQuery m_RouteQuery
Query that selects route entities (used when doing a full route initialization after load). -
private EntityQuery m_RouteConfigQuery
Query used to obtain the singleton RouteConfigurationData that contains configuration (e.g., icon id for pathfind notifications). -
private bool m_Loaded
Internal flag used to trigger a one-time full-route pass on the first OnUpdate after OnGameLoaded is called. GetLoaded() will return true once and clear this flag. -
private TypeHandle __TypeHandle
Container for component/buffer lookup/type handles used by the job and assigned in OnCreateForCompiler. This bundles all ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup instances required by RoutePathReadyJob.
Properties
- (none)
Constructors
public RoutePathReadySystem()
Default constructor. No public initialization beyond base class construction — the system sets up queries and references during OnCreate.
Methods
-
protected override void OnCreate()
Initializes the system: obtains the IconCommandSystem reference and creates the entity queries used by the system (m_PathReadyQuery, m_RouteQuery, m_RouteConfigQuery). Marked with [Preserve] in the original source. -
protected override void OnGameLoaded(Context serializationContext)
Called when the game/scene is loaded; sets m_Loaded = true so the next OnUpdate will perform a full-route pass. -
private bool GetLoaded()
Helper that returns true exactly once after OnGameLoaded was called (clears m_Loaded on first read). Used to switch query selection on the next update. -
protected override void OnUpdate()
Main update loop. Chooses between the incremental path-updated query (m_PathReadyQuery) and the full-route query (m_RouteQuery) depending on the loaded flag. If the selected query is not empty, constructs and schedules a RoutePathReadyJob (Burst compiled) with all required component/buffer lookups and the current RouteConfigurationData singleton. The job returns a JobHandle which is registered with the IconCommandSystem (AddCommandBufferWriter) and assigned to the system dependency. -
protected override void OnCreateForCompiler()
Internal setup method used by the generated/compiled code path to assign query & handle state for the job (calls __AssignQueries and __TypeHandle.__AssignHandles). Present to satisfy ECS code-generation requirements. -
private void __AssignQueries(ref SystemState state)
Compiler helper stub used by OnCreateForCompiler (no runtime behavior besides ensuring handles/queries are registered).
Inner types
- RoutePathReadyJob (private struct, [BurstCompile], implements IJobChunk)
- Purpose: The job runs per archetype chunk and either:
- Processes PathUpdated events: updates PathTargets for the owner entity with the ready start/end positions from the PathUpdated payload; if the owner has PathInformation and is not marked Temp, it triggers UpdatePathfindNotifications for that owner.
- Or (if no PathUpdated events present in the chunk) iterates route buffers (RouteWaypoint / RouteSegment) and for every RouteSegment with a PathTargets component updates the PathTargets m_ReadyStartPosition and m_ReadyEndPosition using Position components of the source/destination waypoints (when available).
- Notable public fields passed in by the system:
- ComponentTypeHandle
m_PathUpdatedType - BufferTypeHandle
m_RouteWaypointType - BufferTypeHandle
m_RouteSegmentType - ComponentLookup
m_OwnerData (read-only) - ComponentLookup
m_PositionData (read-only) - ComponentLookup
m_SegmentData (read-only) - ComponentLookup
m_PathInformationData (read-only) - ComponentLookup
m_TempData (read-only) - BufferLookup
m_RouteWaypoints (read-only) - BufferLookup
m_RouteSegments (read-only) - ComponentLookup
m_PathTargetsData (read-write) - RouteConfigurationData m_RouteConfigurationData (read-only, struct singleton)
- IconCommandBuffer m_IconCommandBuffer (writes icon commands)
- ComponentTypeHandle
- Key methods inside the job:
- Execute(in ArchetypeChunk chunk, ...)
Core per-chunk logic described above. When PathUpdated entries exist in the chunk, they are processed directly. Otherwise, buffers are iterated to update PathTargets for route segments. - UpdatePathfindNotifications(Entity entity)
When an owner entity has a Segment and an Owner component, uses the segment index to find adjacent waypoints and calls the per-waypoint notification updater for the two waypoint indexes involved around that segment. - UpdatePathfindNotification(DynamicBuffer
waypoints, DynamicBuffer segments, int waypointIndex)
Checks the two neighboring segments for that waypoint (previous and next) and inspects their PathInformation components; if either has a negative distance (m_Distance < 0) the job adds a pathfind notification icon (warning priority) for that waypoint via m_IconCommandBuffer.Add. If not, it removes the notification icon. The icon id comes from m_RouteConfigurationData.m_PathfindNotification.
- Execute(in ArchetypeChunk chunk, ...)
-
Implementation details: uses BufferAccessor to access buffer arrays per chunk. Uses ComponentLookup.TryGetComponent and HasComponent checks to avoid exceptions when components are missing. The job is Burst-compiled and performs native math via Unity.Mathematics.
-
TypeHandle (private struct)
Helper to collect and assign all component/buffer handles and lookups needed by the job. __AssignHandles fills the handles by calling state.GetComponentTypeHandle / GetBufferTypeHandle / GetComponentLookup / GetBufferLookup. Used in OnCreateForCompiler.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by the system:
m_IconCommandSystem = base.World.GetOrCreateSystemManaged<IconCommandSystem>();
m_PathReadyQuery = GetEntityQuery(ComponentType.ReadOnly<Event>(), ComponentType.ReadOnly<PathUpdated>());
m_RouteQuery = GetEntityQuery(ComponentType.ReadOnly<Route>());
m_RouteConfigQuery = GetEntityQuery(ComponentType.ReadOnly<RouteConfigurationData>());
}
Additional notes for modders: - This system is intended to run on ECS world update. If you need to interact with path-ready state, modify PathTargets components or PathUpdated events; the system will pick up PathUpdated events and update PathTargets accordingly. - Icon changes are buffered via IconCommandBuffer and applied by IconCommandSystem. If you add your own icon types, ensure they are compatible with the system's expectations (RouteConfigurationData.m_PathfindNotification is the icon id used). - The system relies on having RouteConfigurationData available as a singleton. If you remove or change that singleton, adapt the system accordingly. - The job uses many ComponentLookup/BufferLookup and is scheduled with JobChunkExtensions.Schedule. Be careful when adding new dependencies or writing components that could create race conditions; follow ECS best practices and update base.Dependency when scheduling additional jobs.