17#include "GameFramework/Controller.h"
34 UE_LOG(LogCarla, Warning, TEXT(
"Invalid definition '%s' ignored"), *Definition.
Id);
43 return ActorFactory.
SpawnActor(Transform, Description);
49 const FTransform &Transform,
55 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
56 return MakeTuple(EActorSpawnResultStatus::InvalidDescription,
nullptr);
59 UE_LOG(LogCarla, Log, TEXT(
"Spawning actor '%s'"), *Description.
Id);
64 if ((Result.
Status == EActorSpawnResultStatus::Success) && (Result.
Actor ==
nullptr))
66 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Trying to spawn '%s'"), *Description.
Id);
67 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Reported success but did not return an actor"));
68 Result.
Status = EActorSpawnResultStatus::UnknownError;
75 UE_LOG(LogCarla, Warning, TEXT(
"Failed to spawn actor '%s'"), *Description.
Id);
76 check(Result.
Status != EActorSpawnResultStatus::Success);
83 return MakeTuple(Result.
Status, View);
87 const FTransform &Transform,
92 UE_LOG(LogCarla, Error, TEXT(
"Invalid ActorDescription '%s' (UId=%d)"), *Description.
Id, Description.
UId);
96 UE_LOG(LogCarla, Log, TEXT(
"Spawning actor '%s'"), *Description.
Id);
101 if ((Result.
Status == EActorSpawnResultStatus::Success) && (Result.
Actor ==
nullptr))
103 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Trying to spawn '%s'"), *Description.
Id);
104 UE_LOG(LogCarla, Warning, TEXT(
"ActorSpawnResult: Reported success but did not return an actor"));
105 Result.
Status = EActorSpawnResultStatus::UnknownError;
109 if (Result.
Status == EActorSpawnResultStatus::Success)
125 UE_LOG(LogCarla, Warning, TEXT(
"Trying to destroy actor that is not in the registry"));
135 APawn* Pawn = Cast<APawn>(Actor);
136 AController* Controller = (Pawn !=
nullptr ? Pawn->GetController() :
nullptr);
137 if (Controller !=
nullptr)
139 UE_LOG(LogCarla, Log, TEXT(
"Destroying actor's controller: '%s'"), *Id);
140 bool Success = Controller->Destroy();
143 UE_LOG(LogCarla, Error, TEXT(
"Failed to destroy actor's controller: '%s'"), *Id);
148 UE_LOG(LogCarla, Log, TEXT(
"UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor);
149 UE_LOG(LogCarla, Log, TEXT(
" %s"), Actor?*Actor->GetName():*FString(
"None"));
150 if (!Actor || !Actor->Destroy())
152 UE_LOG(LogCarla, Error, TEXT(
"Failed to destroy actor: '%s'"), *Id);
173 #if defined(WITH_ROS2)
175 if (ROS2->IsEnabled())
181 if (Attr.Key ==
"ros_name")
183 RosName = std::string(TCHAR_TO_UTF8(*Attr.Value.Value));
186 const std::string
id = std::string(TCHAR_TO_UTF8(*Description.
Id));
188 if(RosName.find(
"vehicle") != std::string::npos)
190 std::string VehicleName =
"vehicle" + std::to_string(View->
GetActorId());
191 ROS2->AddActorRosName(
static_cast<void*
>(&Actor), VehicleName);
195 size_t pos = RosName.find_last_of(
'.');
196 if (pos != std::string::npos) {
197 std::string lastToken = RosName.substr(pos + 1) +
"__";
198 ROS2->AddActorRosName(
static_cast<void*
>(&Actor), lastToken);
202 ROS2->AddActorRosName(
static_cast<void*
>(&Actor), RosName);
208 if (Attr.Key ==
"role_name" && (Attr.Value.Value ==
"hero" || Attr.Value.Value ==
"ego"))
214 boost::variant2::visit(Handler, Data);
245 #if defined(WITH_ROS2)
247 if (ROS2->IsEnabled())
249 ROS2->RemoveActorRosName(
reinterpret_cast<void *
>(Actor));
Base class for Carla actor factories.
virtual TArray< FActorDefinition > GetDefinitions()
Retrieve the list of actor definitions that this class is able to spawn.
virtual FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription)
Spawn an actor based on ActorDescription and Transform.
static void TagActor(const AActor &Actor, bool bTagForSemanticSegmentation)
Set the tag of an actor.
FCarlaActor::IdType IdType
FCarlaActor * Register(AActor &Actor, FActorDescription Description, IdType DesiredId=0)
Register the Actor in the database.
void Deregister(IdType Id)
FCarlaActor * FindCarlaActor(IdType Id)
void PutActorToSleep(IdType Id, UCarlaEpisode *CarlaEpisode)
void WakeActorUp(IdType Id, UCarlaEpisode *CarlaEpisode)
A view over an actor and its properties.
const FActorInfo * GetActorInfo() const
IdType GetActorId() const
static bool CheckActorDefinition(const FActorDefinition &ActorDefinitions)
Return whether the actor definition is valid. Prints all the errors found.
void OnActorDestroyed(AActor *Actor)
TFunction< FActorSpawnResult(const FTransform &, const FActorDescription &)> SpawnFunctionType
void Bind(FActorDefinition Definition, SpawnFunctionType SpawnFunction)
Bind a definition to a spawn function.
void PutActorToSleep(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode *CarlaEpisode)
FCarlaActor * RegisterActor(AActor &Actor, FActorDescription ActorDescription, FActorRegistry::IdType DesiredId=0)
Register an actor that was not created using "SpawnActor" function but that should be kept in the reg...
bool DestroyActor(FCarlaActor::IdType ActorId)
Destroys an actor, properly removing it from the registry.
AActor * ReSpawnActor(const FTransform &Transform, FActorDescription ActorDescription)
ReSpawns an actor based on ActorDescription at Transform.
TPair< EActorSpawnResultStatus, FCarlaActor * > SpawnActor(const FTransform &Transform, FActorDescription ActorDescription, FCarlaActor::IdType DesiredId=0)
Spawns an actor based on ActorDescription at Transform.
TArray< FActorDefinition > Definitions
TArray< SpawnFunctionType > SpawnFunctions
TArray< TSubclassOf< AActor > > Classes
static std::shared_ptr< ROS2 > GetInstance()
boost::variant2::variant< VehicleControl > ROS2CallbackData
A definition of a Carla Actor with all the variation and attributes.
FString Id
Display ID that identifies the actor.
uint32 UId
Uniquely identifies the definition (no need to fill it).
TSubclassOf< AActor > Class
Class of the actor to be spawned (Optional).
A description of a Carla Actor with all its variation.
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
TSubclassOf< AActor > Class
Class of the actor to be spawned.
uint32 UId
UId of the definition in which this description was based.
FString Id
Display ID that identifies the actor.
FActorDescription Description
Result of an actor spawn function.
EActorSpawnResultStatus Status