Skip to content

Game.UI.InGame.IndicatorValue

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

Type: readonly struct

Base: System.ValueType, IEquatable, IJsonWritable

Summary:
Represents a small immutable value object used by UI indicators to describe a numeric value constrained to a range. It stores a min and max range and a current value that is clamped into that range. Provides JSON serialization via IJsonWritable and a helper to calculate a normalized indicator from supply/demand values (useful for supply/demand UI bars in Cities: Skylines 2). The struct is immutable (readonly) and implements value equality.


Fields

  • This type does not declare any explicit fields; it exposes three read-only properties (min, max, current) and relies on the compiler-generated backing (auto-)fields for those properties.

Properties

  • public float min { get; }
    Represents the lower bound of the indicator range. This is the minimum allowable value for current.

  • public float max { get; }
    Represents the upper bound of the indicator range. This is the maximum allowable value for current.

  • public float current { get; }
    The current indicator value. Constructed values are clamped to [min, max] using Unity.Mathematics.math.clamp, so current is always within the defined range.

Constructors

  • public IndicatorValue(float min, float max, float current)
    Creates a new IndicatorValue with the given min/max bounds and a current value. The provided current is clamped to the [min, max] range before storage. This makes the struct safe to construct with out-of-range inputs.

Methods

  • public void Write(IJsonWriter writer)
    Serializes the instance to a JSON writer. The method writes a type begin with the struct's name, then writes the properties "min", "max", and "current" in that order, and finishes the type. Useful for dumping UI state or saving indicator values.

  • public static IndicatorValue Calculate(float supply, float demand, float minRangeFactor = -1f, float maxRangeFactor = 1f)
    Helper that computes a normalized/current indicator value from supply and demand. If supply > float.Epsilon it computes (supply - demand) / supply and clamps that result to [minRangeFactor, maxRangeFactor]; if supply is effectively zero it returns minRangeFactor as the current value. Returns a new IndicatorValue constructed with the supplied range factors and the computed current value. Commonly used to transform supply/demand ratios into UI indicator values.

  • public bool Equals(IndicatorValue other)
    Value equality implementation: returns true when min, max and current are all equal to the corresponding values in other.

  • public override bool Equals(object obj)
    Overrides Object.Equals to support boxed comparisons. If obj is an IndicatorValue it delegates to the strongly-typed Equals implementation.

  • public override int GetHashCode()
    Returns a hash code derived from the (min, max, current) tuple. Suitable for use in hash-based collections.

Usage Example

// Create an indicator directly (current will be clamped to the range)
var indicator = new IndicatorValue(-1f, 1f, 0.75f);

// Compute an indicator from supply/demand (e.g. for a UI bar)
float supply = 100f;
float demand = 80f;
var computed = IndicatorValue.Calculate(supply, demand); // normalized (supply-demand)/supply, clamped to [-1,1]

// Serialize to JSON using a provided IJsonWriter
IJsonWriter writer = ...;
computed.Write(writer);

Notes: - Uses Unity.Mathematics.math.clamp for clamping behavior. - Immutable/read-only pattern makes instances safe to share without defensive copying. - The Calculate method guards against division by (near) zero by returning the minimum factor when supply is effectively zero.