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

#include <ActorInformation.h>

Inheritance diagram for IActorInformation:
Inheritance graph
[legend]

Public Member Functions

FString GetActorID () const
 
FString GetActorName () const
 
FString GetActorInformation () const
 
void SetActorName (const FString &NewActorName)
 
void SetActorIDAndName (const FString &NewActorName, const FString &NewID)
 

Static Public Member Functions

static void SetAndValidateActorIDAndName (FString &ActorName, FString &ActorID, TWeakObjectPtr< AActor > Actor)
 
static bool DestroyActorByID (const FString &ID)
 
static AActor * GetActorByID (const FString &ID)
 
template<typename T >
static TArray< T * > GetActorsWithInterface ()
 
static void PrintAllIds ()
 

Static Private Member Functions

static FString ValidateID (const FString &ActorID, TWeakObjectPtr< AActor > Actor)
 
static FString GenerateUniqueID ()
 

Detailed Description

Interface for actors with an ID.

Definition at line 22 of file ActorInformation.h.

Member Function Documentation

◆ DestroyActorByID()

bool IActorInformation::DestroyActorByID ( const FString &  ID)
static

Try to Destroy Actor by its ID.

Parameters
IDActor ID
Returns
boolean whether destroying was successfull

Definition at line 96 of file ActorInformation.cpp.

97{
98 if (!GEngine || !GEngine->GameViewport)
99 {
100 return false;
101 }
102
103 TArray<AActor*> ActorsWithInterface;
104 UWorld* World = GEngine->GameViewport->GetWorld();
105 UGameplayStatics::GetAllActorsWithInterface(World, UActorInformation::StaticClass(), ActorsWithInterface);
106
107 // All ID's are in lowercase.
108 FString IDLowered = ID.ToLower();
109
110 for (AActor* Actor : ActorsWithInterface)
111 {
112 FString ActorID = IActorInformation::Execute_GetActorID(Actor);
113 if (ActorID == IDLowered)
114 {
115 return Actor->Destroy();
116 }
117 }
118
119 return false;
120}

Referenced by UROSCommands::HandleTryDestroyObjectByID().

◆ GenerateUniqueID()

FString IActorInformation::GenerateUniqueID ( )
staticprivate

Generate unique ID.

Definition at line 62 of file ActorInformation.cpp.

63{
64 return FGuid::NewGuid().ToString(EGuidFormats::DigitsWithHyphensLower);
65}

Referenced by SetAndValidateActorIDAndName().

◆ GetActorByID()

AActor * IActorInformation::GetActorByID ( const FString &  ID)
static

Try to find Actor by ID

Parameters
IDActor ID
Returns
Actor pointer or nullptr

Definition at line 122 of file ActorInformation.cpp.

123{
124 if (!GEngine || !GEngine->GameViewport)
125 {
126 return nullptr;
127 }
128
129 TArray<AActor*> ActorsWithInterface;
130 UWorld* World = GEngine->GameViewport->GetWorld();
131 UGameplayStatics::GetAllActorsWithInterface(World, UActorInformation::StaticClass(), ActorsWithInterface);
132
133 // All ID's are in lowercase.
134 FString IDLowered = ID.ToLower();
135
136 for (AActor* Actor : ActorsWithInterface)
137 {
138 if (Actor)
139 {
140 FString ActorID = IActorInformation::Execute_GetActorID(Actor);
141 if (ActorID == IDLowered)
142 {
143 return Actor;
144 }
145 }
146 }
147
148 return nullptr;
149}

Referenced by UROSCommands::HandleChangeOverlapSensorBounds(), UROSCommands::HandleChangeOverlapSensorPosition(), UROSCommands::HandleMoveDroneTo(), UROSCommands::HandleMoveWalkerTo(), UROSCommands::HandleSetSensorEnabled(), UROSCommands::HandleSetWalkerMovement(), and UROSCommands::HandleTeleportActorByID().

◆ GetActorID()

FString IActorInformation::GetActorID ( ) const

Get Actor ID. This ID can be used to destroy or otherwise control actor through ROS.

◆ GetActorInformation()

FString IActorInformation::GetActorInformation ( ) const

Get Actor information.

◆ GetActorName()

FString IActorInformation::GetActorName ( ) const

Get Actor name. This is for UI related things.

◆ GetActorsWithInterface()

template<typename T >
TArray< T * > IActorInformation::GetActorsWithInterface
static

Retrieves an array of actors with IActorID interface.

Template Parameters
TThe interface class type.
Returns
An array of actors with IActorID interface.

Definition at line 152 of file ActorInformation.cpp.

153{
154 if (!GEngine || !GEngine->GameViewport)
155 {
156 return TArray<T*>();
157 }
158
159 TArray<AActor*> ActorsWithInterface;
160 UWorld* World = GEngine->GameViewport->GetWorld();
161 UGameplayStatics::GetAllActorsWithInterface(World, UActorInformation::StaticClass(), ActorsWithInterface);
162
163 // Convert the TArray<AActor*> to TArray<T*>
164 TArray<T*> ConvertedActors;
165 for (AActor* Actor : ActorsWithInterface)
166 {
167 T* TypedActor = Cast<T>(Actor);
168 if (TypedActor)
169 {
170 ConvertedActors.Add(TypedActor);
171 }
172 }
173
174 return ConvertedActors;
175}

