Skip to content

Game.Economy.ResourceInfo

Assembly:
Namespace: Game.Economy

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
ResourceInfo is an ECS component used by the economy system to describe a resource entry: which resource it is, its current price, and its trade distance. It implements Colossal's ISerializable to control how the data is written/read for save/load and network operations, and it implements IQueryTypeParameter so it can be used in entity queries.


Fields

  • public Resource m_Resource
    This field identifies the resource this component refers to. EconomyUtils.GetResource and EconomyUtils.GetResourceIndex are used to map between a storage index (sbyte) and this Resource value when serializing/deserializing.

  • public float m_Price
    Current price of the resource. Written/read as a float during serialization.

  • public float m_TradeDistance
    Trade distance associated with the resource (units defined by the game). Written/read as a float during serialization.

Properties

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

Constructors

  • Implicit default constructor (public)
    There is no explicit constructor defined; instances are created with the default parameterless constructor and fields assigned directly. Example: new ResourceInfo { m_Resource = ..., m_Price = ..., m_TradeDistance = ... }.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component to the provided writer in the following order:
  • The resource index as an sbyte (obtained via EconomyUtils.GetResourceIndex(m_Resource)).
  • The price as a float (m_Price).
  • The trade distance as a float (m_TradeDistance).

Notes: - The resource is stored as a signed byte index — ensure the index range fits into sbyte to avoid overflow. - The serialization order must match the Deserialize implementation.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the component data from the provided reader in the same order written by Serialize:
  • Reads an sbyte resource index and maps it to Resource using EconomyUtils.GetResource(value).
  • Reads m_Price as a float (reads into ref float price).
  • Reads m_TradeDistance as a float (reads into ref float tradeDistance).

Notes: - Uses ref locals when reading into the struct fields to populate them directly. - Ensure compatibility between writer/reader versions; the order and types must remain consistent.

Usage Example

// Create a ResourceInfo instance and attach it to an entity (EntityManager example)
var resourceInfo = new ResourceInfo
{
    m_Resource = EconomyUtils.GetResource(0), // get Resource for index 0 (example)
    m_Price = 12.5f,
    m_TradeDistance = 1500f
};

entityManager.AddComponentData(entity, resourceInfo);

// Example of manual serialization (writer is any TWriter : IWriter)
var writer = /* obtain writer from serialization system */;
resourceInfo.Serialize(writer);

// Corresponding deserialization
var reader = /* obtain reader from serialization system */;
var loaded = new ResourceInfo();
loaded.Deserialize(reader);

Additional notes: - This component is suitable for use in ECS systems that manage economy/state for resources. - Because it implements ISerializable, it will be used by Colossal's serialization pipeline — changes to the field order or types require careful versioning/compatibility handling.