Game.Simulation.RandomTrafficRequest
Assembly: Game
Namespace: Game.Simulation
Type: struct (value type)
Base: System.ValueType
Implements: IComponentData, IQueryTypeParameter, ISerializable
Summary:
Represents an ECS component used to request spawning or routing of random traffic for a specific target entity. The struct encodes desired characteristics for the requested traffic (road/track types, energy type, vehicle size class) and behavioural flags. It implements custom serialization with version checks so older save/version formats can omit newer fields.
Fields
-
public Entity m_Target
Entity that is the target of the random traffic request (Unity.Entities.Entity). Typically this is the entity at/near which random traffic should be generated or directed. -
public RoadTypes m_RoadType
Enumerates the desired road type for the requested traffic. Stored/serialized as a byte. -
public TrackTypes m_TrackType
Enumerates the desired track type (for rail/tram-like vehicles). Stored/serialized as a byte. -
public EnergyTypes m_EnergyTypes
Enumerates allowed/desired energy types for vehicles (e.g., petrol, electric). Stored/serialized as a byte. -
public SizeClass m_SizeClass
Enumerates the size class for vehicles (small/medium/large). Stored/serialized as a byte. -
public RandomTrafficRequestFlags m_Flags
Flags modifying the behaviour of the request (stored as a byte). Presence of this field in serialization is guarded by a version check.
Properties
- This struct does not expose any C# properties; all data is stored in public fields.
Constructors
public RandomTrafficRequest(Entity target, RoadTypes roadType, TrackTypes trackType, EnergyTypes energyTypes, SizeClass sizeClass, RandomTrafficRequestFlags flags)
Creates a new RandomTrafficRequest with the given target entity and desired traffic characteristics/flags. Assigns each parameter to its corresponding field.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Serializes the component data to the provided writer. Writes the target Entity first, then writes each enum field as a single byte in this order: m_RoadType, m_TrackType, m_EnergyTypes, m_SizeClass, and finally m_Flags. Consumers should note that older readers may not expect the later bytes; serialization writes all bytes unconditionally. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Deserializes component data from the provided reader. Reads the target Entity first. The method checks reader.context.version to conditionally read newer fields: - If reader.context.version >= Version.randomTrafficTypes: reads 4 bytes (roadType, trackType, energyTypes, sizeClass) and assigns them to the corresponding fields.
- If reader.context.version >= Version.randomTrafficFlags: reads an additional byte and assigns it to m_Flags. If the saved data/version predates these Version markers, the corresponding fields remain at their default values.
Usage Example
// Create a request and attach it as a component to an entity (EntityManager usage shown as example)
Entity targetEntity = /* obtain target entity */;
var request = new RandomTrafficRequest(
targetEntity,
RoadTypes.Road, // desired road type
TrackTypes.None, // desired track type
EnergyTypes.All, // allowed energy types
SizeClass.Medium, // vehicle size class
RandomTrafficRequestFlags.None
);
// Add the request to an entity so systems can process it
entityManager.AddComponentData(targetEntity, request);
Notes and tips: - This struct is intended to be used within the ECS (Unity.Entities) pipeline; systems can query for RandomTrafficRequest components to spawn or route random traffic. - Be mindful of save/version compatibility: Deserialize uses Version.randomTrafficTypes and Version.randomTrafficFlags to decide which fields are present in serialized data. When creating tooling or custom serializers, follow the same version-aware approach.