Skip to content

Game.Prefabs.ExpenseStatistic

Assembly: Assembly-CSharp (typical for Cities: Skylines 2 mods)
Namespace: Game.Prefabs

Type: class

Base: ParametricStatistic

Summary:
ExpenseStatistic is a ParametricStatistic component that exposes a set of expense sources (ExpenseSourceInfo entries) as statistic parameters. Each entry in m_Expenses is turned into a StatisticParameterData (an integer parameter id derived from an ExpenseSource enum value and an associated color). The class also resolves parameter names via the ExpenseSource enum. The component is annotated with [ComponentMenu("Statistics/", typeof(StatisticsPrefab))] so it can be added through the Statistics prefab/component menu.


Fields

  • public ExpenseSourceInfo[] m_Expenses
    Array of expense definitions used to produce statistic parameters. Each ExpenseSourceInfo is expected to contain at least:
  • m_ExpenseSource (an ExpenseSource enum value) — used as the parameter id (cast to int)
  • m_Color (likely a UnityEngine.Color) — used as the parameter color If m_Expenses is null, GetParameters() yields no entries.

Properties

  • None declared in this class (inherits any properties from ParametricStatistic).

Constructors

  • public ExpenseStatistic()
    No explicit constructor is defined in the source — the default parameterless constructor is used. Initialization of m_Expenses is expected to be done via inspector or code after construction.

Methods

  • public override IEnumerable<StatisticParameterData> GetParameters()
    Returns a sequence of StatisticParameterData objects produced from the m_Expenses array. For each ExpenseSourceInfo in m_Expenses this method yields:
  • new StatisticParameterData((int)m_ExpenseSource, m_Color) The method checks for null m_Expenses and yields nothing if it's null. The method uses deferred execution (yield return) so enumeration happens at iteration time.

  • public override string GetParameterName(int parameter)
    Returns the name of the ExpenseSource enum member corresponding to the provided integer parameter by calling Enum.GetName(typeof(ExpenseSource), parameter). If the integer does not correspond to a named enum value, Enum.GetName returns null.

Usage Example

// Typical usage: assign expense entries (from code or in the inspector) so the statistic system
// can enumerate parameters and show them with configured colors.

var expenseStat = new Game.Prefabs.ExpenseStatistic();
expenseStat.m_Expenses = new ExpenseSourceInfo[]
{
    new ExpenseSourceInfo { m_ExpenseSource = ExpenseSource.Operating, m_Color = UnityEngine.Color.red },
    new ExpenseSourceInfo { m_ExpenseSource = ExpenseSource.Capital,   m_Color = UnityEngine.Color.green }
};

// When the statistics system calls GetParameters(), it will receive StatisticParameterData
// entries with ids equal to (int)ExpenseSource.Operating, (int)ExpenseSource.Capital, etc.

{{ Additional notes: - ExpenseSourceInfo, ExpenseSource, StatisticParameterData and ParametricStatistic are types defined elsewhere in the game's codebase (e.g., Game.City or statistics systems). Ensure you reference those namespaces/assemblies when using this class. - Because GetParameters() uses yield return, avoid modifying m_Expenses while it is being enumerated. - GetParameterName relies on Enum.GetName; if you pass a numeric parameter that has no corresponding named enum value, the returned string will be null. - The [ComponentMenu("Statistics/", typeof(StatisticsPrefab))] attribute places this component in the "Statistics" category of the component/prefab menu, typically used by the game's prefab editing tools. }}