Game.WaterPipeRoadConnectionGraphSystem
Assembly:
Assembly-CSharp (game runtime)
Namespace: Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
WaterPipeRoadConnectionGraphSystem monitors road connection updates and keeps the water-pipe graph in sync for road edges. It collects road-edge entities whose connections changed (via a NativeQueue), then processes those edges to ensure that any road edge that serves buildings with water consumption but lacks a building sink connection gets a flow-edge to the global sink node (or clears that sink connection when no longer needed). The system uses two Burst-compiled jobs (UpdateRoadConnectionsJob to gather updated edges, and UpdateRoadEdgesJob to inspect and modify the graph), interacts with WaterPipeFlowSystem for sink/edge archetype info, and writes structural changes through a ModificationBarrier5 command buffer.
Fields
-
private WaterPipeFlowSystem m_WaterPipeFlowSystem
Reference to the WaterPipeFlowSystem used to obtain the sink node entity and edge archetype. -
private ModificationBarrier5 m_ModificationBarrier
Barrier system used to create an EntityCommandBuffer for safe structural changes from jobs. -
private EntityQuery m_EventQuery
EntityQuery used to find entities with RoadConnectionUpdated components (events that drive updates). -
private NativeQueue<Entity> m_UpdatedEdges
Persistent NativeQueue that stores road-edge entities which require their sink connections re-evaluated. -
private JobHandle m_WriteDependencies
Accumulated JobHandle tracking dependent writers that produce data consumed by this system (used to combine scheduling dependencies). -
private TypeHandle __TypeHandle
Internal generated struct holding ComponentTypeHandle/ComponentLookup/BufferLookup instances used by the jobs.
Properties
- This class exposes no public properties.
Constructors
public WaterPipeRoadConnectionGraphSystem()
Default (parameterless) constructor. Marked with [Preserve] in the compiled code; normal initialization is done in OnCreate.
Methods
-
protected override void OnCreate()
Initializes references to WaterPipeFlowSystem and ModificationBarrier5, constructs the event query for RoadConnectionUpdated, and allocates the persistent NativeQueue for updated edges. -
protected override void OnDestroy()
Disposes the persistent NativeQueue and performs base teardown. -
public NativeQueue<Entity> GetEdgeUpdateQueue(out JobHandle deps)
Returns the internal NativeQueue of updated road-edge entities and outputs the current write dependencies (JobHandle) that the caller must respect if they will write to the queue from a job. Useful for producers that need to enqueue edges into this system's queue. -
public void AddQueueWriter(JobHandle handle)
Registers a writer job handle with the system by combining it into m_WriteDependencies. Call this from external producers that enqueued into the queue to ensure proper dependency tracking. -
protected override void OnUpdate()
Main update logic: - Combines base.Dependency with m_WriteDependencies to account for external writers.
- If there are RoadConnectionUpdated events, schedules UpdateRoadConnectionsJob (IJobChunk) to scan those events, find affected road edges for buildings that are WaterConsumers, and enqueue the old/new edges into m_UpdatedEdges (as a parallel writer).
-
Schedules UpdateRoadEdgesJob (IJob) to process and deduplicate queued edges, inspect connected buildings and water consumers, and either create/update a sink flow edge or remove the sink flow edge when appropriate. Structural changes use the command buffer from the modification barrier. The scheduled job writes are tracked and added to the barrier as producer work; m_WriteDependencies is updated to the resulting dependency.
-
private void __AssignQueries(ref SystemState state)
Generated helper (compiler path) that sets up internal queries. In this compiled class it constructs a temporary EntityQueryBuilder and disposes it; the important query for events is set up elsewhere in OnCreate. -
protected override void OnCreateForCompiler()
Generated helper invoked by the compiler setup paths to call __AssignQueries and assign component handles in __TypeHandle.
Nested job types (summary):
- UpdateRoadConnectionsJob (private struct, BurstCompile, IJobChunk)
- Reads RoadConnectionUpdated event components and checks whether the associated building is a WaterConsumer (uses ComponentLookup
). -
Enqueues old and new road-edge entities (when applicable and not deleted) into the provided NativeQueue< Entity>.ParallelWriter.
-
UpdateRoadEdgesJob (private struct, BurstCompile, IJob)
- Dequeues entities from the NativeQueue and deduplicates them with a NativeParallelHashSet.
- For each road-edge that has a WaterPipeNodeConnection, inspects the connected buildings buffer (ConnectedBuilding) to sum water demand for buildings that are WaterConsumer and that do not have a WaterPipeBuildingConnection (i.e., consumers without building sink).
- If there is consumer demand, attempts to set or create a flow edge from the road-edge's flow node to the sink node with capacity equal to summed consumer demand. Uses WaterPipeGraphUtils.TrySetFlowEdge or CreateFlowEdge (via command buffer + edge archetype).
- If no consumer demand remains, clears any existing sink flow edge by marking the flow-edge entity as Deleted via the command buffer.
Notes on thread-safety and scheduling: - External producers that enqueue into m_UpdatedEdges should call AddQueueWriter with their producer JobHandle so the system can correctly chain dependencies. - The UpdateRoadConnectionsJob runs as a parallel job that writes to the queue via AsParallelWriter; the UpdateRoadEdgesJob is scheduled after to consume the queue.
Usage Example
// Example: from another system that discovers a changed road edge in a job,
// get the queue and register the job handle so this system will wait for the producer.
protected override void OnUpdate()
{
var waterPipeSystem = World.GetOrCreateSystemManaged<WaterPipeRoadConnectionGraphSystem>();
// Suppose `producerJob` is a scheduled JobHandle that writes to the queue (enqueues Entities).
JobHandle producerJob = ScheduleMyProducerJob();
// Tell the WaterPipeRoadConnectionGraphSystem about our producer so it can combine dependencies.
waterPipeSystem.AddQueueWriter(producerJob);
// If you need to enqueue directly on the main thread:
JobHandle deps;
var queue = waterPipeSystem.GetEdgeUpdateQueue(out deps);
// Use queue.Enqueue(myEdgeEntity) here on main thread (ensure dependency rules if mixing threads).
}