Skip to content

Game.NaturalResourceCell

Assembly: Game (in-game code)
Namespace: Game.Simulation

Type: struct

Base: System.ValueType (implements IStrideSerializable, ISerializable)

Summary:
Represents the natural resource quantities for a single simulation cell (fertility, ore, oil, fish). Contains serialization helpers for saving/loading with Colossal.Serialization and convenience accessors returning resource values as a Unity.Mathematics.float4. Note: this struct depends on NaturalResourceAmount, Context, FormatTags, IWriter/IReader and Unity.Mathematics.float4.


Fields

  • public NaturalResourceAmount m_Fertility
    Holds fertility resource amounts (base and used) for the cell.

  • public NaturalResourceAmount m_Ore
    Holds ore resource amounts (base and used) for the cell.

  • public NaturalResourceAmount m_Oil
    Holds oil resource amounts (base and used) for the cell.

  • public NaturalResourceAmount m_Fish
    Holds fish resource amounts (base and used) for the cell. Note: fish serialization is conditional (see Deserialize).

Properties

  • (none)

Constructors

  • (default struct constructor)
    Uses default value semantics for a struct. No explicit constructors are defined in the source.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes each NaturalResourceAmount field (fertility, ore, oil, fish) by writing them to the provided writer. The method writes all four amounts unconditionally.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes resource amounts from the reader into the fields. Fertility, ore and oil are always read. Fish is only read if reader.context.format.Has(FormatTags.FishResource) is true — this preserves backward compatibility with older save formats that did not include fish.

  • public int GetStride(Context context)
    Returns the combined stride (serialized size) for fertility, ore and oil by summing each NaturalResourceAmount.GetStride(context). Note: this method does not include m_Fish in the stride calculation — likely intentional for older formats or a potential omission depending on use.

  • public float4 GetBaseResources()
    Returns base resource values as a float4 in the order: fertility, ore, oil, fish. Each component is cast from the respective NaturalResourceAmount.m_Base.

  • public float4 GetUsedResources()
    Returns used resource values as a float4. As implemented, it returns (fertility.m_Used, ore.m_Used, oil.m_Used, oil.m_Used) — the fourth component incorrectly repeats oil.m_Used instead of fish.m_Used. This appears to be a bug; the expected behavior is to return fish.m_Used for the fourth component.

Usage Example

// Example: create a cell, inspect resources and serialize it.
using Unity.Mathematics;
using Colossal.Serialization.Entities;

public void Example()
{
    NaturalResourceCell cell = new NaturalResourceCell();

    // Initialize resource amounts (NaturalResourceAmount constructors/properties assumed)
    // cell.m_Fertility = new NaturalResourceAmount(base:10, used:2);
    // cell.m_Ore = new NaturalResourceAmount(base:5, used:1);
    // cell.m_Oil = new NaturalResourceAmount(base:3, used:0);
    // cell.m_Fish = new NaturalResourceAmount(base:4, used:1);

    float4 baseResources = cell.GetBaseResources();
    float4 usedResources = cell.GetUsedResources(); // NOTE: current implementation repeats oil in W component

    // Serialize with a writer (IWriter implementation provided by the game's serialization system)
    // var writer = ...;
    // cell.Serialize(writer);
}

Notes and recommendations: - Be mindful of the conditional fish serialization in Deserialize — when adding support for fish ensure the save format tag FormatTags.FishResource is used. - Consider fixing GetUsedResources to return fish.m_Used for the fourth component: float4 GetUsedResources() => new float4((int)m_Fertility.m_Used, (int)m_Ore.m_Used, (int)m_Oil.m_Used, (int)m_Fish.m_Used); - If fish needs to be included in stride calculations for newer formats, update GetStride to include m_Fish.GetStride(context) when appropriate.