Skip to content

Game.UI.InGame.OutsideConnectionsInfoviewUISystem

Assembly:
Assembly-CSharp

Namespace:
Game.UI.InGame

Type:
public class OutsideConnectionsInfoviewUISystem

Base:
InfoviewUISystemBase

Summary:
This ECS UI system drives the "Outside connections" info view chart data in Cities: Skylines 2. It collects resource production/consumption data from several economy systems, computes the top imports and exports (by resource), and exposes JSON-bound values (names, colors and chart data) to the UI via RawValueBinding objects under the "outsideInfo" group. The system uses NativeArrays and job handles from dependent systems to safely read data, sorts results into TopResource entries, and limits the presented items to the top 10.


Fields

  • private const string kGroup = "outsideInfo"
    Group key used for RawValueBinding registrations for the UI.

  • private CommercialDemandSystem m_CommercialDemandSystem
    Cached reference to the CommercialDemandSystem used to obtain commercial consumption data.

  • private IndustrialDemandSystem m_IndustrialDemandSystem
    Cached reference to the IndustrialDemandSystem used to obtain industrial consumption data.

  • private CountCompanyDataSystem m_CountCompanyDataSystem
    Cached reference to the CountCompanyDataSystem used to obtain production data across companies.

  • private PrefabSystem m_PrefabSystem
    Cached reference to the PrefabSystem used to read ResourcePrefab information (name, color, resource index).

  • private EntityQuery m_ResourceQuery
    EntityQuery that selects entities with ResourceData, TaxableResourceData and PrefabData — used to enumerate resource prefabs in the world.

  • private RawValueBinding m_TopImportNames
    Binding that produces an array of top import resource names for the UI.

  • private RawValueBinding m_TopExportNames
    Binding that produces an array of top export resource names for the UI.

  • private RawValueBinding m_TopImportColors
    Binding that produces an array of hex color strings for top import resources.

  • private RawValueBinding m_TopExportColors
    Binding that produces an array of hex color strings for top export resources.

  • private RawValueBinding m_TopImportData
    Binding that produces a ChartData object (values + total) for top imports.

  • private RawValueBinding m_TopExportData
    Binding that produces a ChartData object (values + total) for top exports.

  • private List<TopResource> m_TopImports
    List of computed TopResource entries representing imports; sorted descending by amount.

  • private List<TopResource> m_TopExports
    List of computed TopResource entries representing exports; sorted descending by amount.

Properties

  • protected override bool Active { get; }
    Active returns true if the base class is active or any of the UI bindings are active. Concretely it checks base.Active and the active flags on the RawValueBinding instances (top import/export names, colors, data). This ensures the system remains enabled only while UI needs it, avoiding unnecessary work when the info view is not shown.

Nested Types

  • public struct TopResource : IComparable<TopResource>
  • Fields: public string id; public int amount; public Color color;
  • Purpose: lightweight holder used when sorting resources for presentation. It implements IComparable to sort primarily by amount (descending) and then by id (ordinal string compare) when amounts are equal.
  • Constructor: public TopResource(string id, int amount, Color color)
  • CompareTo: sorts so largest amounts come first; ties broken by id.

Constructors

  • public OutsideConnectionsInfoviewUISystem()
  • Marked with [Preserve]. Default/no-arg constructor used by the runtime/ECS; no custom initialization here (initialization is performed in OnCreate).

