Game.Simulation.TrafficLightSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
TrafficLightSystem is an ECS system responsible for updating traffic light logic for nodes in the simulation. It queries entities with TrafficLights and UpdateFrame, schedules a parallel IJobChunk (UpdateTrafficLightsJob) that evaluates lane signals, determines the next signal group, updates lane-level LaneSignal components and TrafficLight objects, and updates moveable bridge point-of-interest positions. The system uses an EndFrameBarrier to produce command buffers for deferred entity changes and runs on a periodic update interval (GetUpdateInterval returns 4, the system builds an UpdateFrame filter to select which TrafficLights to process).
Fields
-
private const uint UPDATE_INTERVAL = 64u
Used as a constant in the class (present but not directly used in the shown OnUpdate). Indicates a conceptual update interval constant present in the system. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem (used to fetch the current simulation frame index when building the UpdateFrame shared component filter). -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create an EntityCommandBuffer.ParallelWriter for making safe changes (e.g., adding EffectsUpdated) from the scheduled job and to add the job handle as producer. -
private EntityQuery m_TrafficLightQuery
EntityQuery used to find TrafficLights entities to operate on. The query requires TrafficLights (read/write) and UpdateFrame, and excludes Deleted/Destroyed/Temp. -
private TypeHandle __TypeHandle
Container for cached type and buffer handles / component lookups used by the job. Populated in OnCreateForCompiler / __AssignHandles. -
(Nested types)
UpdateTrafficLightsJob
(private struct) andTypeHandle
(private struct)
UpdateTrafficLightsJob performs the per-chunk logic; TypeHandle holds the handles used to access components/buffers inside the job.
Properties
- None (the system does not expose public properties).
Constructors
public TrafficLightSystem()
Default constructor (preserved attribute on lifecycle methods, constructor does default initialization).
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 4. The system uses this to decide the frequency (frames) at which it participates in the UpdateFrame selection logic. -
[Preserve] protected override void OnCreate()
Initializes references to SimulationSystem and EndFrameBarrier, creates the m_TrafficLightQuery and calls RequireForUpdate(m_TrafficLightQuery). Sets up the query used by OnUpdate. -
[Preserve] protected override void OnUpdate()
Main update path: - Resets the query filter and sets a shared component filter with UpdateFrame computed from SimulationUtils.GetUpdateFrameWithInterval using the SimulationSystem frame index and the system's update interval.
- Builds and schedules UpdateTrafficLightsJob as a parallel job (JobChunkExtensions.ScheduleParallel) passing required Component/Buffer lookups and a parallel command buffer.
-
Assigns the resulting JobHandle to base.Dependency and registers it with m_EndFrameBarrier.AddJobHandleForProducer.
-
public static void UpdateLaneSignal(TrafficLights trafficLights, ref LaneSignal laneSignal)
Static helper that updates a single LaneSignal.m_Signal based on the current TrafficLights state (Current/NexSignalGroup and state machine: Beginning, Ongoing, Extending, Extended, Ending, Changing). Encodes logic for mapping traffic light state groups to lane-level signals (Go, Stop, Yield, SafeStop, None). -
public static void UpdateTrafficLightState(TrafficLights trafficLights, ref TrafficLight trafficLight)
Static helper that converts the net-level TrafficLights state/group info to the object-level TrafficLight states (two independent signal sets encoded in the TrafficLight.m_State). Considers LevelCrossing flags for flashing/yellow behavior. -
public static void UpdateMoveableBridge(TrafficLights trafficLights, Transform transform, MoveableBridgeData moveableBridgeData, ref PointOfInterest pointOfInterest)
Updates the PointOfInterest position for a moveable bridge subobject depending on the current/next signal group and configured lift offsets. Used to animate/position moveable bridge POI for effects/visuals. -
private void __AssignQueries(ref SystemState state)
Compiler helper that prepares queries (present as a stub calling EntityQueryBuilder). Part of generated/compiled-once path. -
protected override void OnCreateForCompiler()
Compiler helper that calls __AssignQueries and __TypeHandle.__AssignHandles to populate component lookups/handles used by the job. -
(Numerous private / nested job methods) The heavy lifting is done inside UpdateTrafficLightsJob:
- Execute(in ArchetypeChunk chunk, ...) — iterates entities in the chunk, reads TrafficLights and related buffers, collects lane signals, handles moveable bridges, updates traffic light state machine and lane-level signals, updates traffic light objects and point-of-interest for bridges, and writes EffectsUpdated to the command buffer when appropriate.
- FillLaneSignals(...) overloads — gather lane signal entities from SubLane buffers, from connected edges and SubNet buffers, taking into account node-edge curve orientation (to pick lanes belonging to a node).
- UpdateTrafficLightState(NativeList
laneSignals, MoveableBridgeData moveableBridgeData, ref TrafficLights trafficLights) — per-node state-machine driver; implements the traffic light state transitions (None, Beginning, Ongoing, Extending, Extended, Ending, Changing). Uses GetNextSignalGroup, RequireEnding, ClearPriority, IsEmpty semantics to decide transitions and timers. - GetNextSignalGroup(...) — evaluates lane signal petitions, priorities, group masks and extension capability to select the next signal group to grant.
- RequireEnding(...) — determines if current group must be ended because other lanes are still set to Go.
- UpdateLaneSignals(...) — iterates gathered lane signals and applies UpdateLaneSignal(...) logic.
- FindMoveableBridge(...) — checks subObjects for a PrefabRef that has MoveableBridgeData to link bridge POI.
- UpdateTrafficLightObjects(...) — updates object-level TrafficLight components for subObjects that have TrafficLight components.
- IsEmpty(...) and CheckNextLane(...) — helper checks to find blockers on lanes (lane objects, reservations, special prefab types) when deciding if moveable bridge can start moving or if next group is empty.
Note: UpdateTrafficLightsJob uses a number of ComponentLookup
Nested types
private struct UpdateTrafficLightsJob : IJobChunk
- Purpose: perform per-chunk processing of TrafficLights entities in parallel.
- Important fields (passed by OnUpdate): EntityTypeHandle, BufferTypeHandle
, BufferTypeHandle , BufferTypeHandle , ComponentTypeHandle , many ComponentLookup and BufferLookup for supporting data (Owner, Node, Edge, Curve, Lane, LaneReservation, Transform, PrefabRef, CarLaneData, MoveableBridgeData, ObjectGeometryData, LaneObject, SubNet/SubLane/ConnectedEdge), parallel EntityCommandBuffer writer, and writable lookups for LaneSignal / TrafficLight / PointOfInterest. -
Main method: Execute(...) — processes each TrafficLights component to collect lane signals, possibly find a moveable bridge subobject, call state-machine logic to update trafficLights, update lane signal components and traffic light objects, update moveable bridge POI, and potentially add EffectsUpdated to the entity.
-
private struct TypeHandle
- Holds a cached set of handles/lookups used by the job (EntityTypeHandle, BufferTypeHandle/ComponentLookup/BufferLookup for all component types used). Provides __AssignHandles(ref SystemState) to get the handles from the SystemState.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_TrafficLightQuery = GetEntityQuery(
ComponentType.ReadWrite<TrafficLights>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Destroyed>(),
ComponentType.Exclude<Temp>()
);
RequireForUpdate(m_TrafficLightQuery);
}
Notes and tips for modders: - This system is tightly integrated with the ECS data model (TrafficLights, LaneSignal, TrafficLight object components, MoveableBridgeData, buffers like SubLane/SubNet). To influence traffic light logic, modify LaneSignal components or TrafficLights component data, or inject different UpdateFrame values so your TrafficLights get updated at different times. - The three static helpers (UpdateLaneSignal, UpdateTrafficLightState, UpdateMoveableBridge) can be reused if creating custom traffic-light-like behaviour in other systems or tools. - When creating modifications that add/modify TrafficLights/Traf ficLight-related subobjects, ensure you respect the EndFrameBarrier usage and either schedule jobs that produce their own command buffers or use existing barriers to avoid race conditions.