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

#include <AssetLibrary.h>

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

Static Public Member Functions

static AActor * SpawnFoliage (EFoliageTypes FoliageType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=true, bool SnapToGround=true)
 
static AVehicleSpawnVehicle (EVehicleTypes VehicleType, FTransform SpawnTransform, const FString &ActorName, const FString &ActorID, bool SnapAboveGround=false, float AboveOffset=150.0f, bool DestroyOverlappingActors=false)
 
static AActor * SpawnProp (EPropTypes PropType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=false, bool SnapToGround=false)
 
static AWalkerSpawnWalker (FWalkerParameters Parameters, const FString &ActorName="", const FString &ActorID="", bool StartAutomatically=true)
 
static UPropActorAssetMapDataAssetGetPropActorAssetMapDataAsset ()
 
static EPropTypes GetRandomPropType ()
 
static TArray< AInstancedActor * > GetAllAddedPropActors ()
 
static UFoliageActorAssetMapDataAssetGetFoliageActorAssetMapDataAsset ()
 
static EFoliageTypes GetRandomFoliageType ()
 
static TArray< AInstancedActor * > GetAllAddedFoliageActors ()
 
static TArray< FVehicleDataGetSpawnedVehicles ()
 
static void DestroyAllWalkers ()
 
static TArray< AWalker * > GetAllWalkers ()
 
static UWalkerActorAssetMapDataAssetGetWalkerActorAssetMapDataAsset ()
 

Static Private Member Functions

static AActor * TrySpawnActor (TSubclassOf< AActor > ActorClass, FTransform &Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod=ESpawnActorCollisionHandlingMethod::Undefined)
 
static void DestroyOverlappingActorsSphere (AActor *SpawnedActor)
 
static void SetupVehicleDataAsset ()
 
static void SetupFoliageDataAsset ()
 
static void SetupPropDataAsset ()
 
static void SetupWalkerDataAsset ()
 

Static Private Attributes

static TWeakObjectPtr< UVehicleActorAssetMapDataAssetVehicleDataAsset
 
static TArray< FVehicleDataSpawnedVehicles
 
static TWeakObjectPtr< UFoliageActorAssetMapDataAssetFoliageActorAssetMapDataAsset
 
static TMap< EFoliageTypes, FString > FoliageTypeMap
 
static TArray< FInstancedActorDataAddedFoliageActors
 
static TArray< EFoliageTypesFoliageKeys
 
static TWeakObjectPtr< UPropActorAssetMapDataAssetPropActorAssetMapDataAsset
 
static TArray< FInstancedActorDataSpawnedPropActors
 
static TMap< EPropTypes, FString > PropTypeMap
 
static TArray< EPropTypesPropKeys
 
static TWeakObjectPtr< UWalkerActorAssetMapDataAssetWalkerActorAssetMapDataAsset
 
static TArray< TWeakObjectPtr< AWalker > > SpawnedWalkers
 

Detailed Description

Library for spawning Vehicles, Foliage, Props and Walkers assets into the World.

Definition at line 53 of file AssetLibrary.h.

Member Function Documentation

◆ DestroyAllWalkers()

void UAssetLibrary::DestroyAllWalkers ( )
static

Destroy all Walkers from the World.

Definition at line 455 of file AssetLibrary.cpp.

456{
457 TArray<AWalker*> WalkersActors = GetAllWalkers();
458
459 if (WalkersActors.IsEmpty())
460 {
461 return;
462 }
463
464 for (AWalker* Walker : WalkersActors)
465 {
466 if (Walker)
467 {
468 Walker->Destroy();
469 }
470 }
471
472 WalkersActors.Empty();
473
474 FString Message = FString::Printf(TEXT("Destroyed all %d Walkers."), WalkersActors.Num());
475 SimulatorLog::Log(Message);
476}
Definition: Walker.h:28
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static TArray< AWalker * > GetAllWalkers()

References GetAllWalkers(), SimulatorLog::Log(), and Walker.

Referenced by UROSCommands::HandleDestroyAllWalkers().

