Skip to content

Game.Prefabs.ResourceStack

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: struct

Base: Colossal.Serialization.Entities.ISerializable

Summary:
Represents a stack of a single resource type and its quantity used by prefabs. Implements ISerializable so instances can be written to and read from Colossal's serialization streams. The resource is serialized as an sbyte index (via EconomyUtils.GetResourceIndex) and the amount as an int.


Fields

  • public Resource m_Resource
    Holds the resource type for this stack. The resource is (de)serialized by converting to/from an sbyte index using EconomyUtils.

  • public int m_Amount
    The quantity of the resource in this stack. Serialized/deserialized as a 32-bit integer.

Properties

  • This type exposes no public properties.

Constructors

  • implicit public ResourceStack()
    Default parameterless struct constructor (value-initialized). No explicit constructors are defined in the source.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes this ResourceStack to the provided writer. Implementation details:
  • Writes the resource index as an sbyte using EconomyUtils.GetResourceIndex(m_Resource).
  • Writes m_Amount as an int.
  • Note: resource index is cast to sbyte, so the resource index must fit within sbyte range.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes this ResourceStack from the provided reader. Implementation details:

  • Reads an sbyte value representing the resource index and converts it to a Resource via EconomyUtils.GetResource(value).
  • Reads the amount into m_Amount (int).
  • Uses ref assignment when reading the amount to directly populate the field.

Usage Example

// Create and serialize
var stack = new ResourceStack {
    m_Resource = EconomyUtils.GetResource(0), // example resource
    m_Amount = 150
};
stack.Serialize(writer); // writer implements IWriter

// Deserialize
var loaded = default(ResourceStack);
loaded.Deserialize(reader); // reader implements IReader
// loaded.m_Resource and loaded.m_Amount are now populated

Additional notes: - EconomyUtils.GetResourceIndex and EconomyUtils.GetResource are used for mapping between Resource and its serialized index; ensure these mappings remain consistent across versions. - Because the resource index is stored as sbyte, the number of resources must be within the sbyte range (typically -128..127). In practice indexes should be non-negative and fit inside that range.