CARLA
ActorDispatcher.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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 "Containers/Array.h"
15 #include "Templates/Function.h"
16 
17 #include "ActorDispatcher.generated.h"
18 
19 class ACarlaActorFactory;
20 
21 /// Object in charge of binding ActorDefinitions to spawn functions, as well as
22 /// keeping the registry of all the actors spawned.
23 UCLASS()
24 class CARLA_API UActorDispatcher : public UObject
25 {
26  GENERATED_BODY()
27 
28 public:
29 
30  using SpawnFunctionType = TFunction<FActorSpawnResult(const FTransform &, const FActorDescription &)>;
31 
32  /// Bind a definition to a spawn function. When SpawnActor is called with a
33  /// matching description @a Functor is called.
34  ///
35  /// @warning Invalid definitions are ignored.
36  void Bind(FActorDefinition Definition, SpawnFunctionType SpawnFunction);
37 
38  /// Bind all the definitions of @a ActorFactory to its spawn function.
39  ///
40  /// @warning Invalid definitions are ignored.
41  void Bind(ACarlaActorFactory &ActorFactory);
42 
43  /// Spawns an actor based on @a ActorDescription at @a Transform. To properly
44  /// despawn an actor created with this function call DestroyActor.
45  ///
46  /// @return A pair containing the result of the spawn function and a view over
47  /// the actor and its properties. If the status is different of Success the
48  /// view is invalid.
49  TPair<EActorSpawnResultStatus, FCarlaActor*> SpawnActor(
50  const FTransform &Transform,
51  FActorDescription ActorDescription,
52  FCarlaActor::IdType DesiredId = 0);
53 
54  /// ReSpawns an actor based on @a ActorDescription at @a Transform. To properly
55  /// despawn an actor created with this function call DestroyActor.
56  /// Used to respawn dormant actors.
57  ///
58  /// @return The actor to be respawned
59  AActor* ReSpawnActor(
60  const FTransform &Transform,
61  FActorDescription ActorDescription);
62 
63  void PutActorToSleep(FCarlaActor::IdType Id, UCarlaEpisode* CarlaEpisode);
64 
65  void WakeActorUp(FCarlaActor::IdType Id, UCarlaEpisode* CarlaEpisode);
66 
67  /// Destroys an actor, properly removing it from the registry.
68  ///
69  /// Return true if the @a Actor is destroyed or already marked for
70  /// destruction, false if indestructible or nullptr.
71  //bool DestroyActor(AActor *Actor);
72 
73  bool DestroyActor(FCarlaActor::IdType ActorId);
74 
75  /// Register an actor that was not created using "SpawnActor" function but
76  /// that should be kept in the registry.
77  FCarlaActor* RegisterActor(
78  AActor &Actor,
79  FActorDescription ActorDescription,
80  FActorRegistry::IdType DesiredId = 0);
81 
82  const TArray<FActorDefinition> &GetActorDefinitions() const
83  {
84  return Definitions;
85  }
86 
88  {
89  return Registry;
90  }
91 
93  {
94  return Registry;
95  }
96 
97 private:
98 
99  UFUNCTION()
100  void OnActorDestroyed(AActor *Actor);
101 
102  TArray<FActorDefinition> Definitions;
103 
104  TArray<SpawnFunctionType> SpawnFunctions;
105 
106  TArray<TSubclassOf<AActor>> Classes;
107 
109 
110 };
Base class for Carla actor factories.
TArray< TSubclassOf< AActor > > Classes
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
const FActorRegistry & GetActorRegistry() const
Object in charge of binding ActorDefinitions to spawn functions, as well as keeping the registry of a...
uint32 IdType
Definition: CarlaActor.h:27
TFunction< FActorSpawnResult(const FTransform &, const FActorDescription &)> SpawnFunctionType
FActorRegistry & GetActorRegistry()
A definition of a Carla Actor with all the variation and attributes.
TArray< FActorDefinition > Definitions
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:38
FActorRegistry Registry
A description of a Carla Actor with all its variation.
uint32_t ActorId
Definition: ActorId.h:14
virtual FActorSpawnResult SpawnActor(const FTransform &SpawnAtTransform, const FActorDescription &ActorDescription)
Spawn an actor based on ActorDescription and Transform.
FCarlaActor::IdType IdType
Definition: ActorRegistry.h:24
TArray< SpawnFunctionType > SpawnFunctions
geom::Transform Transform
Definition: rpc/Transform.h:16
A view over an actor and its properties.
Definition: CarlaActor.h:23
const TArray< FActorDefinition > & GetActorDefinitions() const