◆ PrintAllIds()

void IActorInformation::PrintAllIds ( )
static

Print all ID's.

Definition at line 177 of file ActorInformation.cpp.

178{
179 FString Message = "IDs: ";
180 int32 NumIds = ExistingIDs.Num();
181
182 // Iterate through the ExistingIDs map
183 int32 IdCounter = 0;
184 for (auto It = ExistingIDs.CreateIterator(); It; ++It)
185 {
186 if (It.Value().Actor.IsValid())
187 {
188 // Append the ID to the Message string
189 Message += FString::Printf(TEXT("%s"), *It.Key());
190 if (++IdCounter < NumIds)
191 {
192 // Add a comma if it's not the last value
193 Message += TEXT(", ");
194 }
195 }
196 else
197 {
198 // Remove entries with nullptr actors
199 It.RemoveCurrent();
200 }
201 }
202
203 SimulatorLog::Log(Message);
204}
static TMap< FString, IdentifierData > ExistingIDs
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)

References ExistingIDs, and SimulatorLog::Log().

Referenced by UROSCommands::HandlePrintIds().

◆ SetActorIDAndName()

void IActorInformation::SetActorIDAndName ( const FString &  NewActorName,
const FString &  NewID 
)

Set Actor ID and name.

◆ SetActorName()

void IActorInformation::SetActorName ( const FString &  NewActorName)

Set Actor name. This is for UI related things.

◆ SetAndValidateActorIDAndName()

void IActorInformation::SetAndValidateActorIDAndName ( FString &  ActorName,
FString &  ActorID,
TWeakObjectPtr< AActor >  Actor 
)
static

Sets and validates the Actor's name and ID. This function ensures the Actor's name is non-empty and generates a unique ID if needed.

Parameters
ActorName[in, out] Reference to the Actor's name. Set to the label if empty.
ActorID[in, out] Reference to the Actor's ID. Validated and set to a unique ID if empty.
Actor[in] Pointer to the Actor. Must not be nullptr.

Definition at line 67 of file ActorInformation.cpp.

68{
69 if (!Actor.IsValid())
70 {
71#if WITH_EDITOR
72 UE_LOG(LogTemp, Warning, TEXT("IActorID.cpp: Invalid Actor!"));
73#endif
74 return;
75 }
76
77 if (ActorName.IsEmpty())
78 {
79 // If the given Actor name is empty, use Actor actual name or label as name
80 // this name is used for Unreal UI side only.
81 ActorName = Actor->GetActorNameOrLabel();
82 }
83
84 if (!ActorID.IsEmpty())
85 {
86 // Validate the ID, If same ID exists, ValidateID adds available number at the end: Actor1..Actor2..ActorN
87 ActorID = ValidateID(ActorID, Actor);
88 }
89 else
90 {
91 // If given Actor ID is empty, generate unique ID (long string)
92 ActorID = GenerateUniqueID();
93 }
94}
static FString GenerateUniqueID()
static FString ValidateID(const FString &ActorID, TWeakObjectPtr< AActor > Actor)

References GenerateUniqueID(), and ValidateID().

Referenced by AVehicle::SetActorIDAndName_Implementation().

◆ ValidateID()

FString IActorInformation::ValidateID ( const FString &  ActorID,
TWeakObjectPtr< AActor >  Actor 
)
staticprivate

Validate that the ID is unique.

Definition at line 28 of file ActorInformation.cpp.

29{
30 FString ActorIDLower = ActorID.ToLower();
31 FString FinalID = ActorIDLower;
32
33 // Check if the ID already exists
34 if (ExistingIDs.Contains(FinalID))
35 {
36 IdentifierData& Data = ExistingIDs[FinalID];
37
38 if (!Data.Actor.IsValid())
39 {
40 // Reuse the ID if the existing Actor no longer exists.
41 Data.Actor = Actor;
42 return FinalID;
43 }
44
45 // Append the count as a numerical suffix
46 FinalID = ActorIDLower + FString::Printf(TEXT("%d"), Data.Count);
47
48 while (ExistingIDs.Contains(FinalID))
49 {
50 // If the new ID exists, keep incrementing the count
51 Data.Count++;
52 FinalID = ActorIDLower + FString::Printf(TEXT("%d"), Data.Count);
53 }
54 }
55
56 // Add the final ID to the map
57 ExistingIDs.Add(FinalID, IdentifierData{ FinalID, Actor });
58
59 return FinalID;
60}
TWeakObjectPtr< AActor > Actor

References IdentifierData::Actor, IdentifierData::Count, and ExistingIDs.

Referenced by SetAndValidateActorIDAndName().


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