-1

I am trying to check what a multi line trace by channel is hitting by printing every hit object's name to the log, however the engine keeps crashing due to (I assume) a memory error. I've tried only printing the first object, which works, but since this a multi line trace I would like to check every object that is being hit

Here is my code:

TArray<FHitResult> hits = {};

ECollisionChannel channel(ECC_GameTraceChannel1);
FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

GetWorld()->LineTraceMultiByChannel(
    OUT hits,
    camWorldLocation,
    end,
    channel,
    TraceParams
);

DrawDebugLine(GetWorld(), camWorldLocation, end, FColor::Green, false, 2.0f);

if (!hits.IsEmpty())
{
    for (int i = 0; i < sizeof(hits); i++)
    {
        if (&hits[i] != nullptr) {
            if (hits[i].GetActor() != nullptr)
            {
                UE_LOG(LogTemp, Error, TEXT("Line trace has hit: %s"), *(hits[i].GetActor()->GetName()));
            }
        }
        else 
        {
            break;
        }
    }
}
zyrg
  • 3
  • 1
  • 6
    `sizeof(hits)` gives you the size of the C++ object in bytes, not the number of items in the container – Jeffrey Aug 25 '22 at 12:49

1 Answers1

0

sizeof(hits) gives you the size of the C++ object in bytes, not the number of items in the container.

You need to use

for (int i = 0; i < hits.Num(); i++)
Jeffrey
  • 11,063
  • 1
  • 21
  • 42