Skip to content

Game.DispatchWaterSystem

Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
DispatchWaterSystem is an ECS system responsible for applying water and sewage flow results from the water-pipe simulation to building WaterConsumer components. It schedules a Burst-compiled IJobChunk (DispatchWaterJob) that reads the water-pipe graph (via WaterPipeFlowSystem) and per-building connection data, computes fulfilled fresh water and sewage values, updates pollution, toggles UI notifications (through IconCommandSystem), and writes building efficiency penalties based on the configured BuildingEfficiencyParameterData. The system runs on a fixed update cadence (see GetUpdateInterval/GetUpdateOffset) and only updates when consumer buildings and the required parameter singletons are available.


Fields

  • private WaterPipeFlowSystem m_WaterPipeFlowSystem
    This is a cached reference to the WaterPipeFlowSystem (the system that computes the flow graph). DispatchWaterSystem reads the sink node and uses the flow data produced by this system. The DispatchWaterJob is only scheduled when m_WaterPipeFlowSystem.ready is true.

  • private IconCommandSystem m_IconCommandSystem
    Used to create an IconCommandBuffer that the job writes to in order to add/remove building notifications (e.g., water shortage, dirty water). The command buffer is submitted back to the IconCommandSystem so UI icons are updated safely from the job.

Properties

  • public bool freshConsumptionDisabled { get; set; }
    When true, fresh water consumption is treated as fully satisfied inside the job (the job forces full fresh consumption and suppresses water shortage/disconnected flags). This is typically used for debugging or special game states where water shortages should be ignored.

  • public bool sewageConsumptionDisabled { get; set; }
    When true, sewage consumption is treated as fully satisfied (the job forces full sewage fulfillment and suppresses sewage-related warnings/flags).

Constructors

  • public DispatchWaterSystem()
    Default system constructor. Initialization of query handles and other runtime linking is performed in OnCreate / OnCreateForCompiler. The system is preserved for managed creation (OnCreate is decorated with [Preserve] in the class).

Methods

  • protected override void OnCreate() : System.Void
    Sets up required systems and entity queries. Specifically:
  • Retrieves or creates the WaterPipeFlowSystem and IconCommandSystem from the world.
  • Builds an EntityQuery to select buildings with a WaterConsumer component (excluding Destroyed, Deleted and Temp).
  • Registers RequireForUpdate on the consumer query and on the singleton parameter data types (WaterPipeParameterData and BuildingEfficiencyParameterData) to ensure the system only runs when necessary data is present.

Additional important methods (implemented in the system): - protected override void OnUpdate()
- If the WaterPipeFlowSystem is ready, creates and schedules the DispatchWaterJob (IJobChunk) to process all consumer buildings in parallel. - The job is provided with Entity and component type handles, component lookups for water-pipe graph elements, an IconCommandBuffer, parameter singletons, the sink node from the flow system, and random seed. - Adds the job dependency to the IconCommandSystem so icon commands are executed after the job.

  • public override int GetUpdateInterval(SystemUpdatePhase phase) / public override int GetUpdateOffset(SystemUpdatePhase phase)

    • These methods control the system's scheduling cadence. In the implementation the interval is 128 and the offset is 62 (so updates are spaced deterministically across frames).
  • protected override void OnCreateForCompiler()

    • Internal helper that assigns component type/lookup handles and builds queries used by the job. This is part of the generated/boilerplate ECS wiring.

Internals inside the DispatchWaterJob (high-level): - Reads WaterPipeBuildingConnection or falls back to building's road edge -> node connection to find the flow edge to the sink node. - Computes fulfilled fresh water and sewage amounts (respecting edge capacities and flows). - Applies m_FreshConsumptionDisabled / m_SewageConsumptionDisabled behavior. - Updates WaterConsumer fields: m_FulfilledFresh, m_FulfilledSewage, m_Pollution, and flags (WaterConnected/SewageConnected). - Handles notification cooldowns (kAlertCooldown) and posts/removes IconCommandBuffer notifications for shortages and dirty water. - Updates building Efficiency buffer entries to apply penalties for water shortage, pollution, and sewage backup.

Usage Example

// Accessing the system and toggling its properties at runtime:
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var dispatchSystem = world.GetExistingSystemManaged<Game.Simulation.DispatchWaterSystem>();

// Temporarily disable fresh water shortages (e.g., for testing)
dispatchSystem.freshConsumptionDisabled = true;

// Re-enable normal behavior later
dispatchSystem.freshConsumptionDisabled = false;

If you need a code example showing how to extend or hook into the system (for example to monitor when icons are added), tell me what you want to do and I can provide a tailored snippet.