Game.UI.InGame.ProfitabilitySection
Assembly:
Assembly-CSharp (game core assembly)
Namespace:
Game.UI.InGame
Type:
class
Base:
InfoSectionBase
Summary:
ProfitabilitySection is a UI info section used by the in-game inspector to compute and present company/building/district profitability information (for a selected building or a selected district). It uses Unity.Entities/ECS jobs (ProfitabilityJob and DistrictProfitabilityJob) to gather data from many component lookups and buffer lookups (employees, renters, resource availabilities, trade costs, building data, company data, etc.), aggregates results into NativeArray fields, and produces a CompanyProfitability and a sorted list of FactorInfo entries. The section also exposes the computed data to the JSON writer for the UI frontend. The class manages persistent NativeArray/NativeList memory and schedules/completes jobs in OnUpdate.
Fields
-
private TaxSystem m_TaxSystem
Used to obtain current tax rates (m_TaxSystem.GetTaxRates()) used by jobs when calculating happiness/profitability factors. -
private ResourceSystem m_ResourceSystem
Used to obtain resource prefab information (ResourcePrefabs) provided to the jobs. -
private EntityQuery m_DistrictBuildingQuery
Query used to enumerate buildings when the selected entity is a district. -
private EntityQuery m_ProcessQuery
Query used to collect industrial processes (entities with IndustrialProcessData) and pass them to jobs. -
private EntityQuery m_EconomyParameterQuery
Query to read global EconomyParameterData singleton used by jobs. -
public NativeArray<int> m_Results
NativeArray of length 3 used by the jobs to aggregate results. Indices/meaning: - m_Results[0] — visibility flag (1 if any company/abandoned present -> section visible)
- m_Results[1] — company count used for averaging
-
m_Results[2] — summed profitability values (used to compute average profitability)
-
private NativeArray<int2> m_Factors
NativeArray (size 28) used to accumulate happiness/profitability factor counts and totals for all considered factors. Used to build profitabilityFactors list in OnProcess. -
private TypeHandle __TypeHandle
Compiler-generated container for EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup handles used when scheduling jobs. -
private CompanyProfitability profitability { get; set; }
Property holding the computed CompanyProfitability result calculated in OnProcess. -
private NativeList<FactorInfo> profitabilityFactors { get; set; }
Persistent NativeList that stores factor index + average value pairs (FactorInfo) for the section. Sorted in OnProcess and limited when writing out.
Properties
-
protected override string group => "ProfitabilitySection"
Identifier string for the info section group. Used internally by the UI system to group/identify the section. -
private CompanyProfitability profitability { get; set; }
Holds final calculated profitability score (constructed from averaged job results in OnProcess). Serialized to JSON in OnWriteProperties. -
private NativeList<FactorInfo> profitabilityFactors { get; set; }
List of FactorInfo entries computed from m_Factors (average factor value per factor index). Written to JSON (up to 10 entries).
Constructors
public ProfitabilitySection()
Default constructor (marked [Preserve] on lifecycle methods). The heavy initialization of systems and persistent native collections happens in OnCreate.
Methods
protected override void OnCreate()
Initializes systems and persistent native collections:- Retrieves TaxSystem and ResourceSystem instances.
- Creates EntityQueries (district building query, process query, economy parameter query).
-
Allocates m_Factors (NativeArray
), profitabilityFactors (NativeList ), and m_Results (NativeArray ). This method must be paired with OnDestroy's disposal to avoid memory leaks. -
protected override void OnDestroy()
Disposes persistent native containers (m_Results, m_Factors, profitabilityFactors) and calls base.OnDestroy(). -
protected override void Reset()
Resets internal state between selections: -
Clears m_Factors, resets profitability to default, clears profitabilityFactors, and zeroes m_Results entries.
-
protected override void OnUpdate()
Core update flow: - Collects processes and economy parameters, tax rates and resource prefabs.
- If the selected entity is a District + Area: schedules and runs DistrictProfitabilityJob over m_DistrictBuildingQuery (job uses chunk iteration).
- Otherwise: schedules and runs ProfitabilityJob for a single building selection.
-
Both jobs populate m_Results and m_Factors. After completion, base.visible is set based on m_Results[0] (visibility flag).
-
protected override void OnProcess()
Post-job processing: - Computes average profitability from m_Results[2] / m_Results[1] and maps it to CompanyProfitability.
- Walks m_Factors and computes per-factor averages; non-zero averages are added as FactorInfo entries to profitabilityFactors.
-
Sorts profitabilityFactors and adds the appropriate tooltip key ("Company" for a building selection, "District" otherwise).
-
public override void OnWriteProperties(IJsonWriter writer)
Writes the computed data to the JSON writer used by the UI/front-end: - property "profitability" -> writes CompanyProfitability object.
-
property "profitabilityFactors" -> writes up to 10 FactorInfo entries as an array using FactorInfo.WriteBuildingHappinessFactor.
-
private void __AssignQueries(ref SystemState state)
Compiler-generated helper used to assign internal queries when building the system; the body in this build just creates and disposes an EntityQueryBuilder placeholder. -
protected override void OnCreateForCompiler()
Compiler helper that calls __AssignQueries and assigns TypeHandle handles via __TypeHandle.__AssignHandles.
Nested jobs / structs (important implementation details):
- private struct ProfitabilityJob : IJob
- Runs for a single selected building: looks up company on the building (CompanyUIUtils.HasCompany), reads Profitability component from company if present, calls BuildingHappiness.GetCompanyHappinessFactors to accumulate factor info into m_Factors. Populates m_Results[0..2] accordingly.
public struct DistrictProfitabilityJob : IJobChunk
-
Runs over building chunks in a district. Iterates entities in a chunk and for buildings belonging to the selected district accumulates company profitability sums/counts and updates m_Factors. At the end, adds chunk results into shared m_Results via atomic-like accumulation (the job simply sums into the shared array; the job is scheduled as a single Complete call in this code path).
-
private struct TypeHandle
- Compiler-generated container storing various EntityTypeHandle/ComponentTypeHandle/ComponentLookup/BufferLookup instances used by the jobs. It has a method __AssignHandles(ref SystemState state) to initialize all handles.
Notes and important behavior: - Memory management: m_Factors and profitabilityFactors are allocated with Allocator.Persistent and must be explicitly disposed in OnDestroy (done correctly). - Jobs are scheduled and immediately completed (Complete()) in OnUpdate; the code uses IJob and IJobChunk and many ComponentLookup/BufferLookup references for read-only access. - m_Results is reused between frames; Reset zeroes it when needed. - OnWriteProperties limits written factor entries to a max of 10 (math.min(10, profitabilityFactors.Length)).
Usage Example
// Example snippet: typical override for OnCreate/OnDestroy already implemented by the section.
// You normally don't need to call these directly; this shows what the section does during OnCreate.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire game systems used by the section
m_TaxSystem = base.World.GetOrCreateSystemManaged<TaxSystem>();
m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
// Allocate persistent native containers used by job work
m_Factors = new NativeArray<int2>(28, Allocator.Persistent);
profitabilityFactors = new NativeList<FactorInfo>(10, Allocator.Persistent);
m_Results = new NativeArray<int>(3, Allocator.Persistent);
}
If you want to read the final profitability values in a custom mod UI, inspect the JSON properties produced by OnWriteProperties (the "profitability" property and "profitabilityFactors" array) or derive from this section and extend OnProcess / OnWriteProperties to expose additional data.