Game.Routes.RoutePathSystem
Assembly:
Unknown (game assembly)
Namespace:
Game.Routes
Type:
class (managed ECS System)
Base:
GameSystemBase, IDefaultSerializable, ISerializable
Summary:
RoutePathSystem is responsible for keeping route segment pathfinds up to date for routed prefabs (routes). It watches for changed/removed lanes, applied lanes (new lane types), and updated segments. When it detects relevant changes it schedules Burst jobs to quickly find which route segments need recomputing and then enqueues pathfinding actions on the PathfindQueueSystem. It also maintains a persistent lazy update set (m_LazyUpdateSet) of segments that should have a path recomputed eventually (used when immediate update isn't feasible). The system implements serialization to persist the lazy update set across saves/loads.
Fields
-
private PathfindQueueSystem m_PathfindQueueSystem
Reference to the PathfindQueueSystem used to enqueue pathfinding work. -
private EntityQuery m_UpdatedSegmentQuery
Query for segments that have the Updated tag and a PathTargets component; used to detect segments whose path targets changed and need immediate recompute. -
private EntityQuery m_DeletedLaneQuery
Query for entities marked Deleted and that are lanes (excluding Temp). Used to detect when a lane referenced by a segment was deleted. -
private EntityQuery m_AppliedLaneQuery
Query for newly applied lanes (Applied and Lane). Used to detect new lane types introduced by prefabs which may change which segments need path recomputation. -
private EntityQuery m_SegmentQuery
Query for segments that have PathElement buffer and are not Deleted. Used broadly for per-segment examinations by jobs. -
private NativeParallelHashSet<Entity> m_LazyUpdateSet
Persistent hashset of segment Entities that should be lazily updated (recomputed later). This set is serialized/deserialized by the system and is allocated with Allocator.Persistent. -
private TypeHandle __TypeHandle
Container of ECS type handles used by this system (auto-generated holder used to get ComponentTypeHandle/BufferTypeHandle/ComponentLookup and set them up in OnCreateForCompiler).
Properties
- None (no public properties exposed by this system)
Constructors
public RoutePathSystem()
Default constructor. The system is managed by the World; initialization is mainly done in OnCreate.
Methods
protected override void OnCreate()
: System.Void
Initializes queries, obtains PathfindQueueSystem, and allocates the m_LazyUpdateSet (NativeParallelHashSet). Sets up EntityQuery instances:- m_UpdatedSegmentQuery: Updated + Segment + PathTargets
- m_DeletedLaneQuery: Deleted + Lane (exclude Temp)
- m_AppliedLaneQuery: Applied + Lane
-
m_SegmentQuery: Segment + PathElement (exclude Deleted) This method prepares the system to start scheduling jobs in OnUpdate.
-
protected override void OnDestroy()
: System.Void
Disposes of m_LazyUpdateSet and calls base.OnDestroy(). -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
: System.Void
Serializes the m_LazyUpdateSet to the provided writer. Writes the count then each Entity (skips Entity.Null when deserializing). Used by the game's save/load pipeline. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
: System.Void
Deserializes the m_LazyUpdateSet from a reader previously written by Serialize. Clears the set and re-adds entities read from the stream (skips Entity.Null entries). -
public void SetDefaults(Context context)
: System.Void
Resets state to defaults on creation/when requested by serialization system — here it clears the lazy update set. -
protected override void OnUpdate()
: System.Void
Main system update; core behavior: - Determines whether there are deletions, applied lanes, or updated segments to process and whether there are lazy updates pending.
- Schedules Burst jobs to find route segments that need updates:
- CheckRoutePathsJob: scans PathElement buffers and enqueues segments referencing deleted targets.
- CheckAppliedLanesJob: inspects applied lanes/prefabs to collect RoutePathType entries that are then used to find segments whose prefab RouteConnectionData matches and thus need updates (CheckSegmentRoutes).
- CheckSegmentRoutes: checks segment owners' prefab route connection data against lane/path types and enqueues segments that should be updated.
- Handles Updated segments directly by iterating matching archetype chunks, verifying lane endpoints and curve positions, and calling SetupPathfind for segments that changed.
- Drains job-produced NativeQueues to call SetupPathfind on affected segments.
- If some segments cannot be immediately updated, they are added to m_LazyUpdateSet for later retry; these lazy entries are drained each update (one at a time) when nothing else is pending.
-
Ensures proper disposal of temporary Native containers and completes job handles where necessary.
-
private void SetupPathfind(Entity entity, float3 startPos, float3 endPos, RouteLane startLane, RouteLane endLane, RouteData route, RouteConnectionData routeConnection, bool highPriority)
: System.Void
Constructs PathfindParameters suitable for the route connection and route type (sets speeds, weights, flags, ignored rules, path methods) and builds a PathfindAction containing start/end PathTarget entries. Then enqueues the action on the m_PathfindQueueSystem with PathEventData containing the start/end positions. This is the method that actually triggers a pathfinding job for a route segment. -
private void __AssignQueries(ref SystemState state)
: System.Void
Auto-generated helper used in OnCreateForCompiler; currently contains no custom queries (a no-op builder in this implementation). -
protected override void OnCreateForCompiler()
: System.Void
Auto-generated entry called by the compiler; calls __AssignQueries and assigns type handles using __TypeHandle.__AssignHandles. -
(Nested/Burst jobs and helper types)
- RoutePathType (struct, IEquatable
)
Small value type representing a route lane/connection type tuple: RouteConnectionType, RoadTypes, TrackTypes. Used as key entries in NativeParallelHashSet<> to compare route types across lanes and route connection data. Implements Equals and GetHashCode. - CheckRoutePathsJob : IJobChunk (Burst compiled)
Scans PathElement buffers on segments and enqueues owning segment Entities when any PathElement references a Deleted target. - CheckAppliedLanesJob : IJobChunk (Burst compiled)
Inspects chunks with lane prefab refs and lane type components to add RoutePathType entries to a NativeParallelHashSetreflecting what path types are present in the world currently (Car, Watercraft, Train, Tram, Subway, Pedestrian, etc.). - CheckSegmentRoutes : IJobChunk (Burst compiled)
Iterates segment chunks, reads owner prefab route connection data and checks whether the route connection type/road/track matches any entries in the path type set; enqueues segments that match for update. - TypeHandle (nested struct)
Auto-generated collection of EntityTypeHandle, BufferTypeHandle and ComponentLookup/ComponentTypeHandle instances used across jobs and OnUpdate to avoid repeated GetComponentTypeHandle/GetBufferTypeHandle calls. It has a __AssignHandles(ref SystemState) method used to initialize the contained handles.
Usage Example
// RoutePathSystem is an ECS system managed by the World and runs automatically.
// You can get a reference to it if you need to inspect or trigger system-level behaviors:
var routeSystem = World.DefaultGameObjectInjectionWorld
.GetExistingSystemManaged<Game.Routes.RoutePathSystem>();
// There are no public methods to force a path recompute directly other than the
// game's ECS flow. The system serializes its lazy update set automatically on save.
// You can, however, call SetDefaults via the system reference when implementing
// custom serialization/initialization helpers (requires a proper Context object).
// Example (conceptual):
// routeSystem.SetDefaults(myContext);
Notes and implementation details: - This system heavily uses Burst-compiled IJobChunk jobs and Native collections (NativeQueue, NativeParallelHashSet) to efficiently find affected segments on changes. - The m_LazyUpdateSet is persisted via ISerializable/IDefaultSerializable, so any segments marked for lazy update survive save/load. - PathfindParameters use IgnoreFlow and Stable flags and a fairly high run speed limit (277.77777f = 1000 km/h in m/s units used internally?) — these are tuned for route pathfinding needs. - The system assumes presence of many game-specific component types: RouteLane, RouteWaypoint, RouteData, RouteConnectionData, PathTargets, PathElement, Segment, Owner, PrefabRef, Deleted, etc.