Agrarsense
Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
AAgrarsenseGameModeBase Class Reference

#include <AgrarsenseGameModeBase.h>

Inheritance diagram for AAgrarsenseGameModeBase:
Inheritance graph
[legend]
Collaboration diagram for AAgrarsenseGameModeBase:
Collaboration graph
[legend]

Public Member Functions

 AAgrarsenseGameModeBase (const FObjectInitializer &ObjectInitializer)
 
ALidarManagerGetLidarManager ()
 
UROSHandlerGetROSHandle ()
 
ASimulationLevelManagerGetSimulationLevelManager ()
 
AInstancedRendererManagerGetInstancedRenderer ()
 
AWeatherGetWeatherActor ()
 
ASpectatorGetSpectator ()
 
UROSCommandsGetROSCommands ()
 

Public Attributes

TSubclassOf< ALidarManagerLidarManagerClass
 
TSubclassOf< UUserWidget > DeveloperTools
 
TSubclassOf< ASimulationLevelManagerSimulationLevelManagerClass
 

Protected Member Functions

void InitGame (const FString &MapName, const FString &Options, FString &ErrorMessage) override
 
void BeginPlay () override
 
void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 

Private Member Functions

void ParseMapChangeLaunchArgument ()
 
void SpawnNeededActorsInitGame ()
 
void SpawnNeededActorsBeginPlay ()
 

Private Attributes

ALidarManagerLidarManager = nullptr
 
UROSHandlerROSHandle = nullptr
 
ASimulationLevelManagerSimulationLevelManager = nullptr
 
AInstancedRendererManagerInstancedRenderer = nullptr
 
AWeatherWeather = nullptr
 
UROSCommandsROSCommands = nullptr
 
ASpectatorSpectator = nullptr
 
ATaggerTagger = nullptr
 

Static Private Attributes

static bool AttemptToLaunchROSBridge = true
 
static bool StartMessageLogged = false
 
static bool MapChanged = false
 

Detailed Description

Definition at line 32 of file AgrarsenseGameModeBase.h.

Constructor & Destructor Documentation

◆ AAgrarsenseGameModeBase()

AAgrarsenseGameModeBase::AAgrarsenseGameModeBase ( const FObjectInitializer &  ObjectInitializer)

Definition at line 33 of file AgrarsenseGameModeBase.cpp.

33 : Super(ObjectInitializer)
34{
35 PrimaryActorTick.bCanEverTick = false;
36}

Member Function Documentation

◆ BeginPlay()

void AAgrarsenseGameModeBase::BeginPlay ( )
overrideprotected

Entry point to the simulation after InitGame.

Definition at line 45 of file AgrarsenseGameModeBase.cpp.

46{
47 Super::BeginPlay();
48
50
52}

References ParseMapChangeLaunchArgument(), and SpawnNeededActorsBeginPlay().

◆ EndPlay()

void AAgrarsenseGameModeBase::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprotected

Called when simulation ends.

Definition at line 54 of file AgrarsenseGameModeBase.cpp.

55{
56 Super::EndPlay(EndPlayReason);
57
58 if (EndPlayReason == EEndPlayReason::Type::Quit || EndPlayReason == EEndPlayReason::Type::EndPlayInEditor)
59 {
61
62 if (ROSCommands)
63 {
65 }
66
67 SimulatorLog::Log("Simulator shutting down. Goodbye.");
69 }
70
71#if WITH_EDITOR
72 if (EndPlayReason == EEndPlayReason::Type::EndPlayInEditor)
73 {
74 EditorEndPlayCleanup();
75 }
76#endif
77}
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static void Shutdown()
static void Destroy()
Definition: InfoTopic.cpp:33
void Destroy()
Definition: ROSCommands.cpp:59

References UInfoTopic::Destroy(), UROSCommands::Destroy(), SimulatorLog::Log(), ROSCommands, and SimulatorLog::Shutdown().

◆ GetInstancedRenderer()

AInstancedRendererManager * AAgrarsenseGameModeBase::GetInstancedRenderer ( )
inline

Get Instanced Renderer pointer.

Returns
AInstancedRenderer actor pointer or nullptr

Definition at line 76 of file AgrarsenseGameModeBase.h.

77 {
78 return InstancedRenderer;
79 }
AInstancedRendererManager * InstancedRenderer

Referenced by UAgrarsenseStatics::GetInstancedRenderer().

◆ GetLidarManager()

ALidarManager * AAgrarsenseGameModeBase::GetLidarManager ( )
inline

Get the Lidar Manager instance

Returns
Pointer to the Lidar Manager or nullptr

Definition at line 46 of file AgrarsenseGameModeBase.h.

