Game.Net.TrafficLightInitializationSystem
Assembly: Assembly-CSharp
Namespace: Game.Net
Type: class
Base: GameSystemBase
Summary:
Initializes traffic light data for nodes in the traffic network. This system groups lanes into signal groups (vehicle and pedestrian groups), detects special cases such as level crossings and moveable bridges, fills lane signal masks/defaults and updates the TrafficLights component so the Traffic Light systems have a coherent initial state. It uses a Burst-compiled IJobChunk (InitializeTrafficLightsJob) to iterate matching entities, read lane/edge/node/prefab data and write LaneSignal and TrafficLights state. The job uses Native containers (NativeList, NativeHashMap, NativeArray, etc.) and ECS ComponentLookup/BufferLookup APIs.
Fields
-
private EntityQuery m_TrafficLightsQuery
Used to find entities that contain a TrafficLights component (and optionally Updated). The query is created in OnCreate and the system requires it for updates (RequireForUpdate). This drives which nodes are processed by the job each frame/update. -
private TypeHandle __TypeHandle
Holds precomputed ECS handles (EntityTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, BufferLookup, etc.) used by the InitializeTrafficLightsJob. These are assigned in OnCreateForCompiler to optimize access to component data in the job.
Properties
- This class exposes no public properties.
Constructors
public TrafficLightInitializationSystem()
Default constructor. The system is decorated with [Preserve] for the game's managed code linking. Initialization of queries and type handles occurs in OnCreate / OnCreateForCompiler rather than in the constructor.
Methods
-
protected override void OnCreate()
Creates the EntityQuery used to find TrafficLights entities. Calls RequireForUpdate with that query so the system runs only when relevant entities exist or are updated. -
protected override void OnUpdate()
Builds and schedules the Burst-compiled InitializeTrafficLightsJob (IJobChunk) using the preassigned TypeHandle data and the m_TrafficLightsQuery. The scheduled job will initialize lane groups, pedestrian groups, and lane signals for every matching chunk, and write back updated TrafficLights components and LaneSignal components. -
private void __AssignQueries(ref SystemState state)
Internal helper used by OnCreateForCompiler. Present for compiler-generated infrastructure; currently constructs an EntityQueryBuilder and disposes it (used to satisfy code-gen / compiler paths). -
protected override void OnCreateForCompiler()
Called by the compiler-generated initialization path. Assigns query-related handles and calls __TypeHandle.__AssignHandles to populate the TypeHandle so the job can access component data efficiently. -
Nested (private) InitializeTrafficLightsJob (BurstCompile) - key methods (job runs per matching chunk)
-
Execute(in ArchetypeChunk chunk, ...)
Main job execute method. For each chunk it:- Allocates temporary NativeLists for lane grouping (vehicle/pedestrian/groups).
- Gathers sub-lanes and sub-objects for the node.
- Detects moveable bridges and level crossings.
- Calls FillLaneBuffers to collect lanes that should get lane signals.
- Calls ProcessMoveableBridgeLanes, ProcessVehicleLaneGroups and ProcessPedestrianLaneGroups to build signal groups and masks.
- Calls InitializeTrafficLights to set TrafficLights.m_SignalGroupCount and to populate LaneSignal data (group mask, default state, flags) and calls TrafficLightSystem.UpdateLaneSignal.
- Writes updated TrafficLights and LaneSignal components back to the ECS ComponentLookup/Buffer. Uses many ComponentLookup/BufferLookup inputs (MasterLane, CarLane, PedestrianLane, Curve, Edge, Node, Lane, PrefabRef, etc.).
-
void IJobChunk.Execute(in ArchetypeChunk chunk, ...)
Explicit interface implementation that forwards to the job's Execute method. -
private bool FindMoveableBridgeData(Entity node, Entity owner, out MoveableBridgeData moveableBridgeData)
Checks the connected edges and sub-objects to see if a moveable bridge prefab is present and returns its MoveableBridgeData. Used to detect moveable bridge special-case behavior for signal grouping. -
private bool FindMoveableBridgeData(DynamicBuffer<Game.Objects.SubObject> subObjects, out MoveableBridgeData moveableBridgeData)
Helper overload that inspects a sub-object buffer to find a PrefabRef with MoveableBridgeData. -
private void ProcessMoveableBridgeLanes(...)
Builds group indices and masks for lanes on nodes that are moveable bridges. Uses a NativeHashMapto map lane endpoints to group indices and handle special grouping/count logic determined by the bridge data. -
private void FillLaneBuffers(DynamicBuffer<SubLane> subLanes, NativeList<LaneGroup> vehicleLanes, NativeList<LaneGroup> pedestrianLanes)
Collects lanes from the node's SubLane buffer that should be considered for traffic signals. Distinguishes MasterLane, SlaveLane, CarLane, PedestrianLane and sets start/end directions, lane index ranges, and flags (IsStraight, IsUnsafe, IsTrack, IsWaterway, IsPedestrian) used later for grouping. -
private void ProcessVehicleLaneGroups(NativeList<LaneGroup> vehicleLanes, NativeList<LaneGroup> groups, bool isLevelCrossing, out int groupCount)
Groups vehicle lanes by their start direction (and track flag if level crossing). Merges groups that can be combined (for turn-through pairs) and computes the final group masks for up to 16 groups. -
private void ProcessPedestrianLaneGroups(DynamicBuffer<SubLane> subLanes, NativeList<LaneGroup> pedestrianLanes, NativeList<LaneGroup> groups, bool isLevelCrossing, ref int groupCount)
Assigns pedestrian lanes to groups and adjusts pedestrian group masks so pedestrians interact correctly with vehicle signal groups (including overlap checks using LaneOverlap buffers). Ensures no-zero group masks by allocating fallback groups if needed. -
private void InitializeTrafficLights(DynamicBuffer<SubLane> subLanes, NativeList<LaneGroup> groups, int groupCount, bool isLevelCrossing, bool isMoveableBridge, ref TrafficLights trafficLights)
Writes the resulting group count into TrafficLights.m_SignalGroupCount (clamped to 16), resets current/next signal group if they are out of range, and iterates lanes in each group to set LaneSignal.m_GroupMask, LaneSignal.m_Default and flags (CanExtend, Physical for moveable bridge). Calls TrafficLightSystem.UpdateLaneSignal to update signal metadata before storing it via m_LaneSignalData lookup. -
TypeHandle.__AssignHandles(ref SystemState state)
Sets up the TypeHandle's internal ECS handles (GetEntityTypeHandle, GetComponentTypeHandle, GetBufferTypeHandle, GetComponentLookup, GetBufferLookup, etc.). This is called from OnCreateForCompiler to populate the job handles before scheduling.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_TrafficLightsQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { ComponentType.ReadOnly<TrafficLights>() },
Any = new ComponentType[] { ComponentType.ReadOnly<Updated>() }
});
RequireForUpdate(m_TrafficLightsQuery);
}
Additional notes for modders: - This system is low-level ECS code that assigns lane signal group masks and defaults. Modifying or interacting with it requires understanding of the game's component types (TrafficLights, LaneSignal, SubLane, MasterLane, CarLane, PedestrianLane, LaneOverlap, etc.). - The job is Burst-compiled and uses Native collections; avoid introducing managed allocations into the job. - Signal groups are limited to 16 (TrafficLights.m_SignalGroupCount is clamped), and lane group masks are stored in a 16-bit mask (ushort). - Special handling exists for level crossings, moveable bridges, waterways and pedestrian overlaps; if you create custom lane/prefab types, ensure PrefabRef/CarLaneData/MoveableBridgeData reflect intended road types or flags so grouping behaves correctly.