Skip to content

Game.Creatures.Divert

Assembly: Assembly-CSharp
Namespace: Game.Creatures

Type: struct

Base: System.ValueType, IComponentData, IQueryTypeParameter, ISerializable

Summary:
Divert is an ECS component used by creature-related systems to mark an entity that diverts or redirects resources/behavior toward a target entity. It stores the target entity, the kind of resource involved, an extra integer data field for custom use, and a Purpose value (from Game.Citizens.Purpose) describing the intent. The struct implements custom binary serialization/deserialization and includes version-aware logic to maintain backward compatibility with older save formats.


Fields

  • public Entity m_Target
    The Entity that is the target of the divert action. This is the primary reference used by systems processing Divert components. Serialized first in the saved stream.

  • public Resource m_Resource
    The resource type being diverted (Game.Economy.Resource). Stored and restored via EconomyUtils.GetResource / EconomyUtils.GetResourceIndex as an sbyte in the serialized data. Only present in serialized data when the save version enables diverted resources (see Deserialize version check).

  • public int m_Data
    An integer data field for additional per-divert information. Its meaning is context-dependent and used by game logic/systems that read this component. This value is serialized (for versions with divertResources).

  • public Game.Citizens.Purpose m_Purpose
    Purpose enum describing the reason/intent for the divert. Serialized as a single byte immediately after the target entity.

Properties

  • None.

Constructors

  • public Divert()
    Default value-type constructor (implicit for structs). Fields default to Entity.Null (m_Target), default Resource (m_Resource), 0 (m_Data), and default Purpose (m_Purpose).

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes the Divert into the provided writer in the following order:
  • m_Target (Entity)
  • m_Purpose cast to a byte
  • m_Data (int)
  • m_Resource index as sbyte (EconomyUtils.GetResourceIndex(m_Resource))
    Note: The code writes m_Data and m_Resource unconditionally; however, compatibility with older versions is handled on read (see Deserialize).

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes the Divert from the provided reader. Reads:

  • m_Target (Entity)
  • a byte -> m_Purpose (cast to Game.Citizens.Purpose)
    For backward compatibility with older save versions, m_Data and m_Resource are only read if the reader context version is >= Version.divertResources. If the version check fails, m_Data and m_Resource remain at their defaults. Resource value is read as sbyte and converted via EconomyUtils.GetResource(value).

Notes: - The generic reader/writer types are constrained to IReader / IWriter interfaces used by the game's serialization system. - The version check uses reader.context.version and Version.divertResources to maintain compatibility with saves prior to the introduction of resource-aware diverts.

Usage Example

using Colossal.Serialization.Entities;
using Game.Citizens;
using Game.Economy;
using Unity.Entities;
using Game.Creatures;

// create a Divert component and assign fields
Divert divert = new Divert
{
    m_Target = someEntity,                   // an Entity reference
    m_Purpose = Game.Citizens.Purpose.Work,  // example purpose
    m_Data = 42,                             // example usage-specific data
    m_Resource = Resource.Food               // example resource
};

// Serialization (pseudo-code - depends on game's writer implementation)
using (var writer = GetGameWriter())
{
    divert.Serialize(writer);
}

// Deserialization (pseudo-code - depends on game's reader implementation)
Divert loaded = new Divert();
using (var reader = GetGameReader())
{
    loaded.Deserialize(reader);
}
// After Deserialize, loaded will contain the same fields if the save version supports them.

Additional notes: - As an IComponentData, Divert is intended to be attached to entities managed by the Unity.Entities (ECS) systems used in the game. - EconomyUtils.GetResource and EconomyUtils.GetResourceIndex handle mapping between Resource enum values and the compact sbyte index used in serialization. Ensure compatibility when introducing new resources or custom mod resources.