◆ DestroyOverlappingActorsSphere()

void UAssetLibrary::DestroyOverlappingActorsSphere ( AActor *  SpawnedActor)
staticprivate

Definition at line 296 of file AssetLibrary.cpp.

297{
298 UWorld* World = nullptr;
299 if (GEngine && GEngine->GameViewport)
300 {
301 World = GEngine->GameViewport->GetWorld();
302 }
303
304 if (!World || !SpawnedActor)
305 {
306 return;
307 }
308
309 FVector Origin;
310 FVector BoxExtent;
311 SpawnedActor->GetActorBounds(true, Origin, BoxExtent);
312
313 float SphereRadius = BoxExtent.GetMax();
314
315 TArray<AActor*> OverlappingActors;
316 UKismetSystemLibrary::SphereOverlapActors(World, SpawnedActor->GetTransform().GetLocation(), SphereRadius, {}, nullptr, {}, OverlappingActors);
317
318 for (AActor* OverlappingActor : OverlappingActors)
319 {
320 if (OverlappingActor && OverlappingActor != SpawnedActor)
321 {
322 if (OverlappingActor->IsA(AWalker::StaticClass()) ||
323 OverlappingActor->IsA(AInstancedActor::StaticClass()) ||
324 OverlappingActor->IsA(AVehicle::StaticClass()))
325 {
326 OverlappingActor->Destroy();
327 }
328 }
329 }
330}

Referenced by SpawnVehicle().

◆ GetAllAddedFoliageActors()

TArray< AInstancedActor * > UAssetLibrary::GetAllAddedFoliageActors ( )
static

Retrieve a list of all added foliage actors within this Foliage Factory.

Returns
An array of non - null AInstancedActor pointers

Definition at line 363 of file AssetLibrary.cpp.

364{
365 AddedFoliageActors.RemoveAll([](const FInstancedActorData& ActorData)
366 {
367 return !ActorData.Actor.IsValid();
368 });
369
370 TArray<AInstancedActor*> ValidActors;
371 ValidActors.Reserve(AddedFoliageActors.Num());
372
373 for (const FInstancedActorData& ActorData : AddedFoliageActors)
374 {
375 if (ActorData.Actor.IsValid())
376 {
377 ValidActors.Add(ActorData.Actor.Get());
378 }
379 }
380
381 return ValidActors;
382}
static TArray< FInstancedActorData > AddedFoliageActors
Definition: AssetLibrary.h:176
TWeakObjectPtr< AInstancedActor > Actor

References FInstancedActorData::Actor, and AddedFoliageActors.

Referenced by UROSCommands::HandleExportFoliage().

◆ GetAllAddedPropActors()

TArray< AInstancedActor * > UAssetLibrary::GetAllAddedPropActors ( )
static

Retrieve a list of all added Prop actors within this Prop Factory.

Returns
An array of non - null AInstancedActor pointers

Definition at line 405 of file AssetLibrary.cpp.

406{
407 SpawnedPropActors.RemoveAll([](const FInstancedActorData& ActorData)
408 {
409 return !ActorData.Actor.IsValid();
410 });
411
412 TArray<AInstancedActor*> ValidActors;
413 ValidActors.Reserve(SpawnedPropActors.Num());
414
415 for (const FInstancedActorData& ActorData : SpawnedPropActors)
416 {
417 if (ActorData.Actor.IsValid())
418 {
419 ValidActors.Add(ActorData.Actor.Get());
420 }
421 }
422
423 return ValidActors;
424}
static TArray< FInstancedActorData > SpawnedPropActors
Definition: AssetLibrary.h:181

References FInstancedActorData::Actor, and SpawnedPropActors.

Referenced by UROSCommands::HandleExportProps().

◆ GetAllWalkers()

TArray< AWalker * > UAssetLibrary::GetAllWalkers ( )
static

Get All Walkers from the World.

Definition at line 433 of file AssetLibrary.cpp.

