Skip to content

Game.PoliceCar

Assembly: Game
Namespace: Game.Vehicles

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
PoliceCar is an ECS component that represents a police vehicle's runtime state in the Cities: Skylines 2 game code. It stores the entity reference for an active service request target, the car's state flags, bookkeeping values used for request handling and path timing, shift timing estimates, and a mask of purposes that the vehicle can fulfill. The struct implements ISerializable to control how its fields are written to and read from saved data, with conditional deserialization behavior depending on game version compatibility.


Fields

  • public Entity m_TargetRequest
    Stores the Entity representing the current service request target (if any). During serialization this Entity is written; during deserialization the value is read only for save versions that include reverseServiceRequests2 (older versions will not populate this field on load).

  • public PoliceCarFlags m_State
    Flags describing the current state of the police car. Serialized as a uint value.

  • public int m_RequestCount
    Count of outstanding requests or a counter used by the vehicle's request handling logic. Serialized as an int.

  • public float m_PathElementTime
    Time spent (or time marker) related to path elements — used for path traversal/timing logic. Serialized as a float.

  • public uint m_ShiftTime
    Shift time stored as an unsigned int. Serialized as a uint.

  • public uint m_EstimatedShift
    Estimated shift time (unsigned int). This field is serialized, but on deserialization it is only read when the save version is at least policeShiftEstimate. For older saves it remains at the default (0) unless otherwise set.

  • public PolicePurpose m_PurposeMask
    Mask indicating which police purposes this vehicle can perform (e.g., Patrol, Emergency, etc.). In serialization this is written as an int. On deserialization this value is only read when the save version is at least policeImprovement3; for earlier saves the mask defaults to PolicePurpose.Patrol | PolicePurpose.Emergency.

Properties

  • (none)
    This struct exposes no managed properties — all state is in public fields. It does, however, implement IQueryTypeParameter to be usable directly in some ECS query contexts.

Constructors

  • public PoliceCar(PoliceCarFlags flags, int requestCount, PolicePurpose purposeMask)
    Initializes a new PoliceCar instance. Sets m_TargetRequest to Entity.Null, applies the provided flags and requestCount, zeroes timing fields (m_PathElementTime, m_ShiftTime, m_EstimatedShift) and sets m_PurposeMask to the provided mask.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component data into the provided writer in the following order:
  • m_TargetRequest (Entity)
  • m_State (cast to uint)
  • m_RequestCount (int)
  • m_PathElementTime (float)
  • m_ShiftTime (uint)
  • m_EstimatedShift (uint)
  • m_PurposeMask (cast to int)

The method uses the writer interface and writes raw primitive representations for enums and numeric types.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads component data from the provided reader. Deserialization behavior is version-aware:
  • If reader.context.version >= Version.reverseServiceRequests2, reads m_TargetRequest (Entity).
  • Reads a uint value (this is the serialized m_State) and holds it until other fields are read.
  • Reads m_RequestCount (int).
  • Reads m_PathElementTime (float).
  • Reads m_ShiftTime (uint).
  • If reader.context.version >= Version.policeShiftEstimate, reads m_EstimatedShift (uint).
  • Sets m_State by casting the earlier-read uint to PoliceCarFlags.
  • If reader.context.version >= Version.policeImprovement3, reads an int and casts it to PolicePurpose for m_PurposeMask. Otherwise sets m_PurposeMask = PolicePurpose.Patrol | PolicePurpose.Emergency (backwards-compatibility default).

Note: Type constraints require TReader to implement IReader and make use of reader.context.version for conditional handling.

Usage Example

// Create a new PoliceCar component and add it to an entity via an EntityManager (example)
var policeCar = new PoliceCar(PoliceCarFlags.None, 0, PolicePurpose.Patrol | PolicePurpose.Emergency);
policeCar.m_PathElementTime = 0f;
policeCar.m_ShiftTime = 0u;
policeCar.m_EstimatedShift = 0u;
policeCar.m_TargetRequest = Entity.Null;

// Example: add component data to an existing entity
entityManager.AddComponentData(entity, policeCar);

// The component's Serialize/Deserialize methods are used by the game's save/load system
// and typically do not need to be called manually.

Additional notes: - The serialization format is explicit and version-dependent; when extending or modifying this struct be careful to preserve field ordering and version-conditional reads/writes to maintain save compatibility. - PoliceCarFlags and PolicePurpose are enums used to drive logic elsewhere — consult their definitions to understand specific flag meanings.