Skip to content

Game.Buildings.CommercialProperty

Assembly:
Assembly-CSharp

Namespace: Game.Buildings

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary: Represents a commercial-property component used with Unity Entities (ECS) in Cities: Skylines 2. The component stores the type of resource associated with the commercial property (the Resource enum from Game.Economy) and implements the game's serialization interface so it can be saved/loaded. It can also be used as a query type parameter in ECS queries.

This struct writes the underlying enum value as a uint when serialized and casts it back when deserializing. Modders should be aware of enum value changes between versions, as serialization writes the numeric value directly.

Fields

  • public Resource m_Resources Stores the resource type for this commercial property (uses the Resource enum from Game.Economy). This value is serialized as an unsigned integer. When used as an ECS component, this field can be read/updated via EntityManager or SystemBase methods (e.g., GetComponentData / SetComponentData). Ensure enum compatibility across game versions to avoid incorrect mapping when loading saved data.

Properties

  • None (no properties defined on this struct)

Constructors

  • public CommercialProperty()
    Implicit default struct constructor is available and initializes m_Resources to the default enum value (typically the enum's zero value). No explicit constructor is defined in the source.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component data to the provided writer. Implementation converts the Resource enum to its underlying uint and calls writer.Write((uint)m_Resources). This is the form used by the game's serialization system for saving component state.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads data back from the provided reader. Implementation reads a uint from the reader and casts it to Resource: reader.Read(out uint value); m_Resources = (Resource)value;. This assumes the serialized value matches a valid Resource enum value.

Notes and best practices: - Serialization writes the numeric value of the enum. If the Resource enum is extended or reordered in future updates, saved values may map to different enum members — consider versioning or migration if creating long-term persistent data. - Both Serialize and Deserialize are generic to accept the game's writer/reader types (IWriter / IReader). They must remain consistent with expected read/write order when combining multiple component serializations.

Usage Example

// Adding the component to an entity (via EntityManager)
var commercial = new CommercialProperty { m_Resources = Resource.Goods };
entityManager.AddComponentData(entity, commercial);

// Reading/updating in a SystemBase
public partial class ExampleSystem : SystemBase
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref CommercialProperty cp) =>
        {
            if (cp.m_Resources == Resource.None)
            {
                cp.m_Resources = Resource.Goods;
            }
        }).ScheduleParallel();
    }
}

// Manually using the serialization methods (example)
void SaveComponent<TWriter>(TWriter writer, CommercialProperty cp) where TWriter : IWriter
{
    cp.Serialize(writer);
}

void LoadComponent<TReader>(TReader reader, out CommercialProperty cp) where TReader : IReader
{
    cp = new CommercialProperty();
    cp.Deserialize(reader);
}

{{ Additional info: - Implements IQueryTypeParameter to allow using CommercialProperty as a query parameter in ECS job queries. - The Resource enum is defined in Game.Economy; consult that enum for the valid values you can assign. - Because this is a struct (value type), be mindful when reading/modifying it from systems — use ref when you need to modify the component in-place to avoid copying. }}