Game.City.CityStatistic
Assembly: Assembly-CSharp.dll (game assembly)
Namespace: Game.City
Type: struct
Base: IBufferElementData, ISerializable
Summary:
CityStatistic is a small ECS buffer element used to store a single numeric statistic for the city. It contains the current value and a running total value, both stored as double-precision floats. The type implements Colossal's ISerializable to support game save/load serialization and contains explicit version-aware deserialization logic to remain compatible with older save formats (which previously stored statistics as ints or longs).
Fields
-
public double m_Value
Current (instant) value of the statistic. Stored as a double to allow fractional and large values. -
public double m_TotalValue
Cumulative/total value tracked for the statistic. Also stored as a double for precision and range.
Properties
- None. This is a plain public-field struct used as a buffer element in ECS.
Constructors
public CityStatistic()
(implicit default)
The struct uses the implicit parameterless constructor created by C#. No explicit constructor is declared in source.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the statistic to the provided writer. The current implementation writes m_Value then m_TotalValue as doubles. The method is generic over the writer type constrained by IWriter. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the statistic from the provided reader. The method is version-aware: - If reader.context.version < Version.statisticOverflowFix: reads two ints (legacy save format) and assigns them to the fields (casts to double).
- Else if reader.context.version < Version.statisticPrecisionFix: reads two longs (intermediate legacy format) and assigns them to the fields (casts to double).
- Else: reads two doubles (current format) directly into m_Value and m_TotalValue via ref reads.
This preserves compatibility with saves made before the statistic overflow and precision fixes.
Usage Example
// Example: add and populate CityStatistic buffer on an entity (ECS):
using Unity.Entities;
using Game.City;
public void AddStatisticToEntity(EntityManager em, Entity e)
{
// Ensure the entity has the buffer
var buffer = em.GetBuffer<CityStatistic>(e);
// Add a statistic element
buffer.Add(new CityStatistic
{
m_Value = 42.5,
m_TotalValue = 1234.75
});
// Later you can update:
ref CityStatistic stat = ref buffer[0];
stat.m_Value += 1.0;
stat.m_TotalValue += stat.m_Value;
}
Additional notes:
- Because this struct implements IBufferElementData, it is intended to be used with DynamicBuffer