47 {
48 return LidarManager;
49 }

Referenced by UAgrarsenseStatics::GetLidarManager().

◆ GetROSCommands()

UROSCommands * AAgrarsenseGameModeBase::GetROSCommands ( )
inline

Get UROSCommands pointer.

Returns
UROSCommands pointer or nullptr

Definition at line 106 of file AgrarsenseGameModeBase.h.

107 {
108 return ROSCommands;
109 }

◆ GetROSHandle()

UROSHandler * AAgrarsenseGameModeBase::GetROSHandle ( )
inline

Get the ROS Handler instance

Returns
Pointer to the ROS Handler or nullptr

Definition at line 56 of file AgrarsenseGameModeBase.h.

57 {
58 return ROSHandle;
59 }

Referenced by UAgrarsenseStatics::GetROSHandle().

◆ GetSimulationLevelManager()

ASimulationLevelManager * AAgrarsenseGameModeBase::GetSimulationLevelManager ( )
inline

Get simulation level's manager

Returns
Simulation level manager pointer or nullptr

Definition at line 66 of file AgrarsenseGameModeBase.h.

67 {
69 }
ASimulationLevelManager * SimulationLevelManager

Referenced by UAgrarsenseStatics::GetSimulationLevelManager().

◆ GetSpectator()

ASpectator * AAgrarsenseGameModeBase::GetSpectator ( )
inline

Get ASpectator pointer.

Returns
AWeather pointer or nullptr

Definition at line 96 of file AgrarsenseGameModeBase.h.

97 {
98 return Spectator;
99 }

Referenced by UAgrarsenseStatics::GetSpectator().

◆ GetWeatherActor()

AWeather * AAgrarsenseGameModeBase::GetWeatherActor ( )
inline

Get AWeather pointer.

Returns
AWeather pointer or nullptr

Definition at line 86 of file AgrarsenseGameModeBase.h.

87 {
88 return Weather;
89 }

Referenced by UAgrarsenseStatics::GetWeatherActor().

◆ InitGame()

void AAgrarsenseGameModeBase::InitGame ( const FString &  MapName,
const FString &  Options,
FString &  ErrorMessage 
)
overrideprotected

Entry point to the simulation. InitGame is called before BeginPlay

Definition at line 38 of file AgrarsenseGameModeBase.cpp.

39{
40 Super::InitGame(MapName, Options, ErrorMessage);
41
43}

References SpawnNeededActorsInitGame().

◆ ParseMapChangeLaunchArgument()

void AAgrarsenseGameModeBase::ParseMapChangeLaunchArgument ( )
private

Parse launch arguments after begin play.

Definition at line 94 of file AgrarsenseGameModeBase.cpp.

95{
96 if (MapChanged)
97 {
98 // If map has already been changed, don't change it again.
99 return;
100 }
101
102 // Map names and their actual paths to check against
103 const TMap<FString, FString> MapLaunchArgs = {
104 { "-vindeln", "/Game/Agrarsense/Maps/Vindeln/Vindeln" },
105 { "-vindeln_dev", "/Game/Agrarsense/Maps/Vindeln/Vindeln_Dev" },
106 { "-rovaniemi", "/Game/Agrarsense/Maps/RovaniemiForest/RovaniemiForest" },
107 { "-rovaniemi_dev", "/Game/Agrarsense/Maps/RovaniemiForest/RovaniemiForest_Dev" },
108 { "-playground", "/Game/Agrarsense/Maps/Playground" }
109 };
110
111 const FString CommandLine = FCommandLine::Get();
112 FString MapName;
113
114 for (const auto& Pair : MapLaunchArgs)
115 {
116 if (FParse::Param(*CommandLine, *Pair.Key))
117 {
118 MapChanged = true;
119 MapName = Pair.Value;
120 break;
121 }
122 }
123
124 if (MapChanged)
125 {
126 // Chage map with minor delay.
127 FTimerHandle Handle;
128 GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([this, MapName]
129 {
130 if (IsValid(this))
131 {
132 UGameplayStatics::OpenLevel(GetWorld(), *MapName);
133 }
134 }), 0.300f, false);
135 }
136}

References MapChanged.

Referenced by BeginPlay().

◆ SpawnNeededActorsBeginPlay()

void AAgrarsenseGameModeBase::SpawnNeededActorsBeginPlay ( )
private

Spawn needed Actors or UObjects that should always exist in the level in BeginPlay. This is the Simulator second entry point.

Definition at line 237 of file AgrarsenseGameModeBase.cpp.

