Game.City.StatisticsEvent
Assembly: Game
Namespace: Game.City
Type: struct
Base: Colossal.Serialization.Entities.ISerializable
Summary:
Represents a single statistics change event used by the game to record or transmit a change to a statistic. The struct is annotated with FormerlySerializedAs to support older serialized type names. Instances contain the statistic identifier, an integer parameter, and a floating-point change amount. The serialization logic contains compatibility handling for older save formats where the change was stored as an integer.
Fields
-
public StatisticType m_Statistic
Represents which statistic is affected (typically an enum). Serialized as an int. -
public int m_Parameter
Auxiliary integer parameter associated with the statistic change; serialized as an int. -
public float m_Change
The amount of change applied to the statistic. Serialized as a float in current formats, but deserialization supports older formats where this value was stored as an int.
Properties
- None. This type exposes public fields rather than properties. Use the fields directly when creating or reading events.
Constructors
- Implicit default constructor (value type default)
No explicit constructors are declared. You can create an instance with default values vianew StatisticsEvent()
or by initializing the fields directly.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the struct into the provided writer. The method writes m_Statistic as an int, then m_Parameter, then m_Change (float). The writer is expected to implement the IWriter interface from Colossal.Serialization.Entities. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the struct from the provided reader. Deserialization reads an int and casts it back to StatisticType for m_Statistic, then reads m_Parameter. For m_Change it checks the reader.context.version: if the saved data version is older than Version.statisticPrecisionFix, the change value is read as an int (legacy format) and converted to float; otherwise it reads a float directly. This provides backwards compatibility with older serialized formats where statistic changes were stored with integer precision.
Usage Example
// Create a new event
var ev = new StatisticsEvent {
m_Statistic = StatisticType.Population,
m_Parameter = 0,
m_Change = 12.5f
};
// Pseudo-code: serialize using a writer (IWriter implementation)
using (var writer = CreateWriter()) // CreateWriter is an engine-specific factory
{
ev.Serialize(writer);
}
// Pseudo-code: deserialize using a reader (IReader implementation)
StatisticsEvent loaded;
using (var reader = CreateReader()) // CreateReader supplies reader.context.version
{
loaded = default;
loaded.Deserialize(reader);
// loaded.m_Change will be correctly handled whether it was saved as int (older versions)
// or float (current versions) due to the version check in Deserialize.
}
Notes: - The struct is optimized for the game's serialization system and expects the writer/reader to supply context.version for version-dependent compatibility. - StatisticType and Version.statisticPrecisionFix are defined elsewhere in the codebase; the version check allows seamless migration from integer to float precision for m_Change.