Game.Objects.SpawnLocationConnectionSystem
Assembly:
Namespace: Game.Objects
Type: class
Base: GameSystemBase
Summary:
SpawnLocationConnectionSystem is a Unity ECS system used by Cities: Skylines 2 to maintain and update SpawnLocation components for in-game objects (buildings/objects that spawn vehicles/agents). It listens for changes in the world (updated nets, lots, spaces and SpawnLocation updates) and schedules Burst-compiled jobs that search nearby nets, areas and airways to find the best lane(s) and curve positions to connect a spawn location to the traffic network. The system uses quad-trees, parallel native collections and command buffers to perform high-performance, multi-threaded updates and mark entities that require pathfinding updates.
Fields
-
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the net search system used to query net quadtrees (roads/tracks) for lanes to connect spawn locations. -
private Game.Net.UpdateCollectSystem m_NetUpdateCollectSystem
Reference to the net update collector; used to check whether nets have been updated and to obtain updated net bounds. -
private AirwaySystem m_AirwaySystem
Reference to the airway system used for airplane/helicopter lane lookups for air-type spawn connections. -
private Game.Areas.SearchSystem m_AreaSearchSystem
Reference to the area search system used to find lots/spaces and area triangles relevant to spawn connection searches. -
private Game.Areas.UpdateCollectSystem m_AreaUpdateCollectSystem
Reference to the area update collector; used to check whether lots/spaces have been updated and to obtain updated bounds. -
private SearchSystem m_ObjectSearchSystem
Reference to the global object search tree used to find nearby spawn locations when nets/areas/lots change. -
private ModificationBarrier5 m_ModificationBarrier
Modification barrier used to obtain a parallel command buffer writer for making structural changes (adding PathfindUpdated, removing/setting components) safely from jobs. -
private EntityQuery m_UpdatedQuery
EntityQuery that detects SpawnLocation entities flagged with Updated or RoadConnectionUpdated events so they can be re-evaluated. -
private TypeHandle __TypeHandle
Internal container for ComponentLookup/TypeHandle/BufferLookup instances the system assigns once during setup to be used when scheduling jobs.
Properties
- None (this system exposes no public properties)
Constructors
public SpawnLocationConnectionSystem()
Default constructor. The system is attributed with Preserve and the real initialization occurs in OnCreate.
Methods
-
protected override void OnCreate()
Initializes references to other world systems (net/area/airway search and update-collect systems, object search system and modification barrier). It also builds the EntityQuery used to detect spawn locations that require re-evaluation. This method wires up the TypeHandle assignment path used later by jobs. -
protected override void OnUpdate()
Main update loop. Checks whether any relevant updates occurred (net/lot/space updates or SpawnLocation update events). If updates exist it: - Allocates temporary native queues & list (per-job-safe collections) to gather updated spawn location entities.
- Schedules FindUpdatedSpawnLocationsJob instances to query the object search tree for SpawnLocation entities within updated bounds (nets/lots/spaces).
- Optionally schedules CheckUpdatedSpawnLocationsJob to gather spawn locations from RoadConnectionUpdated events.
- Runs ListUpdatedSpawnLocationsJob to merge and de-duplicate those queues into a sorted NativeList
. - Schedules FindSpawnLocationConnectionJob (Burst, parallel) over the list of updated entities to compute the best lane connections and curve positions. That job:
- Looks up prefab spawn configuration (SpawnLocationData).
- Handles air connections via AirwayData maps.
- Uses net/area/attached/owner fallbacks to find best lane(s).
- Applies connection-type checks (road/track/pedestrian/air) and respects activity masks and route connection rules.
- Writes updates to SpawnLocation components via a parallel EntityCommandBuffer and adds PathfindUpdated when a spawn location changed.
-
Manages job dependencies and registers readers with the appropriate search/update systems; disposes temporary native containers once jobs are scheduled.
-
protected override void OnCreateForCompiler()
Internal helper called during codegen/compilation for system initialization. Calls __AssignQueries and assigns Component/Buffer/Type handles via __TypeHandle. -
private void __AssignQueries(ref SystemState state)
Compiler-generated helper that sets up any EntityQueryBuilder usage and is used by OnCreateForCompiler. It is essentially a placeholder for query assignments done at compile-time. -
(nested types and jobs) FindUpdatedSpawnLocationsJob, CheckUpdatedSpawnLocationsJob, ListUpdatedSpawnLocationsJob, FindSpawnLocationConnectionJob and several iterator/helper structs.
These nested Burst-compiled jobs perform the heavy work: - FindUpdatedSpawnLocationsJob: iterates a NativeList
(deferred) and queries the object search quadtree in parallel to enqueue any SpawnLocation entities intersecting expanded bounds. - CheckUpdatedSpawnLocationsJob: chunk-based job that inspects entities with RoadConnectionUpdated or the Updated query and enqueues relevant spawn locations.
- ListUpdatedSpawnLocationsJob: merges up to four native queues into a single NativeList
, sorts and de-duplicates the list (stable, by entity index) for parallel processing. - FindSpawnLocationConnectionJob: for each spawn location entity, inspects prefab data, attached/owner/building fallbacks, searches nearby net quadtrees and area quadtrees and airway maps, determines best connection lanes and curve positions, and updates SpawnLocation components (and adds PathfindUpdated) via a parallel command buffer.
Notes about the jobs: - All heavy jobs are Burst-compiled and use NativeQuadTree iteration or area triangle distance checks for accuracy and speed. - Jobs use NativeQueue/NativeList deferred arrays and are scheduled with proper dependency chaining and external readers registered in the various systems to maintain correct concurrency. - The FindSpawnLocationConnectionJob contains multiple lane-type checks and logic for pairing car lanes into opposite-direction pairs where applicable (m_ConnectedLane1/m_ConnectedLane2).
Usage Example
// Systems are created and managed by the World. You can get a reference to this system from your mod code:
var spawnConnSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Objects.SpawnLocationConnectionSystem>();
// The system runs automatically each frame. If you need to force a single update (rare), you can call:
spawnConnSystem.Update(); // not commonly needed — use carefully in mod context
// Example: inside a custom system you can query SpawnLocation components or listen for PathfindUpdated
// to react when the SpawnLocationConnectionSystem marks an entity for pathfind recalculation.
Additional tips for modders: - If you add custom spawn-related prefabs, supply a proper SpawnLocationData and RouteConnectionData so this system can find and respect the desired connection type and activity masks. - When interacting with spawn locations from jobs, be mindful that this system uses ModificationBarrier5 to make structural changes; use the same barrier or a compatible approach to avoid race conditions. - The system uses distance thresholds and clamps for curve positions; when creating custom net geometries, ensure curves and lane flags match the expected ConnectionLane/LaneFlags to be discovered correctly.