Skip to content

Game.Buildings.MailProducer

Assembly:
Namespace: Game.Buildings

Type: struct

Base: Implements IComponentData, IQueryTypeParameter, ISerializable

Summary:
Represents the mail-producing state for a building in Cities: Skylines 2. This component stores an Entity reference for an active mail request, counts for sending/receiving mail, a dispatch index byte, and encodes a "mail delivered" flag inside the high bit of the receiving-mail ushort. It supports serialization and conditional deserialization of the dispatch index based on the saved-game/version context.


Fields

  • public Entity m_MailRequest
    Reference to an Entity that represents the building's current mail request (if any). Serialized/deserialized as the first field in the component's data layout.

  • public ushort m_SendingMail
    Unsigned 16-bit value representing the amount (or id/index) of sending mail. Serialized as the second field.

  • public ushort m_ReceivingMail
    Encodes both the receiving mail value and a delivery flag in a single ushort:

  • Lower 15 bits (0x7FFF) hold the receivingMail value (0..32767).
  • High bit (0x8000) is used as the mailDelivered flag. Serialized as the third field.

  • public byte m_DispatchIndex
    Small index/byte related to dispatching mail requests. This field is serialized, but when deserializing it is only read if the saved-game version supports it (see Deserialize behavior).

Properties

  • public int receivingMail { get; set; }
    Gets or sets the receiving-mail value using only the lower 15 bits of m_ReceivingMail. The getter returns (m_ReceivingMail & 0x7FFF). The setter preserves the high bit and writes the provided value into the low 15 bits. Use this to read/write the numeric receiving-mail count without affecting the delivered flag.

  • public bool mailDelivered { get; set; }
    Gets or sets the high-bit flag stored in m_ReceivingMail. The getter returns true when (m_ReceivingMail & 0x8000) != 0. The setter sets or clears the high bit (0x8000) while preserving the lower 15 bits. Use this to check whether the mail has been marked delivered.

Constructors

  • public MailProducer()
    Default value-type constructor (implicit). The struct relies on field defaults; no custom constructor is defined in the source. When created manually, ensure fields are initialized to the desired values before use.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component data into the provided writer in the following order:
  • m_MailRequest (Entity)
  • m_SendingMail (ushort)
  • m_ReceivingMail (ushort)
  • m_DispatchIndex (byte)
    The method uses the generic IWriter interface so the exact serialization format is determined by the writer implementation used by the game.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the component data from the provided reader in the same order as Serialize. For m_DispatchIndex, the method checks the reader.context.version and only reads dispatchIndex when the version is >= Version.requestDispatchIndex. This ensures backward compatibility with older save formats that did not include the dispatch index.

Usage Example

// Creating and initializing a MailProducer component manually
var mailProducer = new Game.Buildings.MailProducer();
mailProducer.m_MailRequest = Entity.Null;     // no active request yet
mailProducer.m_SendingMail = 10;              // example sending count
mailProducer.receivingMail = 5;               // stores 5 in low 15 bits
mailProducer.mailDelivered = false;           // high bit cleared
mailProducer.m_DispatchIndex = 0;             // default dispatch index

// Mark mail delivered while preserving receiving count:
mailProducer.mailDelivered = true;

// Reading encoded fields:
int recv = mailProducer.receivingMail;        // 5
bool delivered = mailProducer.mailDelivered;  // true

// Serialization (example usage - writer comes from game's serialization system):
// writer.Write(...) calls are done by the game's writer implementation.
// mailProducer.Serialize(writer);

// Deserialization respects versioning: dispatch index is only read
// when reader.context.version >= Version.requestDispatchIndex
// mailProducer.Deserialize(reader);

{{ Additional details: - The m_ReceivingMail packing into a single ushort is important to maintain memory efficiency for component storage in ECS. - When modifying receivingMail, ensure values stay within 0..32767 to avoid overwriting the delivered flag. - The Version.requestDispatchIndex check relies on the game's Version enum / versioning system exposed via reader.context; mods should not assume the presence of m_DispatchIndex in older saves. - This struct is a pure data component (IComponentData) and is intended to be used with Unity ECS systems for mail-related behavior. }}