434{
435 SpawnedWalkers.RemoveAll([](const TWeakObjectPtr<AWalker>& ActorData)
436 {
437 return !ActorData.IsValid();
438 });
439
440 TArray<AWalker*> ValidWalkers;
441 ValidWalkers.Reserve(SpawnedWalkers.Num());
442
443 for (const TWeakObjectPtr<AWalker>& ActorData : SpawnedWalkers)
444 {
445 AWalker* WalkerPtr = ActorData.Get();
446 if (WalkerPtr)
447 {
448 ValidWalkers.Add(WalkerPtr);
449 }
450 }
451
452 return ValidWalkers;
453}
static TArray< TWeakObjectPtr< AWalker > > SpawnedWalkers
Definition: AssetLibrary.h:187

References SpawnedWalkers.

Referenced by DestroyAllWalkers(), and UROSCommands::HandleExportWalkers().

◆ GetFoliageActorAssetMapDataAsset()

UFoliageActorAssetMapDataAsset * UAssetLibrary::GetFoliageActorAssetMapDataAsset ( )
static

Definition at line 342 of file AssetLibrary.cpp.

343{
345
347}
static TWeakObjectPtr< UFoliageActorAssetMapDataAsset > FoliageActorAssetMapDataAsset
Definition: AssetLibrary.h:174
static void SetupFoliageDataAsset()

References FoliageActorAssetMapDataAsset, and SetupFoliageDataAsset().

◆ GetPropActorAssetMapDataAsset()

UPropActorAssetMapDataAsset * UAssetLibrary::GetPropActorAssetMapDataAsset ( )
static

Definition at line 384 of file AssetLibrary.cpp.

385{
387
388 return PropActorAssetMapDataAsset.Get();
389}
static void SetupPropDataAsset()
static TWeakObjectPtr< UPropActorAssetMapDataAsset > PropActorAssetMapDataAsset
Definition: AssetLibrary.h:180

References PropActorAssetMapDataAsset, and SetupPropDataAsset().

◆ GetRandomFoliageType()

EFoliageTypes UAssetLibrary::GetRandomFoliageType ( )
static

Definition at line 349 of file AssetLibrary.cpp.

350{
352
353 EFoliageTypes RandomFoliageType = EFoliageTypes::NONE;
354 if (FoliageKeys.Num())
355 {
356 int32 RandomIndex = FMath::RandRange(0, FoliageKeys.Num() - 1);
357 RandomFoliageType = FoliageKeys[RandomIndex];
358 }
359
360 return RandomFoliageType;
361}
EFoliageTypes
Definition: FoliageTypes.h:15
static TArray< EFoliageTypes > FoliageKeys
Definition: AssetLibrary.h:177

References FoliageKeys, NONE, and SetupFoliageDataAsset().

◆ GetRandomPropType()

EPropTypes UAssetLibrary::GetRandomPropType ( )
static

Definition at line 391 of file AssetLibrary.cpp.

392{
394
395 EPropTypes RandomPropType = EPropTypes::NONE;
396 if (PropKeys.Num())
397 {
398 int32 RandomIndex = FMath::RandRange(0, PropKeys.Num() - 1);
399 RandomPropType = PropKeys[RandomIndex];
400 }
401
402 return RandomPropType;
403}
EPropTypes
Definition: PropTypes.h:15
static TArray< EPropTypes > PropKeys
Definition: AssetLibrary.h:183

References NONE, PropKeys, and SetupPropDataAsset().

◆ GetSpawnedVehicles()

TArray< FVehicleData > UAssetLibrary::GetSpawnedVehicles ( )
static

Retrieves an array of spawned vehicles.

Definition at line 332 of file AssetLibrary.cpp.

333{
334 SpawnedVehicles.RemoveAll([](const FVehicleData& VehicleData)
335 {
336 return !VehicleData.Vehicle.IsValid();
337 });
338
339 return SpawnedVehicles;
340}
static TArray< FVehicleData > SpawnedVehicles
Definition: AssetLibrary.h:171
TWeakObjectPtr< AVehicle > Vehicle
Definition: AssetLibrary.h:40

