Skip to content

Game.Prefabs.ResourceProductionData

Assembly: Assembly-CSharp
Namespace: Game.Prefabs

Type: struct

Base: System.ValueType, Unity.Entities.IBufferElementData, Colossal.Serialization.Entities.ISerializable

Summary:
Represents a single resource production entry used by prefabs/entities. Holds the resource type, its production rate and storage capacity. The struct is a buffer element (IBufferElementData) with an internal buffer capacity attribute of 0 and also implements the game's ISerializable interface to support save/load. It includes a helper Combine method to merge production entries by resource type.


Fields

  • public Resource m_Type
    The resource type (enum/identifier) this entry refers to. Mapped to/from a small integer index during serialization using EconomyUtils.

  • public int m_ProductionRate
    The production rate for this resource (units per some game time interval).

  • public int m_StorageCapacity
    The storage capacity allocated for this resource at the producing prefab/entity.

Properties

  • (none)

Constructors

  • public ResourceProductionData(Resource type, int productionRate, int storageCapacity)
    Creates a new ResourceProductionData instance with the given resource type, production rate and storage capacity.

Methods

  • public static void Combine(NativeList<ResourceProductionData> resources, DynamicBuffer<ResourceProductionData> others)
    Merges entries from the DynamicBuffer 'others' into the NativeList 'resources'. For each entry in 'others' it searches 'resources' for an existing entry with the same m_Type; if found it adds the production rate and storage capacity to the existing entry, otherwise it appends the new entry. Use this to aggregate resource production entries and avoid duplicate types.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes this entry into the provided writer. Writes m_ProductionRate, m_StorageCapacity, then writes the resource as a signed byte index obtained via EconomyUtils.GetResourceIndex(m_Type). The sbyte storage implies the resource index set is expected to fit in a signed byte.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes fields from the reader into this instance. Reads m_ProductionRate and m_StorageCapacity, then reads an sbyte resource index and resolves it back to a Resource value via EconomyUtils.GetResource(index).

Usage Example

// Add a resource production entry to an entity buffer
var data = new ResourceProductionData(Resource.Wood, productionRate: 10, storageCapacity: 100);
var buffer = entityManager.GetBuffer<ResourceProductionData>(entity);
buffer.Add(data);

// Combine a DynamicBuffer into a NativeList for aggregation
NativeList<ResourceProductionData> aggregated = new NativeList<ResourceProductionData>(Allocator.Temp);
DynamicBuffer<ResourceProductionData> prefabBuffer = entityManager.GetBuffer<ResourceProductionData>(somePrefabEntity);
ResourceProductionData.Combine(aggregated, prefabBuffer);

// Serialization is handled by the ISerializable implementation when the game's save system runs.
// The struct writes productionRate, storageCapacity and a small resource index (sbyte).

{{ This struct is intended to be attached as a dynamic buffer on prefab/entity archetypes that produce resources. Use Combine when you need a compact aggregated view (one entry per resource type). Note the [InternalBufferCapacity(0)] attribute — buffers will start empty and allocate storage as needed. Serialization stores the resource as a small signed byte index via EconomyUtils, so custom Resource enums must be compatible with that mapping. }}