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 ()
 
AInstancedRendererGetInstancedRenderer ()
 
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
 
AInstancedRendererInstancedRenderer = nullptr
 
UAgrarsenseConsoleCommandsAgrarsenseCommands = nullptr
 
AWeatherWeather = nullptr
 
UROSCommandsROSCommands = nullptr
 
ASpectatorSpectator = nullptr
 
ATaggerTagger = nullptr
 

Static Private Attributes

static bool StartMessageLogged = false
 
static bool MapChanged = false
 

Detailed Description

Definition at line 29 of file AgrarsenseGameModeBase.h.

Constructor & Destructor Documentation

◆ AAgrarsenseGameModeBase()

AAgrarsenseGameModeBase::AAgrarsenseGameModeBase ( const FObjectInitializer &  ObjectInitializer)

Definition at line 32 of file AgrarsenseGameModeBase.cpp.

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

Member Function Documentation

◆ BeginPlay()

void AAgrarsenseGameModeBase::BeginPlay ( )
overrideprotected

Entry point to the simulation after InitGame.

Definition at line 44 of file AgrarsenseGameModeBase.cpp.

45{
46 Super::BeginPlay();
47
49
51}

References ParseMapChangeLaunchArgument(), and SpawnNeededActorsBeginPlay().

◆ EndPlay()

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

Called when simulation ends.

Definition at line 53 of file AgrarsenseGameModeBase.cpp.

54{
55 Super::EndPlay(EndPlayReason);
56
57 if (EndPlayReason == EEndPlayReason::Type::Quit || EndPlayReason == EEndPlayReason::Type::EndPlayInEditor)
58 {
60
61 if (ROSCommands)
62 {
64 }
65
66 SimulatorLog::Log("Simulator shutting down. Goodbye.");
68 }
69
70#if WITH_EDITOR
71 if (EndPlayReason == EEndPlayReason::Type::EndPlayInEditor)
72 {
73 EditorEndPlayCleanup();
74 }
75#endif
76}
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()

AInstancedRenderer * AAgrarsenseGameModeBase::GetInstancedRenderer ( )
inline

Get Instanced Renderer pointer.

Returns
AInstancedRenderer actor pointer or nullptr

Definition at line 73 of file AgrarsenseGameModeBase.h.

74 {
75 return InstancedRenderer;
76 }
AInstancedRenderer * 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 43 of file AgrarsenseGameModeBase.h.

44 {
45 return LidarManager;
46 }

Referenced by UAgrarsenseStatics::GetLidarManager().

◆ GetROSCommands()

UROSCommands * AAgrarsenseGameModeBase::GetROSCommands ( )
inline

Get UROSCommands pointer.

Returns
UROSCommands pointer or nullptr

Definition at line 103 of file AgrarsenseGameModeBase.h.

104 {
105 return ROSCommands;
106 }

◆ GetROSHandle()

UROSHandler * AAgrarsenseGameModeBase::GetROSHandle ( )
inline

Get the ROS Handler instance

Returns
Pointer to the ROS Handler or nullptr

Definition at line 53 of file AgrarsenseGameModeBase.h.

54 {
55 return ROSHandle;
56 }

Referenced by UAgrarsenseStatics::GetROSHandle().

◆ GetSimulationLevelManager()

ASimulationLevelManager * AAgrarsenseGameModeBase::GetSimulationLevelManager ( )
inline

Get simulation level's manager

Returns
Simulation level manager pointer or nullptr

Definition at line 63 of file AgrarsenseGameModeBase.h.

64 {
66 }
ASimulationLevelManager * SimulationLevelManager

Referenced by UAgrarsenseStatics::GetSimulationLevelManager().

◆ GetSpectator()

ASpectator * AAgrarsenseGameModeBase::GetSpectator ( )
inline

Get ASpectator pointer.

Returns
AWeather pointer or nullptr

Definition at line 93 of file AgrarsenseGameModeBase.h.

94 {
95 return Spectator;
96 }

Referenced by UAgrarsenseStatics::GetSpectator().

◆ GetWeatherActor()

AWeather * AAgrarsenseGameModeBase::GetWeatherActor ( )
inline

