Skip to content

Game.Companies.TradeCost

Assembly:
Namespace: Game.Companies

Type: struct

Base: Implements Unity.Entities.IBufferElementData, Colossal.Serialization.Entities.ISerializable

Summary: Represents per-resource trade cost information used by company entities. Stored as a buffer element on entities, a TradeCost instance holds which resource it applies to, the buy and sell prices, and a timestamp for the last transfer request. The struct provides custom serialization/deserialization logic to persist these values via the game's serialization system.


Fields

  • public Resource m_Resource Holds the resource type this cost entry applies to (enum defined in Game.Economy). When serialized, the resource is written as a small integer (sbyte) index via EconomyUtils.GetResourceIndex and reconstructed on deserialization via EconomyUtils.GetResource.

  • public float m_BuyCost Buy price for the resource. During deserialization any NaN value will be replaced with 0f to ensure a valid price.

  • public float m_SellCost Sell price for the resource. During deserialization any NaN value will be replaced with 0f to ensure a valid price.

  • public long m_LastTransferRequestTime Timestamp (raw long) indicating when the last transfer request was made for this resource/cost entry. The concrete time unit/epoch is managed by the caller/game simulation; this field is persisted and restored by serialization.

Properties

  • None. This is a simple data struct with public fields and no managed properties.

Constructors

  • public TradeCost()
    Default parameterless struct constructor (implicit). Fields default to their language defaults: m_Resource default enum value, m_BuyCost = 0f, m_SellCost = 0f, m_LastTransferRequestTime = 0. You can initialize fields directly when creating an instance.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the struct to the provided writer in the following order:
  • Resource index as sbyte (EconomyUtils.GetResourceIndex(m_Resource))
  • m_BuyCost (float)
  • m_SellCost (float)
  • m_LastTransferRequestTime (long) This method is used by the game's persistence system (Colossal.Serialization) to save company trade cost buffers.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads fields from the reader in the same order they were written. After reading buy/sell costs it checks for NaN and clamps them to 0f if necessary to avoid invalid float values. Finally reconstructs m_Resource from the stored sbyte index via EconomyUtils.GetResource(value).

Usage Example

// Example: creating and adding a TradeCost to a company's DynamicBuffer<TradeCost>
// (assumes you have access to an EntityManager and the entity already has the buffer).

var tradeCost = new TradeCost {
    m_Resource = Game.Economy.Resource.Grain, // example resource
    m_BuyCost = 12.5f,
    m_SellCost = 9.0f,
    m_LastTransferRequestTime = /* supply appropriate simulation time or tick value */ 0L
};

// Add to a buffer on an entity (pseudo-code; adapt to your system context)
var buffer = entityManager.GetBuffer<TradeCost>(companyEntity);
buffer.Add(tradeCost);

// Notes:
// - This struct is intended to be stored in an ECS DynamicBuffer on company entities.
// - On deserialization NaN buy/sell values are converted to 0 to ensure data integrity.
// - Resource values are serialized as sbyte indices; EconomyUtils maps between index and enum.