Skip to content

Game.Buildings.EmergencyShelter

Assembly:
Game (assembly containing building components; used by Cities: Skylines 2 runtime and mods)

Namespace:
Game.Buildings

Type:
struct

Base:
Implements: IComponentData, IQueryTypeParameter, ISerializable

Summary:
EmergencyShelter is a small value-type ECS component used to store state for an emergency shelter building. It contains a reference to a related request Entity (m_TargetRequest) and a set of flags (m_Flags) describing the shelter's state. The component implements custom serialization/deserialization so it can be saved and loaded with compatibility checks against game save versioning (notably Version.reverseServiceRequests).


Fields

  • public Unity.Entities.Entity m_TargetRequest
    This Entity field holds a reference to a target request associated with the shelter (for example a service/request entity). During deserialization this field is only read when the save version is new enough (see Deserialize).

  • public EmergencyShelterFlags m_Flags
    A bitfield/enum storing shelter state flags. The enum type definition is not in this file; typical values indicate states such as Active, Occupied, etc. Flags are serialized as a single byte.

Properties

  • This type does not expose any C# properties. It exposes public fields and implements ISerializable for custom save/load behavior.

Constructors

  • public EmergencyShelter()
    No explicit constructor is defined in the source; the struct uses the default value-type constructor which zero-initializes fields (Entity = Entity.Null, flags = 0).

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component to the provided writer. It writes the m_TargetRequest Entity first and then writes m_Flags as a single byte. Implementation details:
  • It writes the Entity reference via writer.Write(targetRequest).
  • It casts the EmergencyShelterFlags to byte and writes that value.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads component data from the provided reader with a version check to maintain backward compatibility. Implementation details:

  • If reader.context.version >= Version.reverseServiceRequests, the code reads the target request Entity into m_TargetRequest. This conditional read preserves compatibility with older save versions that did not store this field.
  • It always reads a single byte and casts it to EmergencyShelterFlags to restore m_Flags.

Notes for modders: - The version check (Version.reverseServiceRequests) means older saves may not contain m_TargetRequest; code reading this component must account for Entity.Null where appropriate. - m_Flags are stored as a single byte; ensure your EmergencyShelterFlags enum's values fit into a byte when modifying or extending.


Usage Example

// Create and assign the component to a building entity
var shelter = new EmergencyShelter {
    m_TargetRequest = someRequestEntity, // an Entity previously created for the service request
    m_Flags = EmergencyShelterFlags.Active // example flag value
};
entityManager.AddComponentData(buildingEntity, shelter);

// Later: read and inspect flags
if (entityManager.HasComponent<EmergencyShelter>(buildingEntity))
{
    var s = entityManager.GetComponentData<EmergencyShelter>(buildingEntity);
    if ((s.m_Flags & EmergencyShelterFlags.Occupied) != 0)
    {
        // handle occupied shelter
    }
}

If you rely on serialized data across versions, remember that m_TargetRequest will only be present in saves with version >= Version.reverseServiceRequests. Always handle Entity.Null and unexpected/unknown flag values defensively.