Game.Buildings.Battery
Assembly: Assembly-CSharp
Namespace: Game.Buildings
Type: struct
Base: IComponentData, IQueryTypeParameter, ISerializable
Summary:
Represents a building-mounted battery energy storage component used by the game's power system. Stores the amount of energy currently held (m_StoredEnergy), the battery capacity (m_Capacity), and a historical/diagnostic flow value (m_LastFlow). Provides a convenience property storedEnergyHours that converts the internal stored energy units into hours by dividing by 85 (integer truncation). The struct also implements custom binary serialization/deserialization with version checks so older save formats remain compatible.
Fields
-
public long m_StoredEnergy
Holds the battery's stored energy in the game's internal units (stored as a 64-bit integer). This value is used to compute storedEnergyHours by dividing by 85. When deserializing, this field is always read from the stream. -
public int m_Capacity
Maximum capacity of the battery in the game's integer units. This field is read during deserialization only if the save/version context includes Version.batteryStats; otherwise it remains the default value. -
public int m_LastFlow
An integer representing the last measured energy flow (diagnostic/history). This field is read during deserialization only if the save/version context includes Version.batteryLastFlow; otherwise it remains the default value.
Properties
public int storedEnergyHours => (int)(m_StoredEnergy / 85)
Computed read-only property converting internal stored energy into "hours" by dividing m_StoredEnergy by 85 and truncating to an int. Useful for UI and gameplay logic that needs a human-friendly duration estimate rather than raw internal units.
Constructors
public Battery()
No explicit constructor is defined in source; the struct relies on the default parameterless constructor. All fields default to 0 unless explicitly assigned.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Serializes the component to the provided writer in the following order: m_StoredEnergy (long), m_Capacity (int), m_LastFlow (int). This method writes all three fields unconditionally so newer save formats will include the full state. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Deserializes the component from the provided reader. Reading behavior is version-gated: - Always reads m_StoredEnergy.
- Reads m_Capacity only if reader.context.version >= Version.batteryStats.
- Reads m_LastFlow only if reader.context.version >= Version.batteryLastFlow. If a field is not present in an older save (because its version is older than the gated constant), the corresponding field remains at its default value (0).
Usage Example
// Constructing and using a Battery instance
Game.Buildings.Battery b = new Game.Buildings.Battery();
b.m_StoredEnergy = 8500L; // internal units
b.m_Capacity = 10000;
b.m_LastFlow = 120;
// Convert to hours (integer truncation)
int hours = b.storedEnergyHours; // = (int)(8500 / 85) == 100
// Serializing (pseudo-code; requires an IWriter implementation)
void SaveBattery<TWriter>(TWriter writer, ref Game.Buildings.Battery battery) where TWriter : IWriter
{
battery.Serialize(writer);
}
// Deserializing (pseudo-code; requires an IReader implementation and proper reader.context.version)
Game.Buildings.Battery LoadBattery<TReader>(TReader reader) where TReader : IReader
{
Game.Buildings.Battery battery = new Game.Buildings.Battery();
battery.Deserialize(reader);
return battery;
}
Notes: - The division by 85 in storedEnergyHours reflects the game's internal conversion factor; it performs integer division so fractional hours are discarded. - Deserialize uses version checks (Version.batteryStats and Version.batteryLastFlow) to remain compatible with older save formats; ensure the reader.context.version is set correctly when loading saves.