C++ LineTraceMultiByObjectType

Hi. I have set up a blueprint that I am trying to replicate in C++. I can do the trace fine, but I’m struggling to get Hit information from the array as shown in the blueprint. Without it I cant use FHitResult.GetActor() or anything.

here is the code I have so far

TArray<FHitResult> HitResults;
	FHitResult HitData(ForceInit);
	FCollisionObjectQueryParams ObjectList;
	ObjectList.AddObjectTypesToQuery(ECC_Pawn);
	FCollisionQueryParams TraceParams = FCollisionQueryParams(FName(TEXT("TraceParams")), false, this);


//Do trace
		GetWorld()->LineTraceMultiByObjectType(HitResults, Start, End, ObjectList, TraceParams);
		    
               //For each loop
		for (int i = 0; i < HitResults.Num(); i++) 
		{
			
			if (HitData.GetActor()->IsA(AEnemy::StaticClass()))
			{

				AEnemy* enemy = Cast<AEnemy>(HitData.GetActor());

				if (!enemy) return;

				enemy->Damage(10);
            }
        }

Thanks in advance

Are you getting a Crash or something? If so Please tell what it says.

Try this instead the foor loop:

UE_LOG(LogTemp,Warning,TEXT("MyTrace Num Hits: %d"), HitResults.Num() ); // Will appear in your Logs just for debuging if hit anything at all
             for (FHitResult& HitData : HitResults) 
             {
                 
                 if (HitData.GetActor()) // pointer check
                 {
     
                     AEnemy* enemy = Cast<AEnemy>(HitData.GetActor()); // Type Check
     
                     if (!enemy) return; // Pointer Check
     
                     enemy->Damage(10);
                 }
             }

Aside from that make sure GetWorld() is valid everything else looks fine to me.

ah I actually just fixed this but thanks for the reply!

Basically, I had my array of FHitresults called “HitResults” and a FHitResult variable called HitData.

I just needed to add the following line so Hitdata was fed the information from HitResults on every iteration of the loop.

HitData = HitResults[i];

Caused my game to crash before, but this fixed it. Cant see it being a problem if I extend to multiple enemy types either.

Unless I’m missing something? Just means I will have to cast to every enemy type and declare its ->Damage() function.