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
 
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 28 of file AgrarsenseGameModeBase.h.

Constructor & Destructor Documentation

◆ AAgrarsenseGameModeBase()

AAgrarsenseGameModeBase::AAgrarsenseGameModeBase ( const FObjectInitializer &  ObjectInitializer)

Definition at line 31 of file AgrarsenseGameModeBase.cpp.

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

Member Function Documentation

◆ BeginPlay()

void AAgrarsenseGameModeBase::BeginPlay ( )
overrideprotected

Entry point to the simulation after InitGame.

Definition at line 43 of file AgrarsenseGameModeBase.cpp.

44{
45 Super::BeginPlay();
46
48
50}

References ParseMapChangeLaunchArgument(), and SpawnNeededActorsBeginPlay().

◆ EndPlay()

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

Called when simulation ends.

Definition at line 52 of file AgrarsenseGameModeBase.cpp.

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

73 {
74 return InstancedRenderer;
75 }
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 42 of file AgrarsenseGameModeBase.h.

43 {
44 return LidarManager;
45 }

Referenced by UAgrarsenseStatics::GetLidarManager().

◆ GetROSCommands()

UROSCommands * AAgrarsenseGameModeBase::GetROSCommands ( )
inline

Get UROSCommands pointer.

Returns
UROSCommands pointer or nullptr

Definition at line 102 of file AgrarsenseGameModeBase.h.

103 {
104 return ROSCommands;
105 }

◆ GetROSHandle()

UROSHandler * AAgrarsenseGameModeBase::GetROSHandle ( )
inline

Get the ROS Handler instance

Returns
Pointer to the ROS Handler or nullptr

Definition at line 52 of file AgrarsenseGameModeBase.h.

53 {
54 return ROSHandle;
55 }

Referenced by UAgrarsenseStatics::GetROSHandle().

◆ GetSimulationLevelManager()

ASimulationLevelManager * AAgrarsenseGameModeBase::GetSimulationLevelManager ( )
inline

Get simulation level's manager

Returns
Simulation level manager pointer or nullptr

Definition at line 62 of file AgrarsenseGameModeBase.h.

63 {
65 }
ASimulationLevelManager * SimulationLevelManager

Referenced by UAgrarsenseStatics::GetSimulationLevelManager().

◆ GetSpectator()

ASpectator * AAgrarsenseGameModeBase::GetSpectator ( )
inline

Get ASpectator pointer.

Returns
AWeather pointer or nullptr

Definition at line 92 of file AgrarsenseGameModeBase.h.

93 {
94 return Spectator;
95 }

Referenced by UAgrarsenseStatics::GetSpectator().

◆ GetWeatherActor()

AWeather * AAgrarsenseGameModeBase::GetWeatherActor ( )
inline

Get AWeather pointer.

Returns
AWeather pointer or nullptr

Definition at line 82 of file AgrarsenseGameModeBase.h.

83 {
84 return Weather;
85 }

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

37{
38 Super::InitGame(MapName, Options, ErrorMessage);
39
41}

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

