Skip to content

Game.Prefabs.ConsumptionData

Assembly: Assembly-CSharp (Game)
Namespace: Game.Prefabs

Type: struct ConsumptionData

Base: IComponentData, IQueryTypeParameter, ICombineData, ISerializable

Summary:
Represents a prefab's resource consumption and upkeep values used by the simulation/entity systems. This struct is used when converting building prefabs into ECS entities: it can add the required consumer/producer components to an archetype, combine consumption data from multiple sources (e.g., when composing complex prefabs), and serialize/deserialize the values for save/load. The fields typically describe per-prefab/per-simulation-update resource usage and upkeep; exact units and update frequency are determined by the surrounding simulation systems.


Fields

  • public int m_Upkeep
    Represents the monetary upkeep cost for the prefab (integer). Used by economy/upkeep systems and summed when combining multiple consumption data entries.

  • public float m_ElectricityConsumption
    Electricity demand for the prefab. If greater than 0, AddArchetypeComponents will add an ElectricityConsumer component to the entity archetype.

  • public float m_WaterConsumption
    Water demand for the prefab. If greater than 0, AddArchetypeComponents will add a WaterConsumer component to the entity archetype.

  • public float m_GarbageAccumulation
    Garbage produced/accumulated by the prefab. If greater than 0, AddArchetypeComponents will add a GarbageProducer component to the entity archetype.

  • public float m_TelecomNeed
    Telecommunications/telecom demand for the prefab. When combining ConsumptionData entries, telecom need uses the maximum value (not additive). Deserialized conditionally depending on save version (backwards compatibility).

Properties

  • None (this struct exposes public fields; no C# properties).

Constructors

  • public ConsumptionData()
    Default value-type constructor (provided implicitly). Initialize fields manually in code or via deserialization.

Methods

  • public void AddArchetypeComponents(HashSet<ComponentType> components)
    Adds required ECS component types to the provided archetype component set based on non-zero consumption fields:
  • Adds ElectricityConsumer if m_ElectricityConsumption > 0.
  • Adds WaterConsumer if m_WaterConsumption > 0.
  • Adds GarbageProducer if m_GarbageAccumulation > 0.
  • Adds TelecomConsumer if m_TelecomNeed > 0. This is used when building an entity archetype for a prefab so the resulting entity contains only the consumer/producer components it needs.

  • public void Combine(ConsumptionData otherData)
    Merges another ConsumptionData into this one:

  • Sums m_Upkeep, m_ElectricityConsumption, m_WaterConsumption, and m_GarbageAccumulation.
  • Sets m_TelecomNeed to the maximum of the two values (math.max). Useful when aggregating consumption for composite prefabs or combined building pieces.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the fields to a writer in a fixed order: m_Upkeep, m_ElectricityConsumption, m_WaterConsumption, m_GarbageAccumulation, m_TelecomNeed. The order must match Deserialize for compatibility.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads fields from a reader in the same order they were written. For backwards compatibility, m_TelecomNeed is read only when reader.context.version > Version.telecomNeed (i.e., older save versions that predate telecomNeed will not contain that field). Use caution: maintain read/write order and version checks to preserve save compatibility.

Usage Example

// Example: creating and combining consumption data for a prefab, and adding archetype components.
var baseData = new ConsumptionData
{
    m_Upkeep = 50,
    m_ElectricityConsumption = 10f,
    m_WaterConsumption = 2f,
    m_GarbageAccumulation = 0.5f,
    m_TelecomNeed = 1f
};

var extensionData = new ConsumptionData
{
    m_Upkeep = 10,
    m_ElectricityConsumption = 5f,
    m_WaterConsumption = 0f,
    m_GarbageAccumulation = 0.2f,
    m_TelecomNeed = 0.8f
};

// Combine extension into base (sums most values, telecom uses max)
baseData.Combine(extensionData);

// Build archetype component set based on resulting consumption
var components = new HashSet<ComponentType>();
baseData.AddArchetypeComponents(components);

// Serialization example (writer must implement IWriter)
// baseData.Serialize(writer);

// Deserialization example (reader must implement IReader and supply context.version)
// var loaded = new ConsumptionData();
// loaded.Deserialize(reader);