Skip to content

Game.Simulation.CompanyStatisticsSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
CompanyStatisticsSystem is an ECS system responsible for aggregating per-company statistics and reporting them to the central CityStatisticsSystem. It queries company-related entities (work providers with renters and update frames) on a coarse update interval (returned by GetUpdateInterval) and schedules a Burst-compiled job (ProcessCompanyStatisticsJob) that iterates matching chunks, collects provider counts, current workers and max workers per resource, and enqueues StatisticsEvent entries describing processing/service/office counts and worker numbers. The system uses SharedComponent UpdateFrame to process only entities that belong to the current update slice and interacts with CityStatisticsSystem by acquiring a StatisticsEvent queue (parallel writer) and registering itself as a writer (AddWriter).


Fields

  • private EntityQuery m_CompanyGroup
    The query that selects company entities to process. It requires WorkProvider, PropertyRenter and UpdateFrame, and either IndustrialCompany or CommercialCompany. Used as the input for the scheduled chunk job.

  • private SimulationSystem m_SimulationSystem
    Reference to the global SimulationSystem (used to get the current frame index and compute the update frame slice).

  • private CityStatisticsSystem m_CityStatisticsSystem
    Reference to the CityStatisticsSystem to obtain the StatisticsEvent queue and register job dependencies/writers so statistical events are safely published.

  • private TypeHandle __TypeHandle
    Internal helper struct holding ComponentTypeHandle/BufferTypeHandle/ComponentLookup instances used to build the job's handle set. Populated by __AssignHandles and used when creating the ProcessCompanyStatisticsJob.

  • private struct ProcessCompanyStatisticsJob (nested)
    Burst-compiled IJobChunk implementation that performs the per-chunk processing: reads WorkProvider, PrefabRef, Employee buffers and IndustrialProcessData lookup, counts providers/workers/max workers per resource, determines statistic types (service/office/processing), and enqueues corresponding StatisticsEvent entries into the CityStatisticsSystem queue via a ParallelWriter.

  • private struct TypeHandle (nested)
    Holds the ComponentTypeHandle/BufferTypeHandle/ComponentLookup fields for WorkProvider, Employee buffer, PrefabRef, CommercialCompany and IndustrialProcessData. Provides __AssignHandles(ref SystemState) to obtain handles from a SystemState.

Properties

  • None (this system exposes no public properties)

Constructors

  • [Preserve] public CompanyStatisticsSystem()
    Default constructor. The system is expected to be created and managed by the World/EntityManager; the constructor contains no custom initialization beyond the base.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 512. This configures the system to run at a coarse cadence (the system's query and scheduling are designed to operate on update-frame slices computed from the simulation frame index).

  • [Preserve] protected override void OnCreate()
    Initializes the system: constructs the EntityQuery (m_CompanyGroup) that selects entities with WorkProvider, PropertyRenter and UpdateFrame and either IndustrialCompany or CommercialCompany, and caches references to SimulationSystem and CityStatisticsSystem via GetOrCreateSystemManaged. This prepares the system's dependencies and query before updates.

  • [Preserve] protected override void OnUpdate()
    Main update entry point: computes the current update-frame slice via SimulationUtils.GetUpdateFrame, populates a ProcessCompanyStatisticsJob with component handles/lookups and the city's StatisticsEvent queue (obtained as a ParallelWriter along with its dependency), then schedules the job in parallel over m_CompanyGroup using JobChunkExtensions.ScheduleParallel. The system also calls m_CityStatisticsSystem.AddWriter(base.Dependency) to register the scheduled dependency with the statistics system.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler/IL generation helper that can set up additional queries if needed. In this compiled form it creates and disposes an EntityQueryBuilder (no-op in this decompiled snippet) — used by OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Called during system initialization used by generated code: invokes __AssignQueries and __TypeHandle.__AssignHandles to populate the internal TypeHandle so that safe component handles are available for the job when OnUpdate runs.

  • private void TypeHandle.__AssignHandles(ref SystemState state) (nested method)
    Assigns the ComponentTypeHandle/BufferTypeHandle/ComponentLookup fields from the provided SystemState, marking them read-only as used by the job. This is essential for correct, burst-compatible access patterns.

  • private struct ProcessCompanyStatisticsJob.Execute(...) (nested job method)
    For each chunk matching the query and the current UpdateFrame shared component, the job:

  • Reads WorkProvider array, PrefabRef array and Employee dynamic buffers for chunk entities.
  • Uses IndustrialProcessData lookup (by company prefab) to determine which resource a provider produces/uses.
  • Counts: number of providers per resource, sum of m_MaxWorkers per resource, and current employees per resource.
  • For each resource with at least one provider, enqueues three StatisticsEvent items into the city's queue:
    • Count of providers (StatisticType.ProcessingCount / ServiceCount / OfficeCount depending on company/resource)
    • Current workers (ProcessingWorkers / ServiceWorkers / OfficeWorkers)
    • Max workers (ProcessingMaxWorkers / ServiceMaxWorkers / OfficeMaxWorkers)
  • The job checks the chunk's UpdateFrame shared component to process only the relevant slice and distinguishes commercial companies and office resources to select correct StatisticType.

Usage Example

A minimal example showing how this system obtains the City's statistics queue and how a mod or system could enqueue a custom StatisticsEvent. In practice CompanyStatisticsSystem schedules a Burst job that enqueues events every update slice; this snippet demonstrates interacting with CityStatisticsSystem similarly.

// inside another system or initialization code
CityStatisticsSystem cityStats = World.GetOrCreateSystemManaged<CityStatisticsSystem>();
JobHandle deps;
var queue = cityStats.GetStatisticsEventQueue(out deps);

// create and enqueue a single statistics event (example)
var writer = queue.AsParallelWriter();
writer.Enqueue(new StatisticsEvent {
    m_Statistic = StatisticType.ServiceCount,
    m_Change = 1,
    m_Parameter = /* resource index (int) */
});

// remember to combine/register dependencies if scheduling jobs that write to the queue
cityStats.AddWriter(myJobHandle);

Notes and tips for modders: - CompanyStatisticsSystem uses a SharedComponent UpdateFrame to divide work across frames; do not assume it processes all companies each frame. - The heavy work is done inside a Burst IJobChunk — follow the same pattern (ComponentTypeHandle / BufferTypeHandle / ComponentLookup) when writing compatible jobs. - Use CityStatisticsSystem.GetStatisticsEventQueue to obtain a thread-safe parallel writer and call AddWriter to register dependencies so the statistics system coordinates writers correctly.