Game.Buildings.RoadConnectionSystem
Assembly:
Assembly-CSharp (game runtime assembly)
Namespace:
Game.Buildings
Type:
class
Base:
GameSystemBase
Summary:
RoadConnectionSystem is an ECS system responsible for detecting, assigning and updating road connections between buildings and net edges (roads) and for maintaining secondary connection lanes/objects (utility lanes, spawn-connection lanes, parking/pedestrian connections) for buildings. It schedules several Burst-compiled jobs to:
- find candidate roads near building front positions,
- create a deduplicated list of buildings that need connection evaluation,
- calculate the best connection (edge and curve position) for each building,
- replace/emit events for changed connections,
- update or create secondary lanes and utility objects associated with the building<->road connection.
The system integrates with in-game search trees (road/object search systems), the ModificationBarrier command buffer, icon/notification system and the audio/source update subsystem. It heavily uses Unity ECS patterns (ComponentLookup/BufferLookup, ArchetypeChunk jobs, Native containers) and is optimized with Burst.
Fields
-
private ModificationBarrier4B m_ModificationBarrier
Handles deferred structural changes (command buffers) produced by the system. Used to create EntityCommandBuffer instances for applying changes that must be performed later on the main thread. -
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the net (road) search system used to query nearby net edges in a static/readonly manner for job usage. -
private Game.Objects.SearchSystem m_ObjectSearchSystem
Reference to the object search system used to query nearby objects/buildings for job usage. -
private IconCommandSystem m_IconCommandSystem
Used to add/remove icon notifications (e.g., road connection warnings) for buildings. -
private AudioManager m_AudioManager
Used to get SourceUpdateData when making modifications that should produce audio updates. -
private EntityQuery m_ModificationQuery
EntityQuery used to track relevant modifications (building/updated/deleted/spawn location / connected edges) that determine when the system should run. -
private EntityQuery m_UpdatedNetQuery
EntityQuery for recently updated nets (edges) that have connected buildings and require connection re-evaluation. -
private EntityQuery m_TrafficConfigQuery
Query to obtain traffic configuration singletons (used for notifications etc.). -
private EntityQuery m_BuildingConfigQuery
Query to obtain building configuration singletons (used when creating secondary lanes/objects). -
private EntityQuery m_ConnectionQuery
Query that collects available connection prefabs (used when creating connection lanes). -
private EntityArchetype m_RoadConnectionEventArchetype
Archetype used to create RoadConnectionUpdated event entities that inform other systems about changes. -
private ComponentTypeSet m_AppliedTypes
Set of component types used when removing/adding components during lane/object updates (e.g., Applied/Created/Updated flags). -
private TypeHandle __TypeHandle
Internal struct holding cached ComponentTypeHandle/ComponentLookup/BufferLookup handles assigned in OnCreateForCompiler for use by jobs.
Properties
- None (no public properties exposed by this system)
Constructors
public RoadConnectionSystem()
Default constructor. The system setup is performed in OnCreate; constructor itself takes no parameters.
Methods
-
protected override void OnCreate()
Initializes the system: retrieves or creates referenced systems (ModificationBarrier4B, SearchSystems, IconCommandSystem, AudioManager), sets up multiple EntityQuery instances, creates the RoadConnectionUpdated event archetype, sets m_AppliedTypes, and calls RequireForUpdate with the modification query so the system runs only when relevant data exists. -
protected override void OnUpdate()
Main scheduling method. Creates temporary Native containers (NativeQueue, NativeList), configures multiple Burst-compiled jobs and schedules them with correct dependencies: - CheckRoadConnectionJob (IJobChunk) collects buildings that might need connection updates (via object search tree and connected building buffers),
- FillReplacementListJob (IJob) dequeues the queue into a sorted/unique NativeList
, - FindRoadConnectionJob (IJobParallelForDefer) determines the best road edge, curve position, and optionally back-side road for a building,
- ReplaceRoadConnectionJob (IJob) applies the determined changes (updates Building components, creates RoadConnectionUpdated event, updates ConnectedBuilding buffers, sends icon notifications),
- UpdateSecondaryLanesJob (IJobParallelForDefer) updates/creates/removes secondary lanes and secondary objects (utility lanes/objects, connection lanes between spawn locations) for the buildings that changed connection.
This method also ensures disposing of temporary containers with appropriate job handles and registers search tree readers and command buffer writers with the referenced systems. It stores the combined job dependency back into base.Dependency and registers it with the modification barrier.
-
protected override void OnCreateForCompiler()
Internal setup used by generated code paths: assigns query handles and type handles used by jobs. Calls __AssignQueries and assigns handles inside __TypeHandle. -
private void __AssignQueries(ref SystemState state)
Compiler-generated helper for query assignment (no-op in this decompiled version but present for compatibility). -
private static void CheckDistance(EdgeGeometry edgeGeometry, EdgeNodeGeometry startGeometry, EdgeNodeGeometry endGeometry, float3 position, bool canBeOnRoad, ref float maxDistance)
Utility that refines maxDistance by checking distances between a position and edge/node geometries and their components (start/end bezier segments, node arcs). Used to determine whether an edge is sufficiently close to be considered for connection. -
private static void CheckDistance(Bezier4x3 curve1, Bezier4x3 curve2, float3 position, ref float maxDistance)
Helper to check distance between a position and two curves (used for middle connection checks) and reduce maxDistance. -
private static void CheckDistance(Bezier4x3 curve, float3 position, ref float maxDistance)
Helper to check distance between a position and a single bezier curve and reduce maxDistance. -
Nested types (jobs / helpers):
- CheckRoadConnectionJob (IJobChunk) and nested CheckRoadConnectionIterator — finds buildings near each edge and enqueues them for replacement check.
- FillReplacementListJob (IJob) — drains enqueue queue into a sorted, deduplicated NativeList
. - ReplaceRoad (struct) — data container per-building used between jobs.
- FindRoadConnectionJob (IJobParallelForDefer) and nested iterator — computes the best road (edge) and curve position for each building, also computes back-side connection when relevant. Writes results into ReplaceRoad array.
- ReplaceRoadConnectionJob (IJob) — applies component updates, creates RoadConnectionUpdated events, updates ConnectedBuilding buffers and notifications/icons.
- UpdateSecondaryLanesJob (IJobParallelForDefer) — updates/creates/removes utility lanes, connection lanes and objects for a building after a road change. Contains helpers for creating/deleting/updating lanes & objects, and logic to match original connections during temp/replace operations.
- ConnectionLaneKey (struct) — key used for matching connection lanes via PathNode pairs.
- SpawnLocationData (struct) — intermediate representation used during secondary lane creation.
- TypeHandle (struct) — stores component handles for job usage and contains an __AssignHandles(ref SystemState) method used in setup.
These nested jobs contain many more internal helper methods used to compute Bezier connections, find lane/object prefabs, calculate Y offsets, and to manipulate buffers/components via command buffers.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical usage: system auto-registers required queries and subsystems.
// If you need to interact with this system from other code, obtain it from world:
// var roadConnectionSystem = World.GetExistingSystemManaged<Game.Buildings.RoadConnectionSystem>();
}
Notes / Tips for modders: - RoadConnectionSystem is heavily integrated with the game's ECS layout. To interact safely, prefer producing or consuming RoadConnectionUpdated events or use existing API surfaces (e.g., connected building buffers) rather than directly modifying internal fields. - When adding new types of connection lanes/objects or custom spawn location connection types, ensure appropriate prefabs and configuration are added so UpdateSecondaryLanesJob can select and instantiate them correctly. - Keep in mind this system uses Native/Deferred jobs and ModificationBarrier command buffers — structural changes should be done via command buffers to avoid race conditions. - If you need to trigger a re-evaluation of connections manually, modify a building (e.g., add Updated component or change a related component) so the system picks it up on the next frame.