Skip to content

Game.Simulation.NaturalResourceAmount

Assembly:
Namespace: Game.Simulation

Type: struct

Base: System.ValueType, Colossal.Serialization.Entities.IStrideSerializable, Colossal.Serialization.Entities.ISerializable

Summary:
Represents a small, fixed-size record of a natural resource's quantities used by the simulation. It stores the total/base amount and the currently used amount as 16-bit unsigned integers and provides simple binary (stride) serialization and deserialization implementations. Commonly used when packing resource counts into compact saved/transfer formats.


Fields

  • public ushort m_Base
    Base/available amount of the natural resource. Stored as a 16-bit unsigned integer. Represents the total or maximum quantity tracked by this record.

  • public ushort m_Used
    Amount of the natural resource currently used/consumed. Stored as a 16-bit unsigned integer. Typically expected to be <= m_Base.

Properties

  • None

Constructors

  • public NaturalResourceAmount()
    No explicit constructors are declared in the source; the type relies on the default parameterless struct constructor. Default-initialized fields will be 0 (m_Base = 0, m_Used = 0).

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the struct fields to the provided writer in binary order: first m_Base, then m_Used. Each value is written as a ushort (2 bytes). This method is used when packing the struct into a stride/stream for save or network.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the struct fields from the provided reader into m_Base and m_Used (reads in the same order they were written). Uses ref locals to assign the read values directly into the struct fields.

  • public int GetStride(Context context)
    Returns the byte size required to serialize this struct: 4 (2 bytes for m_Base + 2 bytes for m_Used). The Context parameter is unused in the implementation but provided by the stride-serialization interface.

Usage Example

// Create and initialize
Game.Simulation.NaturalResourceAmount amount = new Game.Simulation.NaturalResourceAmount();
amount.m_Base = 100;
amount.m_Used = 25;

// Serialize (pseudo-code; actual writer depends on game's serialization API)
someWriter.Write(amount.m_Base);
someWriter.Write(amount.m_Used);

// Or using the struct's Serialize method:
amount.Serialize(someWriter);

// Deserialize into a struct instance (pseudo-code)
Game.Simulation.NaturalResourceAmount readAmount = default;
readAmount.Deserialize(someReader);

// After deserialization:
ushort baseValue = readAmount.m_Base;
ushort usedValue = readAmount.m_Used;

{{ This struct is intentionally minimal and fixed-size to keep serialized data compact and predictable. Because fields are public and are plain value types, there is no internal validation (for example, ensuring m_Used <= m_Base); callers should enforce any invariants they require. The GetStride method returns a constant 4, matching two ushorts (2 bytes each). The generic Serialize/Deserialize methods rely on the game's IWriter/IReader abstractions (from Colossal.Serialization.Entities). }}