Home > OS >  Raycast check crashing UE5
Raycast check crashing UE5

Time:08-26

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;
        }
    }
}

CodePudding user response:

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  )
  • Related