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

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

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

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

122{
124 {
126 SimulatorLog::Log("Simulator starting.");
127 StartMessageLogged = true;
128 }
129
130 UWorld* World = GetWorld();
131
132 // Setup Agrarsense settings
134 if (Settings)
135 {
136 Settings->Setup(World);
137 }
138
139 UROSIntegrationGameInstance* RosInstance = Cast<UROSIntegrationGameInstance>(World->GetGameInstance());
140 bool ROSConnected = RosInstance->IsROSConnected();
141
142 // Create UROSHandle UObject
143 if (!ROSHandle)
144 {
145 ROSHandle = NewObject<UROSHandler>();
146
147 if (ROSHandle)
148 {
149 ROSHandle->Setup(ROSConnected);
150 if (RosInstance->bConnectToROS && !ROSConnected && AttemptToLaunchROSBridge)
151 {
153
154 // Only try to attempt once.
155 // Otherwise if ROS is not connected, simulator attempts to launch ROS Bridge
156 // process each time map is changed.
158 }
159 }
160 }
161
162 // Spawn Lidar Mananger. This actor should always exists in the scene.
163 // This actor only ticks when there is Lidar sensor(s) in the world.
165 {
166 LidarManager = World->SpawnActor<ALidarManager>(LidarManagerClass);
167 }
168 else
169 {
170 UE_LOG(LogTemp, Error, TEXT("Missing LidarManager class or it already exists."));
171 }
172
174 {
175 // Try to find from the world, if not found, spawn a new one
176 AActor* simulationLevelManagerActor = UGameplayStatics::GetActorOfClass(World, (TSubclassOf<AActor>)SimulationLevelManagerClass);
177 if (IsValid(simulationLevelManagerActor))
178 {
179 SimulationLevelManager = Cast<ASimulationLevelManager>(simulationLevelManagerActor);
180 }
181 else
182 {
184 }
185 }
186
187 if (!Weather)
188 {
189 // Try to find AWeather (BP_Weather) from the level.
190 TArray<AActor*> WeatherActors;
191 UGameplayStatics::GetAllActorsOfClass(World, AWeather::StaticClass(), WeatherActors);
192 if (!WeatherActors.IsEmpty())
193 {
194 Weather = Cast<AWeather>(WeatherActors[0]);
195
196#if WITH_EDITOR
197 if (WeatherActors.Num() >= 2)
198 {
199 UE_LOG(LogTemp, Warning, TEXT("AAgrarsenseGameModeBase.cpp: Level has multiple Weather blueprints. Make sure to only use one."));
200 }
201#endif
202 }
203 }
204
205 if (!InstancedRenderer)
206 {
207 TArray<AActor*> InstancedRendererActors;
208 UGameplayStatics::GetAllActorsOfClass(World, AInstancedRenderer::StaticClass(), InstancedRendererActors);
209 if (!InstancedRendererActors.IsEmpty())
210 {
211 InstancedRenderer = Cast<AInstancedRenderer>(InstancedRendererActors[0]);
212 }
213 else
214 {
215 InstancedRenderer = World->SpawnActor<AInstancedRenderer>();
216 }
217 }
218}
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: