CARLA
Loading...
Searching...
No Matches
CarlaGameModeBase.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
20
22#include <carla/rpc/Texture.h>
25
26#include <util/ue-header-guard-begin.h>
27#include "CoreMinimal.h"
28#include "GameFramework/GameModeBase.h"
30#include <util/ue-header-guard-end.h>
31
32#include <optional>
33
34#include "CarlaGameModeBase.generated.h"
35
36DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEpisodeInitialisedDelegate, UCarlaEpisode*, InitialisedEpisode);
37
38/// Base class for the CARLA Game Mode.
39UCLASS(HideCategories=(ActorTick))
40class CARLA_API ACarlaGameModeBase : public AGameModeBase
41{
42 GENERATED_BODY()
43
44public:
45
46 ACarlaGameModeBase(const FObjectInitializer& ObjectInitializer);
47
49 {
50 check(Episode != nullptr);
51 return *Episode;
52 }
53
54 const std::optional<carla::road::Map>& GetMap() const {
55 return Map;
56 }
57
58 const FString GetFullMapPath() const;
59
60 // get path relative to Content folder
61 const FString GetRelativeMapPath() const;
62
63 UFUNCTION(Exec, Category = "CARLA Game Mode")
64 void DebugShowSignals(bool enable);
65
66 UFUNCTION(BlueprintCallable, Category = "CARLA Game Mode")
67 ATrafficLightManager* GetTrafficLightManager();
68
69 UFUNCTION(Category = "Carla Game Mode", BlueprintCallable)
70 const TArray<FTransform>& GetSpawnPointsTransforms() const{
71 return SpawnPointsTransforms;
72 }
73
74 UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec)
75 TArray<FBoundingBox> GetAllBBsOfLevel(uint8 TagQueried = 0xFF) const;
76
77 UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec)
78 TArray<FEnvironmentObject> GetEnvironmentObjects(uint8 QueriedTag = 0xFF) const
79 {
80 return ObjectRegister->GetEnvironmentObjects(QueriedTag);
81 }
82
83 void EnableEnvironmentObjects(const TSet<uint64>& EnvObjectIds, bool Enable);
84
85 /// The registered environment objects disabled now, and a count of the
86 /// changes to that set: Multi-GPU secondaries mirror it.
87 const TSet<uint64> &GetDisabledEnvironmentObjects() const
88 {
89 return DisabledEnvironmentObjects;
90 }
91
93 {
94 return EnvironmentObjectsSerial;
95 }
96
97 /// How many times the environment objects were registered (on every map
98 /// layer streamed in or out).
100 {
101 return EnvironmentObjectsRegistrations;
102 }
103
104 /// rpc::MapLayer bits of the layers loaded now.
105 int32 GetLoadedMapLayers() const
106 {
107 return LoadedMapLayers;
108 }
109
110 void EnableOverlapEvents();
111
112 void CheckForEmptyMeshes();
113
114 UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec)
115 void LoadMapLayer(int32 MapLayers);
116
117 UFUNCTION(Category = "Carla Game Mode", BlueprintCallable, CallInEditor, Exec)
118 void UnLoadMapLayer(int32 MapLayers);
119
120 UFUNCTION(Category = "Carla Game Mode")
121 ULevel* GetULevelFromName(FString LevelName);
122
123 UFUNCTION(BlueprintCallable, Category = "Carla Game Mode")
124 void OnLoadStreamLevel();
125
126 UFUNCTION(BlueprintCallable, Category = "Carla Game Mode")
127 void OnUnloadStreamLevel();
128
129 ALargeMapManager* GetLMManager() const {
130 return LMManager;
131 }
132
133 AActor* FindActorByName(const FString& ActorName);
134
135 UTexture2D* CreateUETexture(const carla::rpc::TextureColor& Texture);
136 UTexture2D* CreateUETexture(const carla::rpc::TextureFloatColor& Texture);
137
139 AActor* Actor,
140 UTexture2D* Texture,
141 const carla::rpc::MaterialParameter& TextureParam);
142
143 [[deprecated("This command no longer makes sense since the apply_textures API relies on IDs.")]]
144 TArray<FString> GetNamesOfAllActors();
145
146 /// Gravitational acceleration used by IMU sensors.
147 /// Default is Earth's, approximately 9.81 m/s^2.
148 UPROPERTY(BlueprintReadOnly, EditAnywhere, Category="Sensor Gravity")
149 float IMUSensorGravity = 9.81f;
150
151 FOnEpisodeInitialisedDelegate OnEpisodeInitialisedDelegate;
152
153protected:
154
155 void InitGame(const FString &MapName, const FString &Options, FString &ErrorMessage) override;
156
157 void RestartPlayer(AController *NewPlayer) override;
158
159 void BeginPlay() override;
160
161 void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
162
163 void Tick(float DeltaSeconds) override;
164
165 /// Loads the episode georeference (parsing the map's OpenDRIVE by default)
166 /// and stores the recommended spawn points. Virtual so Autoware maps can
167 /// take the georeference from an MGRS data asset instead (tier4 port).
168 virtual void LoadGeoReference();
169
170 void StoreSpawnPoints();
171
172 UPROPERTY()
173 UCarlaEpisode *Episode = nullptr;
174
175private:
176
177 /// Tags actors of World Partition cells as they stream in. The legacy
178 /// ALargeMapManager used to do this for WorldComposition tiles, but it
179 /// retires itself on World Partition maps, which left streamed-in cells
180 /// untagged (semantic segmentation rendered them unlabeled).
181 void OnLevelAddedToWorld(ULevel* InLevel, UWorld* InWorld);
182
183 void SpawnActorFactories();
184
185 void GenerateSpawnPoints();
186
187 void ParseOpenDrive();
188
189public:
190
191 // Public so map managers can refresh environment objects after relocating
192 // actors (e.g. traffic sign height adjustment).
193 void RegisterEnvironmentObjects();
194
195private:
196
197 void ConvertMapLayerMaskToMapNames(int32 MapLayer, TArray<FName>& OutLevelNames);
198
199 void OnEpisodeSettingsChanged(const FEpisodeSettings &Settings);
200
201 UPROPERTY()
202 UCarlaGameInstance *GameInstance = nullptr;
203
204 UPROPERTY()
205 UTaggerDelegate *TaggerDelegate = nullptr;
206
207 UPROPERTY()
208 UCarlaSettingsDelegate *CarlaSettingsDelegate = nullptr;
209
210 UPROPERTY()
211 ACarlaRecorder *Recorder = nullptr;
212
213 UPROPERTY()
214 UObjectRegister* ObjectRegister = nullptr;
215
216 /// The class of Weather to spawn.
217 UPROPERTY(Category = "CARLA Game Mode", EditAnywhere)
218 TSubclassOf<AWeather> WeatherClass;
219
220 /// List of actor spawners that will be used to define and spawn the actors
221 /// available in game.
222 UPROPERTY(Category = "CARLA Game Mode", EditAnywhere)
223 TSet<TSubclassOf<ACarlaActorFactory>> ActorFactories;
224
225 UPROPERTY()
226 TArray<FTransform> SpawnPointsTransforms;
227
228 UPROPERTY()
229 TArray<ACarlaActorFactory *> ActorFactoryInstances;
230
231 UPROPERTY()
232 ATrafficLightManager* TrafficLightManager = nullptr;
233
234 ALargeMapManager* LMManager = nullptr;
235
236 FDelegateHandle OnEpisodeSettingsChangeHandle;
237
238 std::optional<carla::road::Map> Map;
239
240 int PendingLevelsToLoad = 0;
241
242 TSet<uint64> DisabledEnvironmentObjects;
243
244 uint64 EnvironmentObjectsSerial = 0u;
245
246 uint64 EnvironmentObjectsRegistrations = 0u;
247
248 int32 LoadedMapLayers = 0;
249 int PendingLevelsToUnLoad = 0;
250
251 bool ReadyToRegisterObjects = false;
252
253 // We keep a global uuid to allow the load/unload layer methods to be called
254 // in the same tick
255 int32 LatentInfoUUID = 0;
256
257};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEpisodeInitialisedDelegate, UCarlaEpisode *, InitialisedEpisode)
static R< void > ApplyTextureToActor(UCarlaEpisode *Episode, carla::rpc::ActorId ActorId, const carla::rpc::MaterialParameter &MaterialParameter, T &&Texture)
std::map< std::string, unsigned int > road
Base class for Carla actor factories.
Base class for the CARLA Game Mode.
const std::optional< carla::road::Map > & GetMap() const
uint64 GetEnvironmentObjectsRegistrations() const
How many times the environment objects were registered (on every map layer streamed in or out).
uint64 GetEnvironmentObjectsSerial() const
const UCarlaEpisode & GetCarlaEpisode() const
const TSet< uint64 > & GetDisabledEnvironmentObjects() const
The registered environment objects disabled now, and a count of the changes to that set: Multi-GPU se...
int32 GetLoadedMapLayers() const
rpc::MapLayer bits of the layers loaded now.
Recorder for the simulation.
Class In charge of creating and assigning traffic light groups, controllers and components.
A simulation episode.
The game instance contains elements that must be kept alive in between levels.
Used to set settings for every actor that is spawned into the world.
Used to tag every actor that is spawned into the world.
This file contains definitions of common data structures used in traffic manager.