Skip to content

Game.Prefabs.TrafficSpawnerData

Assembly:
Assembly-CSharp

Namespace:
Game.Prefabs

Type:
struct

Base:
IComponentData, IQueryTypeParameter, ISerializable

Summary:
TrafficSpawnerData is a lightweight ECS component used by prefab traffic spawners. It stores spawn configuration (spawn rate), the type of road/track the spawner is associated with, and a flag that disables spawning of slow vehicles. The struct implements ISerializable to control binary serialization order and format (used by the game's save/load and prefab serialization systems) and IQueryTypeParameter to be usable in ECS queries or query templates.


Fields

  • public float m_SpawnRate
    Controls how often vehicles are spawned by this spawner. The value is serialized first. Typical usage: a multiplier or frequency value (higher means more frequent spawning). Default (if not set) is 0.0f (struct default).

  • public RoadTypes m_RoadType
    Enum indicating which road type this spawner belongs to. Serialized as a byte. Ensure the enum's values fit into a byte for safe serialization compatibility.

  • public TrackTypes m_TrackType
    Enum indicating which track type (e.g., tram, metro) this spawner belongs to. Serialized as a byte. Also must fit into a byte.

  • public bool m_NoSlowVehicles
    When true, the spawner will not spawn slow vehicles (e.g., service vehicles or slow-moving vehicle categories). Serialized second (after spawn rate) as a boolean.

Properties

  • None (no public properties defined on this struct)

Constructors

  • public TrafficSpawnerData()
    Struct has the implicit default parameterless constructor. Typical initialization is via object initializer to set fields explicitly (see usage example).

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component data in a fixed order expected by the deserializer and by the prefab/scene serialization system:
  • Writes m_SpawnRate (float).
  • Writes m_NoSlowVehicles (bool).
  • Writes m_RoadType cast to byte.
  • Writes m_TrackType cast to byte. Note: the explicit cast of enums to byte defines the on-disk on-wire enum width — changing enum values or expanding beyond byte will break compatibility unless serialization is updated.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads fields in the same order the serializer writes them:

  • Reads into m_SpawnRate (float) by reference.
  • Reads into m_NoSlowVehicles (bool) by reference.
  • Reads a byte for road type and casts to RoadTypes.
  • Reads a byte for track type and casts to TrackTypes. Be careful: the method assumes the binary layout and enum sizes match what Serialize writes.

Remarks: - Maintain read/write order and enum widths when modifying this struct to avoid corrupting saves/prefabs. - If you add fields, consider adding versioning to the serialization format or appending optional blocks instead of changing the existing order.

Usage Example

// Create and configure a spawner component
var spawner = new Game.Prefabs.TrafficSpawnerData {
    m_SpawnRate = 1.5f,
    m_NoSlowVehicles = false,
    m_RoadType = RoadTypes.RoadHighway, // example enum value
    m_TrackType = TrackTypes.None
};

// Example: serializing with an IWriter (pseudo-code; concrete writer provided by engine)
writer.Write(spawner.m_SpawnRate);
writer.Write(spawner.m_NoSlowVehicles);
writer.Write((byte)spawner.m_RoadType);
writer.Write((byte)spawner.m_TrackType);

// Example: deserializing (pseudo-code)
Game.Prefabs.TrafficSpawnerData loaded;
reader.Read(out loaded.m_SpawnRate);
reader.Read(out loaded.m_NoSlowVehicles);
reader.Read(out byte roadByte);
reader.Read(out byte trackByte);
loaded.m_RoadType = (RoadTypes)roadByte;
loaded.m_TrackType = (TrackTypes)trackByte;

{{ Notes for modders: When creating custom spawners or modifying this data for compatibility with saves/prefabs, keep the serialization order and enum sizes unchanged or implement a versioned serializer to maintain backwards compatibility. Use IQueryTypeParameter to include this component in ECS queries where needed. }}