Crash when implementing PhysicsHandle

Hi!

I am having an app crash when implementing the both “if” loops in Grab and TickComponent sections. Visual studio or Unreal compile do not show any errors, and Unreal crashes when play is pressed, showing error due to Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000000b8

If I comment out those loops, it works (without grabbing funcion obviously). I have looked for similar topics and reviewing the lecture to search for any misspelled error for my part, with no luck yet.

(the loops, as shown by Michael: for Grab;

if (HitResult.GetActor())

PhysicsHandle->GrabComponentAtLocation
	(
	ComponentToGrab,
	NAME_None, 
	LineTraceEnd
	);

and for TickComp;

if (PhysicsHandle->GrabbedComponent)
{
	PhysicsHandle->SetTargetLocation(LineTraceEnd);
}

(I am using 4.25 version)

Any help would be greatly apreciated.

Thanks

Could you show your entire code?

Sure, here it is:

#include "DrawDebugHelpers.h"
#include "Engine/World.h"
#include "Engine/EngineTypes.h"
#include "GameFramework/PlayerController.h"
#include "CollisionQueryParams.h"
#include "Grabber.h"

#define OUT //help to define out parameters: does nothing, only for description

UGrabber::UGrabber()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;
}


void UGrabber::BeginPlay()
{
	Super::BeginPlay();

	FindPhysicsHandle();

	SetupInputComponent();
}	

void UGrabber::SetupInputComponent()
{	
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
	if (InputComponent)
	{
		InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
		InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
	}
}	

void UGrabber::FindPhysicsHandle()

{
	PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if (PhysicsHandle)
	{
		//physics handle is found
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("No physics handle component found on %s!"), *GetOwner()->GetName());
	}
}

void UGrabber::Grab()
{
	//check for grab key
	UE_LOG(LogTemp, Warning, TEXT("test key - grab pressed"));

	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;

	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT PlayerViewPointLocation,
		OUT PlayerViewPointRotation
	);

	FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;

	FHitResult HitResult = GetFirstPhysicsBodyInReach();
	
	UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent();

	//if we hit something then attach the physics handle

	if (HitResult.GetActor())

	{
		PhysicsHandle->GrabComponentAtLocation
		(
		ComponentToGrab,
		NAME_None, 
		LineTraceEnd
		);
	}

}

void UGrabber::Release()
{
	//check for grab key
	UE_LOG(LogTemp, Warning, TEXT("test key - grab released"));

	//to do remove/release physics handle
}


void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	//if the physic handle is attach -> move object attached

	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;

	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT PlayerViewPointLocation,
		OUT PlayerViewPointRotation
	);

	FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;

	if (PhysicsHandle->GrabbedComponent)
	{
		PhysicsHandle->SetTargetLocation(LineTraceEnd);
	}

}

FHitResult UGrabber::GetFirstPhysicsBodyInReach() const
{
	// Get players viewpoint
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;

	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT PlayerViewPointLocation,
		OUT PlayerViewPointRotation
	);

	FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach; //temporary end point
	//draw a line from player showing the reach
	DrawDebugLine(
		GetWorld(),
		PlayerViewPointLocation,
		LineTraceEnd,
		FColor(0, 255, 0),
		false,
		0.f,
		0,
		5.f
	);

	// Ray-cast out to a certain distance (Reach)
	FHitResult Hit;
	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

	GetWorld()->LineTraceSingleByObjectType(
		OUT Hit,
		PlayerViewPointLocation,
		LineTraceEnd,
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
		TraceParams
	);

	// See what it hits

	AActor* ActorHit = Hit.GetActor();

	if (ActorHit)
	{
		UE_LOG(LogTemp, Warning, TEXT("Line trace has hit: %s"), *(ActorHit->GetName()));
	}

	return Hit;
}

And when you comment out the code do you see anything in your output log?

Finally found the problem! I had not added PhysicsHandle component to the pawn blueprint :man_facepalming:

Found out thanks to your indication to look the log, thanks!

No clue of how to determine that from unreal crash log, though

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms