Difficulties with SweepMultiByChannel and storing pointers to hit actors.

TheoRat3
Thu, 08 Feb 2024 21:20:15 +0000
C++

I’m working on creating an ability that applies a status effect to enemies in the form of attaching actors of a class I made “WeakPoint” to their meshes. I ran into a wall today when trying to apply this ability to an indefinite number of targets. I’m attempting to take all of the actors hit in the OutHIts array, itterate through it and store pointers to the actors that are derived from my Enemy class into its own array, that I then use to add the weakpoints to their respective meshes. I have two enemies currently in the world, and after performing the code below if I UE_LOG the size of my array for enemies hit by the sweep the number shown is 30… If I UE_LOG the size of OutHIts the number shown is 68. So clearly my for loop is doing some kind of filtering, but it’s storing duplicate addresses to the same enemy, causing an excess of weak points to be spawned. Below is my code for my sweep and how I’m currently iterating through actors as well as my Struct I’m using to store the weakpoints to the enemies for context.

FCollisionShape HitSphere = FCollisionShape::MakeSphere(TacticalRange);

bool bHitSuccess = GetWorld()->SweepMultiByChannel(
OutHits,
PlayerCharacter->GetActorLocation(),
PlayerCharacter->GetActorLocation(),
FQuat::Identity,
ECC_GameTraceChannel1,
HitSphere);

  if(bHitSuccess)
  {
       for(int32 i = 0; i < OutHits.Num(); i++)
       {

            if (OutHits[i].GetActor()->IsA(AEnemy::StaticClass()))
            {
                FEnemyData EnemyHit = {Cast<AEnemy>(OutHits[i].GetActor())};
                EnemiesHitByTactical.Add(EnemyHit);

            }
        }

       UE_LOG(LogTemp, Warning, TEXT("number of enemies hit %i"),
       EnemiesHitByTactical.Num());
       UE_LOG(LogTemp, Warning, TEXT("number of actors hit %i"),
       OutHits.Num());
}

USTRUCT(BlueprintType)
struct FEnemyData
{
    GENERATED_BODY()

    class AEnemy* EnemyPtr;
    TArray<AWeakPoint*> WeakPointsArr;

};

13 posts - 3 participants