Skip to content

Game.UI.InGame.ProductionUISystem

Assembly: Assembly-CSharp
Namespace: Game.UI.InGame

Type: class

Base: UISystemBase

Summary:
ProductionUISystem is the UI backend system responsible for collecting, caching and exposing production-related data (resources, resource categories, production chains, services and production metrics) to the in-game UI. It binds several JSON-producing writers to a UI binding system (via AddBinding) so the frontend can request structured production data. The system queries multiple game simulation systems (prefab, resource, demand and company-count systems) and uses Native collections and JobHandles to safely copy and expose production and consumption data for each resource. The system only refreshes while the game is in Game mode and uses an internal UIUpdateState to throttle updates.


Fields

  • private const string kGroup = "production"
    Constant group name for UI binding keys.

  • private UIUpdateState m_UpdateState
    Controls throttled update timing for UI data updates.

  • private PrefabSystem m_PrefabSystem
    Reference to the game's PrefabSystem used to resolve prefabs (ResourcePrefab, ServicePrefab, etc.).

  • private ResourceSystem m_ResourceSystem
    Reference to ResourceSystem for retrieving ResourcePrefabs and resource metadata.

  • private CommercialDemandSystem m_CommercialDemandSystem
    Reference used to obtain commercial consumption per resource.

  • private IndustrialDemandSystem m_IndustrialDemandSystem
    Reference used to obtain industrial consumption per resource.

  • private CountCompanyDataSystem m_CountCompanyDataSystem
    Reference used to obtain production counts and commercial production capacities.

  • private EntityQuery m_ResourceCategoryQuery
    EntityQuery to fetch UI resource category objects/prefabs.

  • private EntityQuery m_IndustrialCompanyQuery
    EntityQuery to fetch industrial company processes (non-service, non-storage).

  • private EntityQuery m_CommercialCompanyQuery
    EntityQuery to fetch commercial company processes (service companies excluded from storage).

  • private EntityQuery m_ServiceUpkeepQuery
    EntityQuery used to find entities that contain ServiceUpkeepData and ServiceObjectData.

  • private NativeParallelMultiHashMap<Entity, (Entity, Entity)> m_ProductionChain
    Multi-hashmap mapping a resource output Entity -> one or more pairs of input resource Entities forming the production graph (used to compute inputs/outputs for resource details). Allocated persistently; must be disposed.

  • private GetterValueBinding<int> m_MaxProgressBinding
    A binding that exposes the maximum progress value (int) computed for production progress bars.

  • private RawValueBinding m_ResourceCategoriesBinding
    A binding that writes the resource categories JSON array.

  • private RawMapBinding<Entity> m_ResourceDetailsBinding
    A binding that writes resource detail JSON for a specific resource entity.

  • private RawMapBinding<Entity> m_ResourceBinding
    A binding that writes resource metadata (icon, tradable, producer, consumers) for a given resource entity.

  • private RawMapBinding<Entity> m_ServiceBinding
    A binding that writes service metadata (icon, name) for a service entity.

  • private RawMapBinding<Entity> m_DataBinding
    A binding that writes current production/ surplus/ deficit data for a resource entity.

  • private NativeList<int> m_ProductionCache
    Cached production values per resource (persistent NativeList).

  • private NativeList<int> m_CommercialConsumptionCache
    Cached commercial consumption per resource (persistent NativeList).

  • private NativeList<int> m_IndustrialConsumptionCache
    Cached industrial consumption per resource (persistent NativeList).


Properties

  • None (the class does not expose public properties).

Constructors

  • public ProductionUISystem()
    Default parameterless constructor. The system is annotated with [CompilerGenerated] in the original assembly but functions as a normal ECS UI system.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system: creates UIUpdateState, resolves required systems (PrefabSystem, ResourceSystem, demand systems, CountCompanyDataSystem), builds EntityQueries and registers UI bindings (m_MaxProgressBinding, m_ResourceCategoriesBinding, m_ResourceBinding, m_ResourceDetailsBinding, m_ServiceBinding, m_DataBinding). Allocates persistent Native collections: m_ProductionChain, m_ProductionCache, m_CommercialConsumptionCache, m_IndustrialConsumptionCache.