References SpawnedVehicles, and FVehicleData::Vehicle.

Referenced by UROSCommands::HandleExportVehicles().

◆ GetWalkerActorAssetMapDataAsset()

UWalkerActorAssetMapDataAsset * UAssetLibrary::GetWalkerActorAssetMapDataAsset ( )
static

Definition at line 426 of file AssetLibrary.cpp.

427{
429
430 return WalkerActorAssetMapDataAsset.Get();
431}
static void SetupWalkerDataAsset()
static TWeakObjectPtr< UWalkerActorAssetMapDataAsset > WalkerActorAssetMapDataAsset
Definition: AssetLibrary.h:186

References SetupWalkerDataAsset(), and WalkerActorAssetMapDataAsset.

◆ SetupFoliageDataAsset()

void UAssetLibrary::SetupFoliageDataAsset ( )
staticprivate

Definition at line 488 of file AssetLibrary.cpp.

489{
490 if (FoliageActorAssetMapDataAsset.IsValid())
491 {
492 return;
493 }
494
496 FSoftObjectPath FoliageActorAssetMapDataAssetPath(FOLIAGE_ASSET_PATH);
497 FoliageActorAssetMapDataAsset = Cast<UFoliageActorAssetMapDataAsset>(FoliageActorAssetMapDataAssetPath.TryLoad());
498
499 if (FoliageActorAssetMapDataAsset.IsValid())
500 {
502 FoliageTypeMap.Empty();
503 FoliageTypeMap = UEnumUtilities::CreateEnumStringMap<EFoliageTypes>("/Script/Agrarsense.EFoliageTypes");
504 }
505}
static const FString FOLIAGE_ASSET_PATH
static TMap< EFoliageTypes, FString > FoliageTypeMap
Definition: AssetLibrary.h:175

References FOLIAGE_ASSET_PATH, FoliageActorAssetMapDataAsset, FoliageKeys, and FoliageTypeMap.

Referenced by GetFoliageActorAssetMapDataAsset(), GetRandomFoliageType(), and SpawnFoliage().

◆ SetupPropDataAsset()

void UAssetLibrary::SetupPropDataAsset ( )
staticprivate

Definition at line 507 of file AssetLibrary.cpp.

508{
509 if (PropActorAssetMapDataAsset.IsValid())
510 {
511 return;
512 }
513
514 FSoftObjectPath PropActorAssetMapDataAssetPath(PROP_ASSET_PATH);
515 PropActorAssetMapDataAsset = Cast<UPropActorAssetMapDataAsset>(PropActorAssetMapDataAssetPath.TryLoad());
516
517 if (PropActorAssetMapDataAsset.IsValid())
518 {
519 PropActorAssetMapDataAsset->Props.GetKeys(PropKeys);
520 SpawnedPropActors.Empty();
521
522 PropTypeMap.Empty();
523 PropTypeMap = UEnumUtilities::CreateEnumStringMap<EPropTypes>("/Script/Agrarsense.EPropTypes");
524 }
525}
static const FString PROP_ASSET_PATH
static TMap< EPropTypes, FString > PropTypeMap
Definition: AssetLibrary.h:182

References PROP_ASSET_PATH, PropActorAssetMapDataAsset, PropKeys, PropTypeMap, and SpawnedPropActors.

Referenced by GetPropActorAssetMapDataAsset(), GetRandomPropType(), and SpawnProp().

◆ SetupVehicleDataAsset()

void UAssetLibrary::SetupVehicleDataAsset ( )
staticprivate

Definition at line 478 of file AssetLibrary.cpp.

479{
480 if (!VehicleDataAsset.IsValid())
481 {
482 VehicleDataAsset.Reset();
483 FSoftObjectPath VehicleActorAssetMap(VEHICLE_ASSET_PATH);
484 VehicleDataAsset = Cast<UVehicleActorAssetMapDataAsset>(VehicleActorAssetMap.TryLoad());
485 }
486}
static const FString VEHICLE_ASSET_PATH
static TWeakObjectPtr< UVehicleActorAssetMapDataAsset > VehicleDataAsset
Definition: AssetLibrary.h:170

