Hi;
In run time , I spawn three (or more) Actors (as inventory like weapon) and I attach them to a socket of my pawn.
How could I iterate through the array of these attached actors , in c++ ?
Thanks.
MSD.
Hi;
In run time , I spawn three (or more) Actors (as inventory like weapon) and I attach them to a socket of my pawn.
How could I iterate through the array of these attached actors , in c++ ?
Thanks.
MSD.
hi,
I use the following snippet in OnConstruct(), but you can use any component class (I store them in arrays):
SkeletalMeshes.Empty();
StaticMeshes.Empty();
FlipBooks.Empty();
// get all components and their children components too of the selected classes
GetComponents<URTSSkeletalMeshComponent>(SkeletalMeshes);
GetComponents<URTSStaticMeshComponent>(StaticMeshes);
GetComponents<URTSFlipBookComponent>(FlipBooks);
I use the following without any hickups:
this->InputComponent = this->GetOwner()->FindComponentsByClass<UInputComponent>();
this line will return a component of type UInputComponent if it exists.
If you want all the components u can use: FindComponentsByClass<class>();
this will return an array of components found matching ur T Type
Hi;
I use these codes but it just shows me the main component of my pawn character , like arrow , camera and so on :
TArray< USceneComponent* > childActor;
childActor = RootComponent->GetAttachChildren();
UE_LOG(LogTemp, Warning, TEXT("components are : %s"), *childActor[0]->GetName());
I need array of actors that I attach at run-time …!
Hi MSD, like I said above. You can find all the components for a class using the following: FindComponentsByClass<class>(); where class is the type of components you are looking for. 99% of the time you are looking for a specific component as you need it to do something.
So for examle consider the following:
TArray<AWeapon> Weapons = this->GetOwner()->FindComponentsByClass<AWeapon>();
for (auto& Weapon: Weapons)
{
// do whatever u want here with Weapon
}
or something like that… i just quickly wrote this up but this is how u can itterate an array. If you are attaching the components or whatever using an array then why dont you just ittirate through the array used to attach them instead?
What about (Cannot try at the moment, so i’m not 100% sure)
TArray<AActor*> Actors;
GetAttachedActors(Actors);
for (AActor* Actor : Actors) {
UE_LOG(LogTemp, Log, TEXT("Hi, i'm %s"), *Actor->GetName());
}