CARLA
Loading...
Searching...
No Matches
ActorDispatcher.h
Go to the documentation of this file.
1// Copyright (c) 2026 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#pragma once
8
13
14#include <util/ue-header-guard-begin.h>
15#include "Containers/Array.h"
16#include "Templates/Function.h"
17#include <util/ue-header-guard-end.h>
18
19#include "ActorDispatcher.generated.h"
20
22
23/// Object in charge of binding ActorDefinitions to spawn functions, as well as
24/// keeping the registry of all the actors spawned.
25UCLASS()
26class CARLA_API UActorDispatcher : public UObject
27{
28 GENERATED_BODY()
29
30public:
31
32 using SpawnFunctionType = TFunction<FActorSpawnResult(const FTransform &, const FActorDescription &)>;
33
34 /// Bind a definition to a spawn function. When SpawnActor is called with a
35 /// matching description @a Functor is called.
36 ///
37 /// @warning Invalid definitions are ignored.
38 void Bind(FActorDefinition Definition, SpawnFunctionType SpawnFunction);
39
40 /// Bind all the definitions of @a ActorFactory to its spawn function.
41 ///
42 /// @warning Invalid definitions are ignored.
43 void Bind(ACarlaActorFactory &ActorFactory);
44
45 /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
46 /// despawn an actor created with this function call DestroyActor.
47 ///
48 /// @return A pair containing the result of the spawn function and a view over
49 /// the actor and its properties. If the status is different of Success the
50 /// view is invalid.
51 TPair<EActorSpawnResultStatus, FCarlaActor*> SpawnActor(
52 const FTransform &Transform,
53 FActorDescription ActorDescription,
54 FCarlaActor::IdType DesiredId = 0);
55
56 /// ReSpawns an actor based on @a ActorDescription at @a Transform. To properly
57 /// despawn an actor created with this function call DestroyActor.
58 /// Used to respawn dormant actors.
59 ///
60 /// @return The actor to be respawned
62 const FTransform &Transform,
63 FActorDescription ActorDescription);
64
66
67 void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode* CarlaEpisode);
68
69 /// Destroys an actor, properly removing it from the registry.
70 ///
71 /// Return true if the @a Actor is destroyed or already marked for
72 /// destruction, false if indestructible or nullptr.
73 //bool DestroyActor(AActor *Actor);
74
76
77 /// Register an actor that was not created using "SpawnActor" function but
78 /// that should be kept in the registry.
80 AActor &Actor,
81 FActorDescription ActorDescription,
82 FActorRegistry::IdType DesiredId = 0);
83
84 /// Forget the classes of every definition that refers to content under
85 /// @a MountPoint (a content pack being unmounted). Slots are kept so UIds
86 /// stay valid; spawning such a definition fails until the next episode.
87 void ReleaseContentPack(const FString &MountPoint);
88
89 /// Bind the definitions of @a ActorFactory that are not bound yet.
90 ///
91 /// Definitions are normally bound once, when the episode registers its
92 /// factories. A content pack mounted later brings new catalog entries, and
93 /// its blueprints must become available without waiting for a world load.
94 void BindNewDefinitions(ACarlaActorFactory &ActorFactory);
95
96 const TArray<FActorDefinition> &GetActorDefinitions() const
97 {
98 return Definitions;
99 }
100
102 {
103 return Registry;
104 }
105
107 {
108 return Registry;
109 }
110
111private:
112
113 UFUNCTION()
114 void OnActorDestroyed(AActor *Actor);
115
116 /// UPROPERTY so the garbage collector clears the class references when a
117 /// content pack's classes are destroyed; a plain array kept dangling
118 /// TSubclassOf pointers that ReleaseContentPack then dereferenced.
119 UPROPERTY()
121
123
124 UPROPERTY()
125 TMap<FString, TSubclassOf<AActor>> Classes;
126
128
129};
Base class for Carla actor factories.
A registry of all the Carla actors.
FCarlaActor::IdType IdType
A view over an actor and its properties.
Definition CarlaActor.h:25
uint32 IdType
Definition CarlaActor.h:28
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Object in charge of binding ActorDefinitions to spawn functions, as well as keeping the registry of a...
void BindNewDefinitions(ACarlaActorFactory &ActorFactory)
Bind the definitions of ActorFactory that are not bound yet.
const FActorRegistry & GetActorRegistry() const
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)
const TArray< FActorDefinition > & GetActorDefinitions() const
TMap< FString, TSubclassOf< AActor > > Classes
FActorRegistry Registry
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.
FActorRegistry & GetActorRegistry()
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
UPROPERTY so the garbage collector clears the class references when a content pack's classes are dest...
TArray< SpawnFunctionType > SpawnFunctions
void ReleaseContentPack(const FString &MountPoint)
Forget the classes of every definition that refers to content under MountPoint (a content pack being ...
A simulation episode.
A definition of a Carla Actor with all the variation and attributes.
A description of a Carla Actor with all its variation.
FActorSpawnResult()=default