References VEHICLE_ASSET_PATH, and VehicleDataAsset.

Referenced by SpawnVehicle().

◆ SetupWalkerDataAsset()

void UAssetLibrary::SetupWalkerDataAsset ( )
staticprivate

Definition at line 527 of file AssetLibrary.cpp.

528{
529 if (!WalkerActorAssetMapDataAsset.IsValid())
530 {
531 FSoftObjectPath WalkerActorAssetMapDataAssetPath(WALKER_ASSET_PATH);
532 WalkerActorAssetMapDataAsset = Cast<UWalkerActorAssetMapDataAsset>(WalkerActorAssetMapDataAssetPath.TryLoad());
533 }
534}
static const FString WALKER_ASSET_PATH

References WALKER_ASSET_PATH, and WalkerActorAssetMapDataAsset.

Referenced by GetWalkerActorAssetMapDataAsset(), and SpawnWalker().

◆ SpawnFoliage()

AActor * UAssetLibrary::SpawnFoliage ( EFoliageTypes  FoliageType,
FTransform  Transform,
FString  ActorName = "",
FString  ActorID = "",
bool  RandomZRotation = true,
bool  SnapToGround = true 
)
static

Spawns a foliage actor at the specified transform. Returns a pointer to the spawned actor or nullptr.

Definition at line 51 of file AssetLibrary.cpp.