238{
239 UWorld* World = GetWorld();
240
241 // Spawn developer tools widget (Ctrl + F9 to open)
242 // this cannot be spawned in InitGame!
243 if (DeveloperTools)
244 {
245 auto developerToolsWidget = CreateWidget<UUserWidget>(World, DeveloperTools);
246 developerToolsWidget->AddToViewport(10);
247 }
248#if WITH_EDITOR
249 else
250 {
251 UE_LOG(LogTemp, Error, TEXT("Missing DeveloperTools class! Please open AgrarsenseGameMode in Unreal and add it."));
252 }
253#endif
254
255 if (!ROSCommands)
256 {
257 ROSCommands = NewObject<UROSCommands>();
258 ROSCommands->Init();
259 }
260
262
263 if (!Spectator)
264 {
265 // try to get Spectator actor
266 TArray<AActor*> Spectators;
267 UGameplayStatics::GetAllActorsOfClass(World, ASpectator::StaticClass(), Spectators);
268 if (!Spectators.IsEmpty())
269 {
270 Spectator = Cast<ASpectator>(Spectators[0]);
271
272#if WITH_EDITOR
273 if (Spectators.Num() >= 2)
274 {
275 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Spectator blueprints. Make sure to only use one."));
276 }
277#endif
278 }
279 }
280
281 if (!Tagger)
282 {
283 // Destroy all existing Tagger if there's any, there should only be one.
284 TArray<AActor*> Taggers;
285 UGameplayStatics::GetAllActorsOfClass(World, ATagger::StaticClass(), Taggers);
286 for (AActor* TaggerActor : Taggers)
287 {
288 if (TaggerActor)
289 {
290 TaggerActor->Destroy();
291 }
292 }
293 Taggers.Empty();
294 Tagger = World->SpawnActor<ATagger>();
295 }
296
297 if (World && GEngine && GEngine->GameViewport)
298 {
299 // Create and add a drag-and-drop overlay widget to the main viewport (Spectator).
300 // Note: All widgets block the drag-and-drop interaction.
301 // To ensure smooth usage, try dragging the file into an area without any visible widgets.
302 // If the cursor shows a cancel icon, it indicates a hidden widget is obstructing the view.
303 TSharedPtr<SDragAndDropWidget> OverlayWidget = SNew(SDragAndDropWidget);
304 GEngine->GameViewport->AddViewportWidgetContent(OverlayWidget.ToSharedRef());
305 }
306}
TSubclassOf< UUserWidget > DeveloperTools
Definition: Tagger.h:51
static void Init()
Definition: InfoTopic.cpp:24

References DeveloperTools, UInfoTopic::Init(), UROSCommands::Init(), ROSCommands, Spectator, and Tagger.

Referenced by BeginPlay().

◆ SpawnNeededActorsInitGame()

void AAgrarsenseGameModeBase::SpawnNeededActorsInitGame ( )
private

Spawn needed Actors or UObjects that should always exist in the level in InitGame. This is the Simulator entry point.

Definition at line 138 of file AgrarsenseGameModeBase.cpp.

139{
141 {
143 SimulatorLog::Log("Simulator starting.");
144 StartMessageLogged = true;
145 }
146
147 UWorld* World = GetWorld();
148
149 // Setup Agrarsense settings
151 if (Settings)
152 {
153 Settings->Setup(World);
154 }
155
156 UROSIntegrationGameInstance* RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
157 bool ROSConnected = RosInstance->IsROSConnected();
158
159 // Create UROSHandle UObject
160 if (!ROSHandle)
161 {
162 ROSHandle = NewObject<UROSHandler>();
163
164 if (ROSHandle)
165 {
166 ROSHandle->Setup(ROSConnected);
167 if (RosInstance->bConnectToROS && !ROSConnected && AttemptToLaunchROSBridge)
168 {
170
171 // Only try to attempt once.
172 // Otherwise if ROS is not connected, simulator attempts to launch ROS Bridge
173 // process each time map is changed.
175 }
176 }
177 }
178
179 // Spawn Lidar Mananger. This actor should always exists in the scene.
180 // This actor only ticks when there is Lidar sensor(s) in the world.
182 {
183 LidarManager = World->SpawnActor<ALidarManager>(LidarManagerClass);
184 }
185 else
186 {
187 UE_LOG(LogTemp, Error, TEXT("Missing LidarManager class or it already exists."));
188 }
189
191 {
192 // Try to find from the world, if not found, spawn a new one
193 AActor* simulationLevelManagerActor = UGameplayStatics::GetActorOfClass(World, (TSubclassOf<AActor>)SimulationLevelManagerClass);
194 if (IsValid(simulationLevelManagerActor))
195 {
196 SimulationLevelManager = Cast<ASimulationLevelManager>(simulationLevelManagerActor);
197 }
198 else
199 {
201 }
202 }
203
204 if (!Weather)
205 {
206 // Try to find AWeather (BP_Weather) from the level.
207 TArray<AActor*> WeatherActors;
208 UGameplayStatics::GetAllActorsOfClass(World, AWeather::StaticClass(), WeatherActors);
209 if (!WeatherActors.IsEmpty())
210 {
211 Weather = Cast<AWeather>(WeatherActors[0]);
212
213#if WITH_EDITOR
214 if (WeatherActors.Num() >= 2)
215 {
216 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Weather blueprints. Make sure to only use one."));
217 }
218#endif
219 }
220 }
221
222 if (!InstancedRenderer)
223 {
224 TArray<AActor*> InstancedRendererActors;
225 UGameplayStatics::GetAllActorsOfClass(World, AInstancedRendererManager::StaticClass(), InstancedRendererActors);
226 if (!InstancedRendererActors.IsEmpty())
227 {
228 InstancedRenderer = Cast<AInstancedRendererManager>(InstancedRendererActors[0]);
229 }
230 else
231 {
232 InstancedRenderer = World->SpawnActor<AInstancedRendererManager>();
233 }
234 }
235}
TSubclassOf< ASimulationLevelManager > SimulationLevelManagerClass
TSubclassOf< ALidarManager > LidarManagerClass
static void Create()
void Setup(UWorld *World)
static UAgrarsenseSettings * GetAgrarsenseSettings()
void Setup(bool RosIsConnected)
Definition: ROSHandler.cpp:11
void LaunchROSBridge()
Definition: ROSHandler.cpp:21

