Not compiling due to issues with GetReachLineStart / End

Hi Guys

I’ve followed along with Ben and refactored, but the code doesn’t compile because it doesnt like the calls to GetReachLineStart() and GetReachLineEnd() in the LineTraceSingleByObjectType() method in GetFirstPhysicsBodyInReach()

Here are the relevant methods my code:

grabber.cpp

FHitResult UGrabber::GetFirstPhysicsBodyInReach() const
{

FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
FHitResult HitResult;

GetWorld()->LineTraceSingleByObjectType(
	OUT HitResult,
	GetReachLineStart(), // This is underlined in red
	GetReachLineEnd(), // This is underlined in red
	FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
	TraceParameters
);

return HitResult;
}

FVector UGrabber::GetReachLineStart()
{
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);

	return PlayerViewPointLocation;
}

FVector UGrabber::GetReachLineEnd()
{
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);

	return PlayerViewPointLocation + (PlayerViewPointRotation.Vector()*Reach);
}

And the header file

grabber.h

FHitResult GetFirstPhysicsBodyInReach() const;
FVector GetReachLineStart();
FVector GetReachLineEnd();

And the errors I’m getting are:

VS
the object has type qualifiers that are not compatible with the member function “UGrabber::GetReachLineStart”

Unreal
error C2662: ‘FVector UGrabber::GetReachLineStart(void)’: cannot convert ‘this’ pointer from ‘const UGrabber’ to ‘UGrabber &’

The references to those methods in GetWorld()->LineTraceSingleByObjectType seem to be the problem. I looked at Ben’s code in GitHub and it seems to be the same. Can anyone suggest what the problem miught be?

Cheers

Lucas

1 Like

You’ve marked GetFirstPhysicsBodyInReach as a const function but not GetReachLineStart or GetReachLineEnd. A const function cannot call a non-const member function as that would mean the initial const function has the potential to modify class data.

4 Likes

That’s fixed it! I made GetFirstPhysicsBodyInReach not a const function and now it works - thanks for your help!

I was actually suggesting to make all three const

2 Likes

OK lol I’ll do it that way, cheers

Privacy & Terms