CARLA
Loading...
Searching...
No Matches
TrafficLightManager.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
10#include "TrafficLightGroup.h"
11#include "TrafficSignBase.h"
13
14#include "TrafficLightManager.generated.h"
15
16/// Class In charge of creating and assigning traffic
17/// light groups, controllers and components.
18UCLASS()
19class CARLA_API ATrafficLightManager : public AActor
20{
21 GENERATED_BODY()
22
23public:
24
26
27 virtual void Tick(float DeltaSeconds) override;
28
29 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
30
31 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
32 void RegisterLightComponentFromOpenDRIVE(UTrafficLightComponent * TrafficLight);
33
34 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
35 void RegisterLightComponentGenerated(UTrafficLightComponent * TrafficLight);
36
37 const std::optional<carla::road::Map> &GetMap();
38
39 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
40 ATrafficLightGroup* GetTrafficGroup(int JunctionId);
41
42 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
43 UTrafficLightController* GetController(FString ControllerId);
44
45 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
46 USignComponent* GetTrafficSign(FString SignId);
47
48 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
49 void SetFrozen(bool InFrozen);
50
51 UFUNCTION(BlueprintCallable, Category = "Traffic Light Manager")
52 bool GetFrozen();
53
54 UFUNCTION(CallInEditor, Category = "Traffic Light Manager")
55 void GenerateSignalsAndTrafficLights();
56
57 UFUNCTION(CallInEditor, Category = "Traffic Light Manager")
58 void RemoveGeneratedSignalsAndTrafficLights();
59
60 UFUNCTION(CallInEditor, Category = "Traffic Light Manager")
61 void MatchTrafficLightActorsWithOpenDriveSignals();
62
63 // Called when the game starts by the gamemode
64 void InitializeTrafficLights();
65
66 /// Copy the authored model configuration (traffic light / sign / speed
67 /// limit blueprints) from another manager, e.g. a baked manager that is
68 /// being replaced by a persistent-level one on World Partition maps.
69 void AdoptModelConfigurationFrom(const ATrafficLightManager& Other);
70
71 // Whether OpenDRIVE-generated signs/lights are snapped to the ground after
72 // spawning. Read by ALargeMapManager so both paths share the same flag.
73 bool GetAdjustSignsHeightToGround() const { return bAdjustSignsHeightToGround; }
74
75 // Toggle ground snapping at runtime (driven by the carla.WorldSettings
76 // episode setting). Enabling it after the signals have already been
77 // generated re-snaps the spawned signs, so the feature works on maps whose
78 // manager is spawned with defaults and never configured in the editor.
79 void SetAdjustSignsHeightToGround(bool bEnabled);
80
81 // Snap the generated signs that are not on the ground yet, if the flag is
82 // on, and refresh the environment objects when any moved. Call it again
83 // after more ground streams in.
84 void SnapSignsToGround();
85
86private:
87
88 // Snap every generated sign to the ground, ignoring the generated set during
89 // the trace so the ray reaches the terrain. Idempotent via bPositioned;
90 // returns true if at least one sign moved.
91 bool AdjustSpawnedSignsHeight();
92
93 // Every valid sign, to be ignored during a ground trace. Otherwise the ray
94 // hits the sign's own collision (or a neighbour still at its nominal height)
95 // instead of the ground, which lifts the actor by roughly its own base
96 // height and leaves the pole floating (see PR #9773).
97 TArray<AActor*> GetSignsToIgnoreWhileTracing() const;
98
99 void SpawnTrafficLights();
100
101 void SpawnSignals();
102
103 void UpdateSignalGroundDormancy();
104
105 void OnLevelAddedToWorld(ULevel *Level, UWorld *World);
106
107 void SuppressSpawnedSignsIfLevelHasPlacedSigns(ULevel *Level);
108
109 void RemoveRoadrunnerProps() const;
110
111 void RemoveAttachedProps(TArray<AActor*> Actors) const;
112
113 // Mapped references to ATrafficLightGroup (junction)
114 UPROPERTY()
115 TMap<int, TObjectPtr<ATrafficLightGroup>> TrafficGroups;
116
117 // Mapped references to UTrafficLightController (controllers)
118 UPROPERTY()
119 TMap<FString, TObjectPtr<UTrafficLightController>> TrafficControllers;
120
121 // Mapped references to individual TrafficLightComponents
122 UPROPERTY()
123 TMap<FString, TObjectPtr<USignComponent>> TrafficSignComponents;
124
125 // Mapped references to TrafficSigns
126 UPROPERTY()
127 TArray<TObjectPtr<ATrafficSignBase>> TrafficSigns;
128
129 // When true, snap generated signs/lights to the ground after spawning.
130 // Opt-in: off by default so maps that need it enable it explicitly.
131 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
132 bool bAdjustSignsHeightToGround = false;
133
134 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
135 TSubclassOf<AActor> TrafficLightModel_RHT;
136
137 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
138 TSubclassOf<AActor> TrafficLightModel_LHT;
139
140
141 // Relates an OpenDRIVE type to a traffic sign blueprint
142 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
143 TMap<FString, TSubclassOf<AActor>> TrafficSignsModels;
144
145 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
146 TMap<FString, TSubclassOf<USignComponent>> SignComponentModels;
147
148 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
149 TMap<FString, TSubclassOf<AActor>> SpeedLimitModels;
150
151 // US (MUTCD) speed plates keyed by mph. Used instead of SpeedLimitModels when the
152 // xodr signal carries country="US" (see Signal::GetCountry(); twin builds with a US
153 // profile stamp it, stock CARLA maps carry "OpenDRIVE" and keep the EU plates).
154 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
155 TMap<FString, TSubclassOf<AActor>> SpeedLimitModels_US;
156
157 UPROPERTY(Category = "Traffic Light Manager", VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
158 TObjectPtr<USceneComponent> SceneComponent;
159
160 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
161 bool TrafficLightsGenerated = false;
162
163 // Id for TrafficLightGroups without corresponding OpenDRIVE junction
164 UPROPERTY()
165 int TrafficLightGroupMissingId = -2;
166
167 // Id for TrafficLightControllers without corresponding OpenDRIVE junction
168 UPROPERTY()
169 int TrafficLightControllerMissingId = -1;
170
171 // Id for TrafficLightComponents without corresponding OpenDRIVE junction
172 UPROPERTY()
173 int TrafficLightComponentMissingId = -1;
174
175 UPROPERTY()
176 bool bTrafficLightsFrozen = false;
177
178 // On World Partition maps the OpenDRIVE-spawned signals are persistent
179 // actors while the ground streams per cell, so a signal can be visible over
180 // terrain that is not resident. Signals with no ground beneath them are
181 // hidden until their cell streams in (legacy tiled maps handled this by
182 // putting out-of-range actors to sleep in ALargeMapManager).
183 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
184 int32 DormancyChecksPerTick = 150;
185
186 UPROPERTY(EditAnywhere, Category= "Traffic Light Manager")
187 float DormancyTraceDepth = 2000.0f;
188
189 int32 DormancySweepIndex = 0;
190
191 // Set when the dormancy sweep snaps a sign, cleared when the environment
192 // objects are re-registered at the end of a full sweep.
193 bool bPendingEnvironmentObjectRefresh = false;
194
195 // Signs SpawnSignals spawned from the OpenDRIVE (stop, yield, speed limit).
196 // On a World Partition map whose own signs are placed in the level (Town15:
197 // Vienna Convention BP_Sign actors), those are what should be seen, but they
198 // are in cells not loaded when SpawnSignals runs, so it cannot adopt them
199 // and spawns stock CARLA signs as well (a US "YIELD" plate among European
200 // signs). Once a placed sign streams in, the spawned ones are hidden and
201 // lose their mesh collision; their sign components and trigger boxes stay,
202 // so the traffic logic is unchanged.
203 TSet<TWeakObjectPtr<ATrafficSignBase>> SpawnedSigns;
204
205 bool bSpawnedSignsSuppressed = false;
206
207 FDelegateHandle LevelAddedHandle;
208
209};
Class which implements the state changing of traffic lights.
Class In charge of creating and assigning traffic light groups, controllers and components.
bool GetAdjustSignsHeightToGround() const
Maps a controller from OpenDrive.