References AttemptToLaunchROSBridge, SimulatorLog::Create(), UAgrarsenseSettings::GetAgrarsenseSettings(), InstancedRenderer, UROSHandler::LaunchROSBridge(), LidarManager, LidarManagerClass, SimulatorLog::Log(), ROSHandle, UROSHandler::Setup(), UAgrarsenseSettings::Setup(), SimulationLevelManager, SimulationLevelManagerClass, StartMessageLogged, and Weather.

Referenced by InitGame().

Member Data Documentation

◆ AttemptToLaunchROSBridge

bool AAgrarsenseGameModeBase::AttemptToLaunchROSBridge = true
staticprivate

Definition at line 196 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ DeveloperTools

TSubclassOf<UUserWidget> AAgrarsenseGameModeBase::DeveloperTools

DeveloperTools widget. Created when simulation starts. This must be setup in AgrarsenseGameMode in Unreal editor.

Definition at line 123 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ InstancedRenderer

AInstancedRendererManager* AAgrarsenseGameModeBase::InstancedRenderer = nullptr
private

Definition at line 182 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ LidarManager

ALidarManager* AAgrarsenseGameModeBase::LidarManager = nullptr
private

Definition at line 173 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ LidarManagerClass

TSubclassOf<ALidarManager> AAgrarsenseGameModeBase::LidarManagerClass

LidarManager class. Spawned when simulation starts. This must be setup in AgrarsenseGameMode in Unreal editor.

Definition at line 116 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ MapChanged

bool AAgrarsenseGameModeBase::MapChanged = false
staticprivate

Definition at line 200 of file AgrarsenseGameModeBase.h.

Referenced by ParseMapChangeLaunchArgument().

◆ ROSCommands

UROSCommands* AAgrarsenseGameModeBase::ROSCommands = nullptr
private

Definition at line 188 of file AgrarsenseGameModeBase.h.

Referenced by EndPlay(), and SpawnNeededActorsBeginPlay().

◆ ROSHandle

UROSHandler* AAgrarsenseGameModeBase::ROSHandle = nullptr
private

Definition at line 176 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ SimulationLevelManager

ASimulationLevelManager* AAgrarsenseGameModeBase::SimulationLevelManager = nullptr
private

Definition at line 179 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ SimulationLevelManagerClass

TSubclassOf<ASimulationLevelManager> AAgrarsenseGameModeBase::SimulationLevelManagerClass

Simulation level manager class. Spawned when simulation starts. This must be setup in AgrarsenseGameMode in Unreal editor.

Definition at line 130 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ Spectator

ASpectator* AAgrarsenseGameModeBase::Spectator = nullptr
private

Definition at line 191 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ StartMessageLogged

bool AAgrarsenseGameModeBase::StartMessageLogged = false
staticprivate

Definition at line 198 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ Tagger

ATagger* AAgrarsenseGameModeBase::Tagger = nullptr
private

Definition at line 194 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ Weather

AWeather* AAgrarsenseGameModeBase::Weather = nullptr
private

Definition at line 185 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().


The documentation for this class was generated from the following files: