Skip to content

Game.Net.TrafficLights

Assembly: Assembly-CSharp
Namespace: Game.Net

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary: Represents the traffic light state for an entity in the game's ECS. Stores the current traffic light state and flags, the configured number of signal groups, the current/next signal group indices and a timer byte. Implements custom serialization/deserialization to persist compact byte-based state and supports version-aware deserialization (handles older save versions that lack the next-signal-group field).


Fields

  • public TrafficLightState m_State
    Stores the current traffic light state (enum). Serialized as a single byte.

  • public TrafficLightFlags m_Flags
    Bitflags describing additional traffic-light properties (enum). Serialized as a single byte.

  • public byte m_SignalGroupCount
    Number of signal groups defined for this traffic light. Serialized as a single byte.

  • public byte m_CurrentSignalGroup
    Index of the currently active signal group. Serialized as a single byte.

  • public byte m_NextSignalGroup
    Index of the next scheduled signal group. Serialized as a single byte when reading/writing; deserialization is version-guarded (older save versions may not include this field).

  • public byte m_Timer
    Compact timer value (tick/count) used by the traffic light logic. Serialized as a single byte.

Notes: The Serialize method writes these members in the order listed above, casting enums to byte. The Deserialize method reads bytes in the same order and casts back to the corresponding enums/fields. The NextSignalGroup field is only read when the save version is >= Version.nextLaneSignal.

Properties

  • This struct exposes no public properties. It is a plain IComponentData struct with public fields.

Constructors

  • public TrafficLights() (implicit default)
    No explicit constructors are defined. Use the default struct initializer or object initializer to create and set fields.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component data to the provided writer in a compact form:
  • Writes m_State as a byte
  • Writes m_Flags as a byte
  • Writes m_SignalGroupCount as a byte
  • Writes m_CurrentSignalGroup as a byte
  • Writes m_NextSignalGroup as a byte
  • Writes m_Timer as a byte

The method relies on the caller's writer implementation and performs explicit casts for enum fields.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the component data from the provided reader. Reading order matches Serialize. Implementation details:
  • Reads two initial bytes which are later cast to TrafficLightState and TrafficLightFlags.
  • Reads m_SignalGroupCount and m_CurrentSignalGroup.
  • Conditionally reads m_NextSignalGroup only if reader.context.version >= Version.nextLaneSignal (preserves compatibility with older save formats).
  • Reads m_Timer.
  • Finally assigns the read bytes to m_State and m_Flags via enum casts.

Important: The method expects the reader to expose a context with a version number to support backward-compatible loading.

Usage Example

// Create and set up a TrafficLights component and attach it to an entity
var lights = new Game.Net.TrafficLights
{
    m_State = TrafficLightState.Green,
    m_Flags = TrafficLightFlags.None,
    m_SignalGroupCount = 2,
    m_CurrentSignalGroup = 0,
    m_NextSignalGroup = 1,
    m_Timer = 0
};

entityManager.AddComponentData(entity, lights);

// Example of explicit serialization (writer provided by the save system)
lights.Serialize(someWriter);

// Example of deserialization (reader provided by the save system)
Game.Net.TrafficLights loaded = default;
loaded.Deserialize(someReader);
entityManager.SetComponentData(entity, loaded);

Additional notes: - TrafficLightState and TrafficLightFlags are enums defined elsewhere in the codebase; they are persisted as single bytes here. - Because this is an ECS component (IComponentData), prefer using EntityManager/Systems to read/update instances rather than modifying shared static state. - The Deserialize method includes version checking (Version.nextLaneSignal) so mods that change save format must consider incrementing/handling versioning appropriately.