Game.UI.InGame.UIPolicySlider
Assembly: Assembly-CSharp (game code)
Namespace: Game.UI.InGame
Type: readonly struct
Base: System.ValueType, implements IEquatable
Summary:
Represents a UI policy slider value and metadata used by the in-game policy UI. The struct stores the current slider value together with its range, default, step size and unit. It is immutable (readonly) and provides JSON serialization via IJsonWritable plus equality members and a hash code implementation.
Fields
-
private readonly float m_Value
The current value of the slider. -
private readonly float m_Default
The default value for the slider (from PolicySliderData). -
private readonly float m_Step
The step increment for the slider (from PolicySliderData). -
private readonly PolicySliderUnit m_Unit
The unit enum for the slider (cast from PolicySliderData.m_Unit). PolicySliderUnit is an enum representing the unit type (e.g., percent, absolute, etc.).
Properties
public Bounds1 range { get; }
The range (min/max) for the slider expressed as a Bounds1 (from Colossal.Mathematics). This is an auto-property with a getter only — part of the struct's immutable state.
Constructors
-
public UIPolicySlider(float value, PolicySliderData sliderData)
Initializes a UIPolicySlider with an explicit current value and reads range, default, step and unit from the provided PolicySliderData. -
public UIPolicySlider(PolicySliderData sliderData)
Initializes a UIPolicySlider using the PolicySliderData's default value as the current value (and reads range, default, step and unit from the data).
Methods
public void Write(IJsonWriter writer)
Serializes this UIPolicySlider to a JSON writer. The method writes a type begin with TypeNames.kPolicySlider, then writes properties:- "range" (Bounds1)
- "value" (current slider value)
- "default" (default value)
- "step" (step increment)
-
"unit" (string name of the PolicySliderUnit enum) and then ends the type. Note: unit is written as Enum.GetName(...) producing the enum name string.
-
public override int GetHashCode()
Returns a hash code based on the tuple (range, m_Value, m_Default, m_Step, m_Unit) by calling GetHashCode on that tuple. -
public override bool Equals(object obj)
Standard override that checks whether obj is a UIPolicySlider and, if so, defers to Equals(UIPolicySlider). -
public bool Equals(UIPolicySlider other)
Value equality comparison that compares range, m_Value, m_Default, m_Step and m_Unit. NOTE: the decompiled source contains a bug: two of the local variables for the "other" instance (step2 and unit2) are incorrectly assigned from this instance rather than from other, which makes the comparison ignore the other instance's step and unit. The intended comparison should use other.m_Step and other.m_Unit. Corrected snippet: - float step2 = other.m_Step;
- PolicySliderUnit unit2 = other.m_Unit;
With that correction the method returns true iff all fields and range match.
-
public static bool operator ==(UIPolicySlider left, UIPolicySlider right)
Returns left.Equals(right). -
public static bool operator !=(UIPolicySlider left, UIPolicySlider right)
Returns !left.Equals(right).
Usage Example
// Create from PolicySliderData using default value
var slider = new UIPolicySlider(sliderData);
// Create from PolicySliderData with explicit value
var customSlider = new UIPolicySlider(0.75f, sliderData);
// Serialize to JSON
writer.TypeBegin(TypeNames.kPolicySlider);
customSlider.Write(writer);
writer.TypeEnd();
// Example of expected JSON properties (approximate):
// {
// "type": "PolicySlider",
// "range": { /* Bounds1 representation */ },
// "value": 0.75,
// "default": 0.5,
// "step": 0.01,
// "unit": "Percent"
// }
Notes and tips for modders: - Bounds1 (Colossal.Mathematics) encapsulates the allowed range and must be serialized/deserialized using the same writer/reader helpers used elsewhere in the game's code. - UIPolicySlider is immutable and small (value type), making it cheap to pass by value. - If you rely on equality comparisons between UIPolicySlider instances, verify that the Equals(UIPolicySlider) implementation used in your runtime includes the correction for comparing step and unit from the other instance. If not, two sliders that differ only by step or unit may be considered equal incorrectly.