Get AWeather pointer.

Returns
AWeather pointer or nullptr

Definition at line 83 of file AgrarsenseGameModeBase.h.

84 {
85 return Weather;
86 }

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 37 of file AgrarsenseGameModeBase.cpp.

38{
39 Super::InitGame(MapName, Options, ErrorMessage);
40
42}

References SpawnNeededActorsInitGame().

◆ ParseMapChangeLaunchArgument()

void AAgrarsenseGameModeBase::ParseMapChangeLaunchArgument ( )
private

Parse launch arguments after begin play.

Definition at line 92 of file AgrarsenseGameModeBase.cpp.

93{
94 if (MapChanged)
95 {
96 return;
97 }
98
99 FString LevelName;
100 if (FParse::Param(FCommandLine::Get(), TEXT("-vindeln")) && !MapChanged)
101 {
102 LevelName = "/Game/Agrarsense/Maps/Vindeln/Vindeln";
103 }
104 else if (FParse::Param(FCommandLine::Get(), TEXT("-rovaniemi")) && !MapChanged)
105 {
106 LevelName = "/Game/Agrarsense/Maps/RovaniemiForest/RovaniemiForest";
107 }
108 else if (FParse::Param(FCommandLine::Get(), TEXT("-playground")) && !MapChanged)
109 {
110 LevelName = "/Game/Agrarsense/Maps/Playground";
111 }
112
113 if (!LevelName.IsEmpty())
114 {
115 MapChanged = true;
116 UGameplayStatics::OpenLevel(GetWorld(), *LevelName);
117 }
118}

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 206 of file AgrarsenseGameModeBase.cpp.

207{
208 UWorld* World = GetWorld();
209
210 // Spawn developer tools widget (Ctrl + F9 to open)
211 // this cannot be spawned in InitGame!
212 if (DeveloperTools != nullptr)
213 {
214 auto developerToolsWidget = CreateWidget<UUserWidget>(World, DeveloperTools);
215 developerToolsWidget->AddToViewport(10);
216 }
217#if WITH_EDITOR
218 else
219 {
220 UE_LOG(LogTemp, Error, TEXT("Missing DeveloperTools class! Please open AgrarsenseGameMode in Unreal and add it."));
221 }
222#endif
223
224 if (ROSCommands == nullptr)
225 {
226 ROSCommands = NewObject<UROSCommands>();
227 ROSCommands->Init();
228 }
229
231
232 // try to get Spectator actor
233 TArray<AActor*> Spectators;
234 UGameplayStatics::GetAllActorsOfClass(World, ASpectator::StaticClass(), Spectators);
235 if (!Spectators.IsEmpty())
236 {
237 Spectator = Cast<ASpectator>(Spectators[0]);
238
239#if WITH_EDITOR
240 if (Spectators.Num() >= 2)
241 {
242 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Spectator blueprints. Make sure to only use one."));
243 }
244#endif
245 }
246
247 // Destroy all existing Tagger if there's any, there should only be one.
248 TArray<AActor*> Taggers;
249 UGameplayStatics::GetAllActorsOfClass(World, ATagger::StaticClass(), Taggers);
250 for (AActor* TaggerActor : Taggers)
251 {
252 if (TaggerActor)
253 {
254 TaggerActor->Destroy();
255 }
256 }
257 Taggers.Empty();
258
259 Tagger = World->SpawnActor<ATagger>();
260
261
262 if (World && GEngine && GEngine->GameViewport)
263 {
264 // Create and add a drag-and-drop overlay widget to the main viewport (Spectator).
265 // Note: All widgets block the drag-and-drop interaction.
266 // To ensure smooth usage, try dragging the file into an area without any visible widgets.
267 // If the cursor shows a cancel icon, it indicates a hidden widget is obstructing the view.
268 TSharedPtr<SDragAndDropWidget> OverlayWidget = SNew(SDragAndDropWidget);
269 GEngine->GameViewport->AddViewportWidgetContent(OverlayWidget.ToSharedRef());
270 }
271}
TSubclassOf< UUserWidget > DeveloperTools
Definition: Tagger.h:29
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 120 of file AgrarsenseGameModeBase.cpp.