220{
221 UWorld* World = GetWorld();
222
223 // Spawn developer tools widget (Ctrl + F9 to open)
224 // this cannot be spawned in InitGame!
225 if (DeveloperTools)
226 {
227 auto developerToolsWidget = CreateWidget<UUserWidget>(World, DeveloperTools);
228 developerToolsWidget->AddToViewport(10);
229 }
230#if WITH_EDITOR
231 else
232 {
233 UE_LOG(LogTemp, Error, TEXT("Missing DeveloperTools class! Please open AgrarsenseGameMode in Unreal and add it."));
234 }
235#endif
236
237 if (!ROSCommands)
238 {
239 ROSCommands = NewObject<UROSCommands>();
240 ROSCommands->Init();
241 }
242
244
245 if (!Spectator)
246 {
247 // try to get Spectator actor
248 TArray<AActor*> Spectators;
249 UGameplayStatics::GetAllActorsOfClass(World, ASpectator::StaticClass(), Spectators);
250 if (!Spectators.IsEmpty())
251 {
252 Spectator = Cast<ASpectator>(Spectators[0]);
253
254#if WITH_EDITOR
255 if (Spectators.Num() >= 2)
256 {
257 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Spectator blueprints. Make sure to only use one."));
258 }
259#endif
260 }
261 }
262
263 if (!Tagger)
264 {
265 // Destroy all existing Tagger if there's any, there should only be one.
266 TArray<AActor*> Taggers;
267 UGameplayStatics::GetAllActorsOfClass(World, ATagger::StaticClass(), Taggers);
268 for (AActor* TaggerActor : Taggers)
269 {
270 if (TaggerActor)
271 {
272 TaggerActor->Destroy();
273 }
274 }
275 Taggers.Empty();
276 Tagger = World->SpawnActor<ATagger>();
277 }
278
279 if (World && GEngine && GEngine->GameViewport)
280 {
281 // Create and add a drag-and-drop overlay widget to the main viewport (Spectator).
282 // Note: All widgets block the drag-and-drop interaction.
283 // To ensure smooth usage, try dragging the file into an area without any visible widgets.
284 // If the cursor shows a cancel icon, it indicates a hidden widget is obstructing the view.
285 TSharedPtr<SDragAndDropWidget> OverlayWidget = SNew(SDragAndDropWidget);
286 GEngine->GameViewport->AddViewportWidgetContent(OverlayWidget.ToSharedRef());
287 }
288}
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 // Setup Agrarsense settings
133 if (Settings)
134 {
135 Settings->Setup(World);
136 }
137
138 UROSIntegrationGameInstance* RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
139 bool ROSConnected = RosInstance->IsROSConnected();
140
141 // Create UROSHandle UObject
142 if (!ROSHandle)
143 {
144 ROSHandle = NewObject<UROSHandler>();
145
146 if (ROSHandle)
147 {
148 ROSHandle->Setup(ROSConnected);
149 if (RosInstance->bConnectToROS && !ROSConnected && AttemptToLaunchROSBridge)
150 {
152
153 // Only try to attempt once.
154 // Otherwise if ROS is not connected, simulator attempts to launch ROS Bridge
155 // process each time map is changed.
157 }
158 }
159 }
160
161 // Spawn Lidar Mananger. This actor should always exists in the scene.
162 // This actor only ticks when there is Lidar sensor(s) in the world.
164 {
165 LidarManager = World->SpawnActor<ALidarManager>(LidarManagerClass);
166 }
167 else
168 {
169 UE_LOG(LogTemp, Error, TEXT("Missing LidarManager class or it already exists."));
170 }
171
173 {
174 // Try to find from the world, if not found, spawn a new one
175 AActor* simulationLevelManagerActor = UGameplayStatics::GetActorOfClass(World, (TSubclassOf<AActor>)SimulationLevelManagerClass);
176 if (IsValid(simulationLevelManagerActor))
177 {
178 SimulationLevelManager = Cast<ASimulationLevelManager>(simulationLevelManagerActor);
179 }
180 else
181 {
183 }
184 }
185
186 if (!Weather)
187 {
188 // Try to find AWeather (BP_Weather) from the level.
189 TArray<AActor*> WeatherActors;
190 UGameplayStatics::GetAllActorsOfClass(World, AWeather::StaticClass(), WeatherActors);
191 if (!WeatherActors.IsEmpty())
192 {
193 Weather = Cast<AWeather>(WeatherActors[0]);
194
195#if WITH_EDITOR
196 if (WeatherActors.Num() >= 2)
197 {
198 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Weather blueprints. Make sure to only use one."));
199 }
200#endif
201 }
202 }
203
204 if (!InstancedRenderer)
205 {
206 TArray<AActor*> InstancedRendererActors;
207 UGameplayStatics::GetAllActorsOfClass(World, AInstancedRenderer::StaticClass(), InstancedRendererActors);
208 if (!InstancedRendererActors.IsEmpty())
209 {
210 InstancedRenderer = Cast<AInstancedRenderer>(InstancedRendererActors[0]);
211 }
212 else
213 {
214 InstancedRenderer = World->SpawnActor<AInstancedRenderer>();
215 }
216 }
217}
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 168 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 119 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 112 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 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 126 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 169 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: