Skip to content

Game.Prefabs.CityServiceStatistic

Assembly:
{{ Likely part of the game's core/modding assembly that contains game prefabs (Cities: Skylines 2 runtime assemblies). If you know the exact assembly name (for example, "Assembly-CSharp"), place it here. }}

Namespace: Game.Prefabs

Type:
public class CityServiceStatistic

Base:
ParametricStatistic

Summary:
{{ CityServiceStatistic is a ParametricStatistic-derived component used to provide statistic parameters for city service types. It exposes an array of CityServiceInfo entries (m_CityServices) and yields StatisticParameterData entries for each configured service, using the service enum value and an associated color. It also resolves parameter names from the CityService enum. This class is typically configured through the prefab/component inspector or by code at runtime. }}


Fields

  • public CityServiceInfo[] m_CityServices
    {{ Array of CityServiceInfo entries describing which city services this statistic should represent. Each CityServiceInfo is expected to contain at least a CityService enum value (m_Service) and a color (m_Color). This field may be null or empty; GetParameters() checks for null before iterating. }}

Properties

  • (none)
    {{ This class does not declare properties of its own. It relies on inherited behaviour from ParametricStatistic. }}

Constructors

  • public CityServiceStatistic()
    {{ No explicit constructor is declared in the source; the default parameterless constructor is used. Initialization of m_CityServices is typically done via the inspector or by code after construction. }}

Methods

  • public override IEnumerable<StatisticParameterData> GetParameters()
    {{ Returns a sequence of StatisticParameterData objects, one for each entry in m_CityServices. Implementation details:
  • If m_CityServices is null, the method yields nothing.
  • Iterates through m_CityServices using an index and yields new StatisticParameterData with:
    • parameter id: (int) m_CityServices[i].m_Service
    • color: m_CityServices[i].m_Color
  • Implemented as an iterator (yield return), so the compiler generates a state machine (IEnumerable). Use this method when the statistics system needs the parameter list for rendering or aggregating service statistics. }}

  • public override string GetParameterName(int parameter)
    {{ Returns the name of the CityService enum value corresponding to the provided parameter integer by calling Enum.GetName(typeof(CityService), parameter). If the integer does not map to a defined CityService value, Enum.GetName will return null. This method is used to get human-readable parameter labels for the statistic parameters. }}

Usage Example

// Example: populate the city service statistic from code and enumerate parameters
var stat = new CityServiceStatistic();
stat.m_CityServices = new CityServiceInfo[]
{
    new CityServiceInfo { m_Service = CityService.Police, m_Color = Color.blue },
    new CityServiceInfo { m_Service = CityService.Fire, m_Color = Color.red },
};

// Iterate parameters exposed by the statistic
foreach (var param in stat.GetParameters())
{
    int id = param.parameter; // parameter id is the CityService enum value cast to int
    Color color = param.color;
    string name = stat.GetParameterName(id) ?? $"Service_{id}";
    // use id, color, name for UI, graphs, etc.
}

{{ Notes and tips: - Ensure CityServiceInfo and CityService types are available; they define the service enum and color data used here. - GetParameterName uses Enum.GetName which returns null for invalid values — consider providing a fallback name when presenting to users. - Because GetParameters uses an iterator, enumerate it only when needed; repeated enumeration will reconstruct the iterator each time. - m_CityServices is public, so it can be set via inspector on the prefab or assigned in code. }}