121{
123 {
125 SimulatorLog::Log("Simulator starting.");
126 StartMessageLogged = true;
127 }
128
129 UWorld* World = GetWorld();
130
131 // Create AgrarsenseConsoleCommands UObject
132 if (AgrarsenseCommands == nullptr)
133 {
134 AgrarsenseCommands = NewObject<UAgrarsenseConsoleCommands>();
135 }
136
137 // Setup Agrarsense settings
139 if (Settings)
140 {
141 Settings->Setup(World);
142 }
143
144 UROSIntegrationGameInstance* RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
145 bool ROSConnected = RosInstance->IsROSConnected();
146
147 // Create UROSHandle UObject
148 if (ROSHandle == nullptr)
149 {
150 ROSHandle = NewObject<UROSHandler>();
151 ROSHandle->Setup(ROSConnected);
152 }
153
154 if (ROSHandle && RosInstance->bConnectToROS && !ROSConnected)
155 {
157 }
158
159 // Spawn Lidar Mananger. This actor should always exists in the scene.
160 // This actor only ticks when there is Lidar sensor(s) in the world.
161 if (LidarManagerClass != nullptr)
162 {
163 LidarManager = World->SpawnActor<ALidarManager>(LidarManagerClass);
164 }
165 else
166 {
167 UE_LOG(LogTemp, Error, TEXT("Missing LidarManager class! Please open AgrarsenseGameMode in Unreal and add it."));
168 }
169
170 if (SimulationLevelManager == nullptr)
171 {
172 // Try to find from the world, if not found, spawn a new one
173 AActor* simulationLevelManagerActor = UGameplayStatics::GetActorOfClass(World, (TSubclassOf<AActor>)SimulationLevelManagerClass);
174 if (IsValid(simulationLevelManagerActor))
175 {
176 SimulationLevelManager = Cast<ASimulationLevelManager>(simulationLevelManagerActor);
177 }
178 else
179 {
181 }
182 }
183
184 // Try to find AWeather (BP_Weather) from the level.
185 TArray<AActor*> WeatherActors;
186 UGameplayStatics::GetAllActorsOfClass(World, AWeather::StaticClass(), WeatherActors);
187 if (!WeatherActors.IsEmpty())
188 {
189 Weather = Cast<AWeather>(WeatherActors[0]);
190
191#if WITH_EDITOR
192 if (WeatherActors.Num() >= 2)
193 {
194 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Weather blueprints. Make sure to only use one."));
195 }
196#endif
197 }
198
199 // Spawn Instanced Renderer to the world.
200 if (InstancedRenderer == nullptr)
201 {
202 InstancedRenderer = World->SpawnActor<AInstancedRenderer>();
203 }
204}
TSubclassOf< ASimulationLevelManager > SimulationLevelManagerClass
UAgrarsenseConsoleCommands * AgrarsenseCommands
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 AgrarsenseCommands, 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

◆ AgrarsenseCommands

UAgrarsenseConsoleCommands* AAgrarsenseGameModeBase::AgrarsenseCommands = nullptr
private

Definition at line 185 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 120 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ InstancedRenderer

AInstancedRenderer* 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 113 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ MapChanged

bool AAgrarsenseGameModeBase::MapChanged = false
staticprivate

Definition at line 170 of file AgrarsenseGameModeBase.h.

Referenced by ParseMapChangeLaunchArgument().

◆ ROSCommands

UROSCommands* AAgrarsenseGameModeBase::ROSCommands = nullptr
private

Definition at line 191 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 127 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ Spectator

ASpectator* AAgrarsenseGameModeBase::Spectator = nullptr
private

Definition at line 194 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ StartMessageLogged

bool AAgrarsenseGameModeBase::StartMessageLogged = false
staticprivate

Definition at line 169 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().

◆ Tagger

ATagger* AAgrarsenseGameModeBase::Tagger = nullptr
private

Definition at line 197 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsBeginPlay().

◆ Weather

AWeather* AAgrarsenseGameModeBase::Weather = nullptr
private

Definition at line 188 of file AgrarsenseGameModeBase.h.

Referenced by SpawnNeededActorsInitGame().


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