54{
56
57 if (!FoliageActorAssetMapDataAsset.IsValid())
58 {
59 return nullptr;
60 }
61
62 UFoliageActorAssetDataAsset* ActorAssetData = FoliageActorAssetMapDataAsset->Foliages.FindRef(FoliageType);
63 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
64
65 AActor* SpawnedActor = TrySpawnActor(ActorClass, Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
66
67 AInstancedActor* InstancedActor = Cast<AInstancedActor>(SpawnedActor);
68 if (InstancedActor)
69 {
70 if (FoliageTypeMap.Contains(FoliageType))
71 {
72 // Set InstactedActor type and model strings, this is used for JSON exporting.
73 FString Model = FoliageTypeMap[FoliageType];
74 InstancedActor->SetTypeAndModel("foliage", Model);
75 }
76
77 // Add to array
79 Data.Actor = InstancedActor;
80 AddedFoliageActors.Add(Data);
81 }
82
83 return SpawnedActor;
84}
void SetTypeAndModel(const FString &Type, const FString &Model)
static AActor * TrySpawnActor(TSubclassOf< AActor > ActorClass, FTransform &Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod=ESpawnActorCollisionHandlingMethod::Undefined)
TSubclassOf< AActor > Actor
Definition: ActorAsset.h:37

References FActorAsset::Actor, FInstancedActorData::Actor, UActorAssetDataAsset::ActorAsset, AddedFoliageActors, FoliageActorAssetMapDataAsset, FoliageTypeMap, AInstancedActor::SetTypeAndModel(), SetupFoliageDataAsset(), Transform, and TrySpawnActor().

Referenced by USimulatorJsonParser::ParsePropOrFoliage().

◆ SpawnProp()

AActor * UAssetLibrary::SpawnProp ( EPropTypes  PropType,
FTransform  Transform,
FString  ActorName = "",
FString  ActorID = "",
bool  RandomZRotation = false,
bool  SnapToGround = false 
)
static

Spawns a prop at the specified location. Returns a pointer to the spawned actor or nullptr.

Definition at line 147 of file AssetLibrary.cpp.

150{
152
153 if (!PropActorAssetMapDataAsset.IsValid())
154 {
155 return nullptr;
156 }
157
158 UPropActorAssetDataAsset* ActorAssetData = PropActorAssetMapDataAsset->Props.FindRef(PropType);
159 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
160
161 AActor* SpawnedActor = TrySpawnActor(ActorClass, Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
162
163 AInstancedActor* InstancedActor = Cast<AInstancedActor>(SpawnedActor);
164 if (InstancedActor)
165 {
166 FInstancedActorData InstancedActorData;
167 InstancedActorData.Actor = InstancedActor;
168 SpawnedPropActors.Add(InstancedActorData);
169
170 if (PropTypeMap.Contains(PropType))
171 {
172 // Set InstactedActor type and model strings, this is used for JSON exporting.
173 FString Model = PropTypeMap[PropType];
174 InstancedActor->SetTypeAndModel("prop", Model);
175 }
176
177
178 // Add to array
180 Data.Actor = InstancedActor;
181 SpawnedPropActors.Add(Data);
182 }
183
184 return SpawnedActor;
185}

References FActorAsset::Actor, FInstancedActorData::Actor, UActorAssetDataAsset::ActorAsset, PropActorAssetMapDataAsset, PropTypeMap, AInstancedActor::SetTypeAndModel(), SetupPropDataAsset(), SpawnedPropActors, Transform, and TrySpawnActor().

Referenced by USimulatorJsonParser::ParsePropOrFoliage().

◆ SpawnVehicle()

AVehicle * UAssetLibrary::SpawnVehicle ( EVehicleTypes  VehicleType,
FTransform  SpawnTransform,
const FString &  ActorName,
const FString &  ActorID,
bool  SnapAboveGround = false,
float  AboveOffset = 150.0f,
bool  DestroyOverlappingActors = false 
)
static

Spawns a vehicle at the specified transform. Returns a pointer to the spawned vehicle or nullptr.

Definition at line 86 of file AssetLibrary.cpp.

90{
92
93 if (!VehicleDataAsset.IsValid())
94 {
95 return nullptr;
96 }
97
98 UVehicleActorAssetDataAsset* ActorAssetData = VehicleDataAsset->Vehicles.FindRef(VehicleType);
99 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
100
101 ESpawnActorCollisionHandlingMethod SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::Undefined;
102 if (VehicleType == EVehicleTypes::Drone)
103 {
104 // Workaround for drone
105 SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
106 }
107
108 FString ID = ActorID;
109 if (ID.IsEmpty())
110 {
112 }
113
114 AActor* SpawnedActor = TrySpawnActor(ActorClass, SpawnTransform, ActorName, ID, false, false, SpawnCollisionHandling);
115
116 AVehicle* Vehicle = Cast<AVehicle>(SpawnedActor);
117 if (SpawnedActor && Vehicle)
118 {
119 if (SnapAboveGround)
120 {
122 SpawnTransform = SpawnedActor->GetTransform();
123 }
124
125 FVehicleData VehicleData;
126 VehicleData.Vehicle = Vehicle;
127 VehicleData.VehicleType = VehicleType;
128 SpawnedVehicles.Add(VehicleData);
129
130 Vehicle->TeleportTo(SpawnTransform.GetLocation(), SpawnTransform.GetRotation().Rotator());
131
132 if (DestroyOverlappingActors)
133 {
135 }
136
137 // Add to array
138 FVehicleData Data;
139 Data.Vehicle = Vehicle;
140 Data.VehicleType = Vehicle->GetVehicleType();
141 SpawnedVehicles.Add(Data);
142 }
143
144 return Vehicle;
145}
static void SetupVehicleDataAsset()
static void DestroyOverlappingActorsSphere(AActor *SpawnedActor)
static FString ConvertVehicleTypeToString(EVehicleTypes VehicleType)
static bool SnapActorAboveGround(AActor *Actor, float AboveOffset=50.0f)
EVehicleTypes VehicleType
Definition: AssetLibrary.h:46

References FActorAsset::Actor, UActorAssetDataAsset::ActorAsset, UEnumUtilities::ConvertVehicleTypeToString(), DestroyOverlappingActorsSphere(), Drone, SetupVehicleDataAsset(), UPhysicsUtilities::SnapActorAboveGround(), SpawnedVehicles, TrySpawnActor(), Vehicle, FVehicleData::Vehicle, VehicleDataAsset, and FVehicleData::VehicleType.

Referenced by USimulatorJsonParser::ParseVehicle().

◆ SpawnWalker()

AWalker * UAssetLibrary::SpawnWalker ( FWalkerParameters  Parameters,
const FString &  ActorName = "",
const FString &  ActorID = "",
bool  StartAutomatically = true 
)
static

Spawns a walker based on the provided parameters. Returns a pointer to the spawned walker or nullptr.

Definition at line 187 of file AssetLibrary.cpp.

189{
191
192 if (!WalkerActorAssetMapDataAsset.IsValid())
193 {
194 return nullptr;
195 }
196
197 UWalkerActorAssetDataAsset* ActorAssetData = WalkerActorAssetMapDataAsset->Walkers.FindRef(Parameters.WalkerType);
198 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->ActorAsset.Actor : nullptr;
199
200 FTransform StartTransform;
201 auto& Transforms = Parameters.Points;
202
203 if (!Transforms.IsEmpty())
204 {
205 if (Parameters.WalkerAction == EWalkerAction::FollowPath && Transforms.Num() < 2)
206 {
207 SimulatorLog::Log("Failed to spawn FollowPath Walker due to insufficient path points.");
208 return nullptr;
209 }
210 StartTransform = Transforms[0];
211 }
212
213 FString ID = ActorID;
214 if (ID.IsEmpty())
215 {
217 }
218
219 AActor* SpawnedActor = TrySpawnActor(ActorClass, StartTransform, ActorName, ID, false, false);
220
221 AWalker* Walker = Cast<AWalker>(SpawnedActor);
222 if (Walker)
223 {
225 Walker->SetWalkerParameters(Parameters);
226 Walker->SetIgnoreInput(!StartAutomatically);
227
228 // Add to array
230 }
231
232 return Walker;
233}
static FString ConvertWalkerTypeToString(EWalkerType WalkerType)
EWalkerAction WalkerAction
TArray< FTransform > Points
EWalkerType WalkerType

References FActorAsset::Actor, UActorAssetDataAsset::ActorAsset, UEnumUtilities::ConvertWalkerTypeToString(), FollowPath, SimulatorLog::Log(), FWalkerParameters::Points, SetupWalkerDataAsset(), SpawnedWalkers, TrySpawnActor(), Walker, FWalkerParameters::WalkerAction, WalkerActorAssetMapDataAsset, and FWalkerParameters::WalkerType.

Referenced by USimulatorJsonParser::SpawnWalker().

◆ TrySpawnActor()

AActor * UAssetLibrary::TrySpawnActor ( TSubclassOf< AActor >  ActorClass,
FTransform &  Transform,
FString  ActorName,
FString  ActorID,
bool  SnapToGround,
bool  RandomZRotation,
ESpawnActorCollisionHandlingMethod  CollisionMethod = ESpawnActorCollisionHandlingMethod::Undefined 
)
staticprivate

Definition at line 235 of file AssetLibrary.cpp.

238{
239 if (!IsInGameThread())
240 {
241#if WITH_EDITOR
242 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: Attempting to spawn actor from a background thread. This is not allowed."));
243#endif
244 return nullptr;
245 }
246
247 if (!ActorClass)
248 {
249#if WITH_EDITOR
250 UE_LOG(LogTemp, Warning, TEXT("AssetLibrary.cpp: ActorClass is nullptr! Cannot spawn actor."));
251#endif
252 return nullptr;
253 }
254
255 UWorld* World = nullptr;
256 if (GEngine && GEngine->GameViewport)
257 {
258 World = GEngine->GameViewport->GetWorld();
259 }
260
261 if (!World)
262 {
263 return nullptr;
264 }
265
266 // Spawn actor deferred
267 AActor* SpawnedActor = World->SpawnActorDeferred<AActor>(ActorClass, Transform, nullptr, nullptr, CollisionMethod);
268
269 if (SpawnedActor)
270 {
271 if (SnapToGround)
272 {
274 Transform = SpawnedActor->GetTransform();
275 }
276
277 if (RandomZRotation)
278 {
279 float RotationAngle = FMath::FRandRange(0.0f, 360.0f);
280 Transform.SetRotation(FQuat::MakeFromEuler(FVector(0.0f, 0.0f, RotationAngle)));
281 }
282
283 // Set ID and name through IActorInformation if the spawned actor implements it
284 if (SpawnedActor && SpawnedActor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
285 {
286 IActorInformation::Execute_SetActorIDAndName(SpawnedActor, ActorName, ActorID);
287 }
288
289 // Finish Actor spawning (BeginPlay runs now)
290 SpawnedActor->FinishSpawning(Transform);
291 }
292
293 return SpawnedActor;
294}
static bool SnapActorToGround(AActor *Actor, float StartZOffset=600.0f, float EndZOffset=600.0f)

References UPhysicsUtilities::SnapActorToGround(), and Transform.

Referenced by SpawnFoliage(), SpawnProp(), SpawnVehicle(), and SpawnWalker().

Member Data Documentation

◆ AddedFoliageActors

TArray< FInstancedActorData > UAssetLibrary::AddedFoliageActors
staticprivate

Definition at line 176 of file AssetLibrary.h.

Referenced by GetAllAddedFoliageActors(), and SpawnFoliage().

◆ FoliageActorAssetMapDataAsset

TWeakObjectPtr< UFoliageActorAssetMapDataAsset > UAssetLibrary::FoliageActorAssetMapDataAsset
staticprivate

◆ FoliageKeys

TArray< EFoliageTypes > UAssetLibrary::FoliageKeys
staticprivate

Definition at line 177 of file AssetLibrary.h.

Referenced by GetRandomFoliageType(), and SetupFoliageDataAsset().

◆ FoliageTypeMap

TMap< EFoliageTypes, FString > UAssetLibrary::FoliageTypeMap
staticprivate

Definition at line 175 of file AssetLibrary.h.

Referenced by SetupFoliageDataAsset(), and SpawnFoliage().

◆ PropActorAssetMapDataAsset

TWeakObjectPtr< UPropActorAssetMapDataAsset > UAssetLibrary::PropActorAssetMapDataAsset
staticprivate

Definition at line 180 of file AssetLibrary.h.

Referenced by GetPropActorAssetMapDataAsset(), SetupPropDataAsset(), and SpawnProp().

◆ PropKeys

TArray< EPropTypes > UAssetLibrary::PropKeys
staticprivate

Definition at line 183 of file AssetLibrary.h.

Referenced by GetRandomPropType(), and SetupPropDataAsset().

◆ PropTypeMap

TMap< EPropTypes, FString > UAssetLibrary::PropTypeMap
staticprivate

Definition at line 182 of file AssetLibrary.h.

Referenced by SetupPropDataAsset(), and SpawnProp().

◆ SpawnedPropActors

TArray< FInstancedActorData > UAssetLibrary::SpawnedPropActors
staticprivate

Definition at line 181 of file AssetLibrary.h.

Referenced by GetAllAddedPropActors(), SetupPropDataAsset(), and SpawnProp().

◆ SpawnedVehicles

TArray< FVehicleData > UAssetLibrary::SpawnedVehicles
staticprivate

Definition at line 171 of file AssetLibrary.h.

Referenced by GetSpawnedVehicles(), and SpawnVehicle().

◆ SpawnedWalkers

TArray< TWeakObjectPtr< AWalker > > UAssetLibrary::SpawnedWalkers
staticprivate

Definition at line 187 of file AssetLibrary.h.

Referenced by GetAllWalkers(), and SpawnWalker().

◆ VehicleDataAsset

TWeakObjectPtr< UVehicleActorAssetMapDataAsset > UAssetLibrary::VehicleDataAsset
staticprivate

Definition at line 170 of file AssetLibrary.h.

Referenced by SetupVehicleDataAsset(), and SpawnVehicle().

◆ WalkerActorAssetMapDataAsset

TWeakObjectPtr< UWalkerActorAssetMapDataAsset > UAssetLibrary::WalkerActorAssetMapDataAsset
staticprivate

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