Skip to content

Game.BatterySection

Assembly: Assembly-CSharp
Namespace: Game.UI.InGame

Type: class

Base: InfoSectionBase

Summary:
UI info section used by the in-game inspector to show battery statistics for the currently selected battery building. The section reads both instance component data (Game.Buildings.Battery) and the battery prefab data (BatteryData) via the base helper TryGetComponentWithUpgrades, then exposes values such as current stored charge, total capacity, last flow and a computed remaining time value. It also controls its own visibility based on whether the selected entity has a Battery component.


Fields

  • This class does not declare explicit private fields; it uses private auto-properties (listed in Properties) to hold its state.

Properties

  • protected override string group => "BatterySection"
    Identifier used by the base InfoSection system to group/identify this section in the UI.

  • private int batteryCharge { get; set; }
    Current stored charge in display units (set from Game.Buildings.Battery.storedEnergyHours).

  • private int batteryCapacity { get; set; }
    Battery capacity read from the BatteryData prefab (data.m_Capacity).

  • private int flow { get; set; }
    The last measured flow for this battery (set from componentData.m_LastFlow). Positive means charging, negative means discharging.

  • private float remainingTime { get; set; }
    Computed remaining time in hours (float). Calculated from stored energy, capacity, and flow and clamped to a maximum value (see OnProcess).

Constructors

  • public BatterySection()
    Default constructor. Marked with [Preserve] in source to avoid stripping by Unity/IL2CPP linker. No custom initialization is performed here.

Methods

  • protected override void Reset()
    Resets internal state values to defaults (zeros). Called by the lifecycle of the info section when needed to clear cached/displayed values.

  • private bool Visible()
    Returns true when the currently selected entity has a Game.Buildings.Battery component (uses base.EntityManager.HasComponent(selectedEntity)). Used to decide whether this section should be shown.

  • [Preserve] protected override void OnUpdate()
    Called regularly by the UI system. Sets base.visible to the result of Visible() so the section appears/disappears based on the selected entity.

  • protected override void OnProcess()
    Core update routine that gathers data for the UI:

  • Uses TryGetComponentWithUpgrades(selectedEntity, selectedPrefab, out var data) to obtain the prefab data (including capacityTicks and m_Capacity) taking upgrades into account.
  • Reads the instance component data via EntityManager.GetComponentData(selectedEntity).
  • Updates batteryCharge, batteryCapacity and flow fields from component + prefab data.
  • Computes remainingTime:

    • If flow > 0 (net charging): remaining ticks to full = (data.capacityTicks - componentData.m_StoredEnergy) / flow.
    • If flow < 0 (net discharging): remaining ticks to empty = componentData.m_StoredEnergy / -flow.
    • Converts ticks to hours by dividing by 2048f (game tick conversion used by the game) and clamps the result to a max of 12f (the UI cap).
    • If flow == 0: remainingTime = 0f. Notes: The 2048f divider and clamping to 12f mirror the game’s internal conversion and UI limits.
  • public override void OnWriteProperties(IJsonWriter writer)
    Serializes the current display values into the provided IJsonWriter using the following property names:

  • "batteryCharge" -> batteryCharge
  • "batteryCapacity" -> batteryCapacity
  • "flow" -> flow
  • "remainingTime" -> remainingTime
    This method is used to send the values to the UI layer (JSON-based binding).

Usage Example

// Example showing how the section updates and writes out its properties.
// In the actual game this lifecycle is managed by the UI/InfoSection system.

[Preserve]
protected override void OnUpdate()
{
    base.visible = Visible();
}

// After the framework calls OnProcess(), the UI layer asks the section to serialize:
IJsonWriter writer = /* obtained from UI framework */;
BatterySection section = new BatterySection();
// Normally the framework invokes section.OnProcess() each frame when appropriate.
section.OnWriteProperties(writer);
// JSON now contains:
// { "batteryCharge": ..., "batteryCapacity": ..., "flow": ..., "remainingTime": ... }