Building Escape crashes after following Physics Handles lecture(Lecture no 125)

Hi, my unreal engine is crashing after following the lecture physics handle in the building escape game.

Here’s the crash error:

I think that the main cause of the problem is as follows:


Because whenever I comment out these lines marked in red rectangle my engine doesn’t crash.

Here’s “GrabbeClass.cpp” code file:

// Developer: Muneeb Muzammal(H00387067)

#include "GrabberClass.h"	
#include "DrawDebugHelpers.h" //Headerfile to draw a line from player
#include "Engine/World.h"
#include "GameFramework/PlayerController.h" //Headerfile to control player's view point


#define OUT
// Sets default values for this component's properties
UGrabberClass::UGrabberClass()
{
	// 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;
}


// Called when the game starts
void UGrabberClass::BeginPlay()
{
	Super::BeginPlay();

	FindPhysicsHandle();
	InputComponentSetup();

	
	
}

void UGrabberClass::InputComponentSetup() 
{
	inputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
	if (inputComponent)
	{
		inputComponent->BindAction("Grab", IE_Pressed, this, &UGrabberClass::Grab); //When press the grab key set out in engine this will refer to grab function
		inputComponent->BindAction("Grab", IE_Released, this, &UGrabberClass::Release);
	}
}

	//Check for physics handle component
void UGrabberClass::FindPhysicsHandle()
{
	physicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>(); //Because Getowner is common in physicsHandle and in grabber
	
	if (physicsHandle)
	{
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("No physics handle component find on %s!"), *GetOwner()->GetName());
	}
}
void UGrabberClass::Grab()
{
	UE_LOG(LogTemp, Warning, TEXT("Grabber pressed!"));

	
	FVector playerViewPointLocation;
	FRotator playerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT playerViewPointLocation,
		OUT playerViewPointRotation
	);

	FVector LineTraceEnd = playerViewPointLocation + playerViewPointRotation.Vector() * playerReach;

	FHitResult HitResult = GetFirstPhysicsBodyInReach();
	UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent(); //UPrimitive Component
	// Attach the physics handel if we hit something
	if (HitResult.GetActor())
	{
		//TODO attach physics handle
		physicsHandle->GrabComponentAtLocation
		(
			ComponentToGrab,
			NAME_None,
			LineTraceEnd
		);
	}
}

void UGrabberClass::Release()
{
	UE_LOG(LogTemp, Warning, TEXT("Grabber released!"));
	
	//TODO Release physics handle

}

// Called every frame
void UGrabberClass::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// Get Player's view point
	FVector playerViewPointLocation; 
	FRotator playerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT playerViewPointLocation,
		OUT playerViewPointRotation
	);

	FVector LineTraceEnd = playerViewPointLocation + playerViewPointRotation.Vector() * playerReach;

	//If physics handle is attached 
	if (physicsHandle->GrabbedComponent)
	{
		//Move abject we are holding
		physicsHandle->SetTargetLocation(LineTraceEnd);
		
	}
}

FHitResult UGrabberClass::GetFirstPhysicsBodyInReach() const
{

	// Get Player's view point
	FVector playerViewPointLocation;
	FRotator playerViewPointRotation;
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
		OUT playerViewPointLocation,
		OUT playerViewPointRotation
	);

	FVector LineTraceEnd = playerViewPointLocation + playerViewPointRotation.Vector() * playerReach;

	FHitResult Hit;
	// Ray-cast out to a certain distance (Reach)

	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

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

	AActor* actorHit = Hit.GetActor();

	if (actorHit)
	{
		UE_LOG(LogTemp, Warning, TEXT("LineTrace is hit: %s"), *(actorHit->GetName()))
	}

	return Hit;
}


And here’s “GrabberClass.h” code file:

// Developer: Muneeb Muzammal(H00387067)

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "GrabberClass.generated.h" 


UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class ESCAPE_API UGrabberClass : public UActorComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UGrabberClass();


public:
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;


protected:
	// Called when the game starts
	virtual void BeginPlay() override;


private:

	float playerReach = 100.0f;

	UPhysicsHandleComponent* physicsHandle = nullptr;
	UInputComponent* inputComponent = nullptr;

	void Grab();
	void Release();
	void FindPhysicsHandle();
	void InputComponentSetup();

	//Return the first actor within reach with physics body
	FHitResult GetFirstPhysicsBodyInReach() const;

};

Thank you.

add a check for physics handle as well , make your if check look like this :

if ( physicsHandle && physicsHandle->GrabbedComponent)

It’s best practice to always check EVERY pointer to make sure they are valid before using them.

cheers!
Tele

1 Like

Thank you very much, It worked. Stay Blessed!

1 Like

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

Privacy & Terms