CARLA
TriggerFactory.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "Carla.h"
9 
16 
17 // =============================================================================
18 // -- ATriggerFactory -----------------------------------------------------------
19 // =============================================================================
20 
21 TArray<FActorDefinition> ATriggerFactory::GetDefinitions()
22 {
23  FActorDefinition TriggerDefinition = FActorDefinition();
24  TriggerDefinition.Class = AFrictionTrigger::StaticClass();
25 
26  TArray<FActorDefinition> TriggerDefinitions;
27 
28  bool Success;
29  UActorBlueprintFunctionLibrary::MakeTriggerDefinition(TEXT("friction"), Success, TriggerDefinition);
30  check(Success);
31  TriggerDefinitions.Add(TriggerDefinition);
32 
33  return TriggerDefinitions;
34 }
35 
37  const FTransform &Transform,
38  const FActorDescription &Description)
39 {
40  auto *World = GetWorld();
41  if (World == nullptr)
42  {
43  UE_LOG(LogCarla, Error, TEXT("ATriggerFactory: cannot spawn trigger into an empty world."));
44  return {};
45  }
46 
48  if (GameInstance == nullptr)
49  {
50  UE_LOG(LogCarla, Error, TEXT("ATriggerFactory:: cannot spawn trigger, incompatible game instance."));
51  return {};
52  }
53 
54  auto *Trigger = World->SpawnActorDeferred<AFrictionTrigger>(
55  Description.Class,
56  Transform,
57  this,
58  nullptr,
59  ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
60 
61  if (Trigger == nullptr)
62  {
63  UE_LOG(LogCarla, Error, TEXT("ATriggerFactory:: spawn trigger failed."));
64  }
65  else
66  {
67  // Retrieve Episode
68  auto *Episode = GameInstance->GetCarlaEpisode();
69  check(Episode != nullptr);
70  Trigger->SetEpisode(*Episode);
71 
72  // Retrieve Friction
74  Description.Variations,
75  3.5f);
76  Trigger->SetFriction(Friction);
77 
78  // Retrieve Extent
79  FVector Extent {100.0f, 100.0f, 100.0f};
80 
82  Description.Variations,
83  Extent.X);
85  Description.Variations,
86  Extent.Y);
88  Description.Variations,
89  Extent.Z);
90 
91  Trigger->SetBoxExtent(Extent);
92  }
93  UGameplayStatics::FinishSpawningActor(Trigger, Transform);
94  return FActorSpawnResult{Trigger};
95 }
The game instance contains elements that must be kept alive in between levels.
static float RetrieveActorAttributeToFloat(const FString &Id, const TMap< FString, FActorAttribute > &Attributes, float Default)
A definition of a Carla Actor with all the variation and attributes.
TArray< FActorDefinition > GetDefinitions() final
Retrieve the definitions of all the sensors registered in the SensorRegistry.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
static UCarlaGameInstance * GetGameInstance(const UObject *WorldContextObject)
Definition: CarlaStatics.h:63
static void MakeTriggerDefinition(const FString &Id, bool &Success, FActorDefinition &Definition)
TSubclassOf< AActor > Class
Class of the actor to be spawned.
A description of a Carla Actor with all its variation.
FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription) final
Spawn an actor based on ActorDescription and Transform.
TSubclassOf< AActor > Class
Class of the actor to be spawned (Optional).
Result of an actor spawn function.
geom::Transform Transform
Definition: rpc/Transform.h:16
UCarlaEpisode * GetCarlaEpisode()