Game.UI.InGame.AgeData
Assembly:
Game
Namespace:
Game.UI.InGame
Type:
readonly struct
Base:
System.ValueType, Colossal.UI.Binding.IJsonWritable
Summary:
Immutable value type that holds population counts broken down by four age groups: children, teens, adults and elders. Designed for efficient value semantics in UI code and provides JSON serialization via the IJsonWritable interface. Also supports simple addition of two AgeData values to produce an aggregated AgeData.
Fields
- This type declares no explicit (user-written) fields. It exposes four read-only auto-properties: children, teens, adults and elders.
These are implemented as compiler-generated backing fields but are not directly accessible.
Properties
-
public int children { get; }
Holds the number of children. Read-only; set only via constructor. -
public int teens { get; }
Holds the number of teenagers. Read-only; set only via constructor. -
public int adults { get; }
Holds the number of adults. Read-only; set only via constructor. -
public int elders { get; }
Holds the number of elders. Read-only; set only via constructor.
Constructors
public AgeData(int children, int teens, int adults, int elders)
Initializes a new AgeData instance with the provided counts for each age group. Arguments are assigned directly to the corresponding read-only properties. Because the struct is marked readonly, its fields cannot be changed after construction.
Methods
-
public static AgeData operator +(AgeData left, AgeData right)
Returns a new AgeData whose age-group counts are the element-wise sum of left and right: children = left.children + right.children, and similarly for teens, adults, elders. Useful for aggregating multiple AgeData instances (for example, summing across districts or zones). -
public void Write(IJsonWriter writer)
Implements IJsonWritable serialization. The method serializes the instance as: - TypeBegin(GetType().FullName)
- property "values" as an array of four integers in the order: children, teens, adults, elders
- property "total" as the sum of the four values
- TypeEnd()
This produces a compact representation suitable for the game's JSON-based UI/data pipeline. Example JSON structure (conceptual): { "TypeName": { "values": [children, teens, adults, elders], "total": sum } }
Remarks: - The order of values in the array is significant and must be preserved when reading. - No null checks are required because this is a value type with primitive fields.
Usage Example
// Create individual AgeData instances
var zoneA = new AgeData(children: 10, teens: 5, adults: 40, elders: 3);
var zoneB = new AgeData(children: 2, teens: 1, adults: 15, elders: 0);
// Aggregate using the + operator
AgeData total = zoneA + zoneB; // children=12, teens=6, adults=55, elders=3
// Serialize to JSON using an IJsonWriter instance provided by the game's systems
IJsonWriter writer = /* obtain writer from the game's serializer */;
total.Write(writer);
Additional notes for modders: - Because AgeData is a readonly struct with small size and value semantics, it's cheap to copy and well-suited to aggregate operations in performance-sensitive UI code. - When reading JSON produced by Write, expect an array of four integers in the "values" property and a computed "total" property. - There are no helpers for parsing JSON back into AgeData in this type; if you need deserialization, implement a complementary reader that follows the same ordering.