Hi, I have attempted to factor out the function returning the ‘Actor’ in view. Here’s what I have done:
/ / Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
APlayerController* thisController = GetWorld()->GetFirstPlayerController();
FVector ViewPointLocation; // <--- create the placeholders for the location and the rotation of the viewpoint
FRotator ViewPointRotation;
// set the location and the rotation with the reference getter
thisController->GetPlayerViewPoint( OUT ViewPointLocation, OUT ViewPointRotation);
// UE_LOG(LogTemp, Warning, TEXT("The location is %s and the rotation is %s"), *(ViewPointLocation.ToString()), *(ViewPointRotation.Vector().ToString()));
// draw the debug line
// get the end of the line
FVector debLineEnd = ViewPointLocation + ViewPointRotation.Vector()*flReach;
DrawDebugLine(
GetWorld(),
ViewPointLocation,
debLineEnd,
FColor(255, 0, 0),
false,
0.f,
0.f,
flThickness
);
// draw the tracing lines and return the colliding object
// log the name of the actor under collision
AActor * actorUnderCollision = ReturnActorInView();
UE_LOG(LogTemp, Warning, TEXT("%s"), *(actorUnderCollision->GetName()))
}
// returns an actor that is in the point of view of the character, taking the reach (private variable) into account
AActor * UGrabber::ReturnActorInView() {
FVector ViewPointPosition; // placeholders for position and rotation
FRotator ViewPointRotation;
FVector EndOfView;
UWorld * thisWorld = GetWorld();
FHitResult Hit;
thisWorld->GetFirstPlayerController()->GetPlayerViewPoint(ViewPointPosition, ViewPointRotation);
EndOfView = ViewPointPosition + ViewPointRotation.Vector()*flReach;
thisWorld->LineTraceSingleByObjectType(
Hit,
ViewPointPosition,
EndOfView,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
FCollisionQueryParams(FName(TEXT("")), false, GetOwner())
);
// return the actor that was hit
return Hit.GetActor();
}
as you can see, the tick component calls the function ‘returnActorInView’ each frame and then writes the name of the actor in view to the log. then it turns out that I write two times into log: one time the coordinates of the ViewPoint and one time the name of the actor in view. When I first compiled this code, it used to work but didn’t write to log the name of the actor in view. Then, after I commented out the first ‘writing’ to Log, the whole thing crashed terribly and it seems that I cannot recovert it from my version control. Can you tell what went wrong with the code?
Thank you a lot.