Methods

  • [Preserve] protected override void OnCreate()
  • Initializes references to required systems (CommercialDemandSystem, IndustrialDemandSystem, CountCompanyDataSystem, PrefabSystem).
  • Builds an EntityQuery to enumerate resource prefabs (ResourceData, TaxableResourceData, PrefabData).
  • Allocates and initializes the m_TopImports and m_TopExports lists (initial capacity 42).
  • Calls UpdateCache() once to populate initial data.
  • Registers six RawValueBinding instances (names, colors, data for imports and exports) under the "outsideInfo" group:
    • "topImportNames" -> UpdateImportNames
    • "topImportColors" -> UpdateImportColors
    • "topImportData" -> UpdateImportData
    • "topExportNames" -> UpdateExportNames
    • "topExportColors" -> UpdateExportColors
    • "topExportData" -> UpdateExportData
  • Notes: Bindings are how this system exposes JSON data to the UI. OnCreate wires everything so the UI can request values when visible.

  • protected override void PerformUpdate()

  • Called by the ECS update loop when the system is active.
  • Calls UpdateCache() to recompute the top imports/exports.
  • Calls Update() on each RawValueBinding so bound UI consumers receive fresh JSON values when requested.

  • private void UpdateCache()

  • Core computation: enumerates resource prefab entities and computes import/export amounts using production and consumption arrays retrieved from dependent systems.
  • Steps:
    1. Converts m_ResourceQuery to NativeArray and NativeArray (Allocator.TempJob). These are disposed in finally.
    2. Calls GetProduction/GetConsumption on CountCompanyDataSystem, IndustrialDemandSystem and CommercialDemandSystem to obtain NativeArray production/consumption arrays and their JobHandles.
    3. Completes the returned JobHandles (JobHandle.CompleteAll) to ensure safe synchronous reads.
    4. Clears m_TopImports and m_TopExports.
    5. For each resource prefab:
    6. Obtains ResourcePrefab via m_PrefabSystem.GetPrefab(prefabData).
    7. Computes resource index via EconomyUtils.GetResourceIndex(EconomyUtils.GetResource(prefab.m_Resource)).
    8. Reads production and the two consumption arrays for the resource index.
    9. Applies the same algorithm used by the game to determine how much is imported and how much is exported:
      • Uses min and differences to determine how local production matches industrial and commercial consumptions.
      • amount (imports) = computed imported amount for commercial/industrial shortfall.
      • amount2 (exports) = production - matched consumption (what remains to export).
    10. Adds TopResource entries (prefab.name, amount, prefab.m_Color) to m_TopImports and (prefab.name, amount2, prefab.m_Color) to m_TopExports.
    11. Sorts both lists (descending by amount, tie by id).
    12. Disposes the NativeArrays in finally.
  • Notes & safety:

    • Completes job handles before reading NativeArrays to avoid race conditions.
    • Always disposes NativeArrays (entities and prefab data) in finally to avoid leaks.
    • m_TopImports / m_TopExports are recreated in-place to avoid GC churn except for list resizing.
  • private void UpdateImportNames(IJsonWriter binder)

  • Writes a JSON array of up to 10 top import resource ids (names) via the provided IJsonWriter.
  • Logic: min(10, m_TopImports.Count).

  • private void UpdateImportColors(IJsonWriter binder)

  • Writes a JSON array of up to 10 color hex strings (Color.ToHexCode()) for top imports.

  • private void UpdateImportData(IJsonWriter binder)

  • Writes an object of type "infoviews.ChartData" with:
    • "values": array of up to 10 integer amounts (m_TopImports[i].amount)
    • "total": integer sum of the values written
  • Used to feed the chart component with values + total.

  • private void UpdateExportNames(IJsonWriter binder)

  • Writes a JSON array of up to 10 top export resource ids (names).

  • private void UpdateExportColors(IJsonWriter binder)

  • Writes a JSON array of up to 10 color hex strings for top exports.

  • private void UpdateExportData(IJsonWriter binder)

  • Writes an object of type "infoviews.ChartData" analogous to UpdateImportData, but for exports.

Notes and Modding Tips

  • The system is authored to minimize work when the UI is not visible: the Active property checks the bindings so the system only updates when necessary. When writing similar systems, follow the same pattern to reduce overhead.
  • UpdateCache uses the game's EconomyUtils and PrefabSystem to map resource prefabs to resource indices. When adding custom resources or changing prefabs, ensure that the PrefabSystem returns correct ResourcePrefab information and that EconomyUtils covers new resource types.
  • The top item limit is hard-coded to 10 in the UI writer methods. If you wish to change the number of entries shown, modify the constant usages in UpdateImport/UpdateExport methods as needed and ensure the UI expects the new size.
  • The code completes job handles returned by other systems before reading data. Never read NativeArrays provided by other systems without ensuring the corresponding job handles are completed or combined properly to avoid race conditions/crashes.
  • The m_TopImports and m_TopExports lists are private. To observe results in other systems or display custom UI, either expose read-only accessors or create a dedicated binding or event to export the computed data.

Usage Example

// From another system or mod initialization code (within an Entities.World context):
var outsideInfoSystem = base.World.GetOrCreateSystemManaged<OutsideConnectionsInfoviewUISystem>();

// The UI bindings ("outsideInfo", "topImportNames", etc.) are registered in OnCreate()
// and the system will produce the JSON data for the info view when the UI requests it.

This describes the responsibilities, internal workings, and modding considerations for OutsideConnectionsInfoviewUISystem.