Notes: - Uses AddBinding to register JSON writers that the UI will call. - Native collections are allocated with Allocator.Persistent and must be disposed in OnDestroy.

  • protected override void OnGameLoaded(Context serializationContext) : System.Void
    Called when a game is loaded. If the current GameMode is Game, it builds the production chain, updates caches, and forces initial binding updates for max progress, resource categories, resources, resource details and services to populate UI.

  • protected override void OnDestroy() : System.Void
    Disposes all persistent Native collections (m_ProductionChain, m_ProductionCache, m_CommercialConsumptionCache, m_IndustrialConsumptionCache) and calls base.OnDestroy(). Important to avoid memory leaks.

  • protected override void OnUpdate() : System.Void
    Called each frame. If the game mode is Game and the internal m_UpdateState.Advance() indicates it's time to update, it refreshes the caches (UpdateCache) and updates the data and max progress bindings. This throttles UI updates to avoid doing heavy work every frame.

  • private void UpdateCache() : System.Void
    Collects production and consumption data from other systems:

  • Calls m_CountCompanyDataSystem.GetProduction(out deps) to obtain production counts.
  • Calls m_CountCompanyDataSystem.GetCommercialCompanyDatas(out deps2) for commercial produce capacity fallback.
  • Calls m_IndustrialDemandSystem.GetConsumption(out deps3) and m_CommercialDemandSystem.GetConsumption(out deps4) for consumption arrays.
  • Completes job handles appropriately (CompleteAll for some, deps2.Complete()) before copying NativeArrays into the persistent NativeLists via CopyFrom.
  • For resources that are not produceable, replaces production with commercial produce capacity (commercialCompanyDatas.m_ProduceCapacity). Notes:
  • Properly completes JobHandles before accessing results.
  • Uses EconomyUtils.GetResource(i) to map indices to Resource enum.

  • private int GetMaxProgress() : System.Int32
    Iterates all resources via ResourceIterator and computes the maximum value used for progress bars. For each resource that is leisure, material or produceable, it calls GetData(entity) and computes the max of production and (surplus + deficit) to determine the maximum progress scale used by the UI.

  • private void WriteResourceCategories(IJsonWriter writer) : System.Void
    Writes an array of resource category objects (production.ResourceCategory) to the provided IJsonWriter. Uses UIObjectInfo.GetSortedObjects for the configured UI group query, then for each category writes its name and the resources within it. Uses temporary NativeLists that are disposed via try/finally.

  • public void WriteResource(IJsonWriter writer, Entity entity) : System.Void
    Writes a single production.Resource object for the specified resource entity. Includes entity id, name, icon, tradable flag, producer info and consumers info. Catches exceptions and writes null on error while logging.

  • private void WriteProductionLink(IJsonWriter writer, UIProductionLinkPrefab prefab) : System.Void
    Writes a small ProductionLink object (type and icon) describing a producer/consumer link as stored in UIProductionLinks on a ResourcePrefab.

  • private void WriteResourceDetails(IJsonWriter writer, Entity entity) : System.Void
    Writes production.ResourceDetails for the specified resource entity. Produces:

  • inputs: arrays of input resource entity pairs from m_ProductionChain (each entry may contain 0/1/2 items).
  • outputs: all resources that use this resource as an input (found via keyValueArrays).
  • serviceOutputs: services that use this resource as upkeep (found by scanning ServiceUpkeepData). Uses temporary native arrays and disposes them in finally blocks.

  • private void FindServiceOutputs(Entity entity, NativeList<Entity> outputs, NativeArray<Entity> serviceUpkeeps) : System.Void
    Scans service upkeep buffers for service object entities that use the specified resource entity. Adds unique service entities to outputs.

  • private void FindOutputs(Entity entity, NativeList<Entity> outputs, NativeKeyValueArrays<Entity, (Entity, Entity)> keyValueArrays) : System.Void
    Scans the production key/value arrays and adds keys (output resources) whose values reference the given resource entity as input1 or input2.

  • private void WriteService(IJsonWriter writer, Entity entity) : System.Void
    Writes production.Service for a given service entity: entity id, name and icon. If the entity does not have PrefabData the method writes null.

  • private void WriteData(IJsonWriter writer, Entity entity) : System.Void
    Writes production.ResourceData: production, surplus and deficit integers for the specified resource entity. Internally calls GetData.

  • private (int, int, int) GetData(Entity entity)
    Computes and returns a tuple (production, surplus, deficit) for the given resource entity:

  • production = cached production for the resource index.
  • consumption = commercial + industrial consumption for that resource index.
  • surplus = production - consumed (limited by min logic in original code).
  • deficit = consumption - satisfied (limited by min logic). Notes:
  • Uses EconomyUtils to find resource index from a ResourcePrefab's m_Resource value.
  • Logic mirrors the original system to produce values suitable for UI display.

  • private void BuildProductionChain(NativeParallelMultiHashMap<Entity, (Entity, Entity)> multiHashMap) : System.Void
    Builds the m_ProductionChain map by reading IndustrialProcessData from industrial and commercial company entity queries. For each process, maps the output resource entity to input resource entity pair(s) via ProcessProductionChainDatas.

  • private void ProcessProductionChainDatas(NativeArray<IndustrialProcessData> datas, ResourcePrefabs resourcePrefabs, NativeParallelMultiHashMap<Entity, (Entity, Entity)> multiHashMap) : System.Void
    Iterates given IndustrialProcessData array and for each process determines input resource Entities (if different from output) and constructs a tuple (input1, input2). Adds unique tuples to the multiHashMap using TryAddUniqueValue.

  • private static void TryAddUniqueValue(NativeParallelMultiHashMap<Entity, (Entity, Entity)> multiHashMap, Entity key, (Entity, Entity) value) : System.Void
    Adds value to the multiHashMap for the key only if an identical pair isn't already present (ensures uniqueness).


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // The system creates bindings and persistent native containers:
    m_UpdateState = UIUpdateState.Create(base.World, 256);
    m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
    m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
    m_CommercialDemandSystem = base.World.GetOrCreateSystemManaged<CommercialDemandSystem>();
    m_IndustrialDemandSystem = base.World.GetOrCreateSystemManaged<IndustrialDemandSystem>();
    m_CountCompanyDataSystem = base.World.GetOrCreateSystemManaged<CountCompanyDataSystem>();

    // Example bindings added by the system:
    AddBinding(m_MaxProgressBinding = new GetterValueBinding<int>("production", "maxProgress", GetMaxProgress));
    AddBinding(m_ResourceCategoriesBinding = new RawValueBinding("production", "resourceCategories", WriteResourceCategories));
    AddBinding(m_ResourceBinding = new RawMapBinding<Entity>("production", "resources", WriteResource));
    AddBinding(m_ResourceDetailsBinding = new RawMapBinding<Entity>("production", "resourceDetails", WriteResourceDetails));
    AddBinding(m_ServiceBinding = new RawMapBinding<Entity>("production", "services", WriteService));
    AddBinding(m_DataBinding = new RawMapBinding<Entity>("production", "data", WriteData));

    // Native containers used by the system:
    m_ProductionChain = new NativeParallelMultiHashMap<Entity, (Entity, Entity)>(50, Allocator.Persistent);
    m_ProductionCache = new NativeList<int>(Allocator.Persistent);
    m_CommercialConsumptionCache = new NativeList<int>(Allocator.Persistent);
    m_IndustrialConsumptionCache = new NativeList<int>(Allocator.Persistent);
}

Notes / Modding tips: - If you extend or interact with this system, be careful with Native collections and their lifetimes — they must be disposed in OnDestroy to avoid leaks. - Many methods assume the game is in Game mode; calls and refreshes are gated by GameManager.instance.gameMode == GameMode.Game. - The system relies heavily on prefab metadata; when adding custom resources or service prefabs, ensure ResourcePrefab, UIResourceCategoryPrefab and UIProductionLinks are populated correctly so the UI writers produce accurate JSON. - Bindings are registered using AddBinding with keys under the "production" group — UI frontend code expects these keys to exist.