Section: Building Escape Original. :Lecture: "Using Physics Handles"

I am having some errors with the fowling code:

The .h FIle:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "DrawDebugHelpers.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GameFramework/Controller.h"
#include "Public/CollisionQueryParams.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Components/InputComponent.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "DrawDebugHelpers.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "garbberComponet.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCSPEORGIN_API UgarbberComponet : public UActorComponent
{
	GENERATED_BODY()

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

	void Grab();
	void Release();
	void FindPhysicsHandleComponent();
	void SetUpEveryFrame();
	const FHitResult GetFirstPhysicsBodyInReach();

	UPROPERTY(EditAnywhere)
	float Reach = 100.0f;
	UPhysicsHandleComponent* PhysicsHandle = nullptr;
	UInputComponent* InputComponent = nullptr;

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

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

		
};

The .cpp file:

// Fill out your copyright notice in the Description page of Project Settings.

#include "garbberComponet.h"
#include "DrawDebugHelpers.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GameFramework/Controller.h"
#include "Public/CollisionQueryParams.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/World.h"

// Sets default values for this component's properties
UgarbberComponet::UgarbberComponet()
{
	// 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 UgarbberComponet::BeginPlay()
{
	Super::BeginPlay();

	// ...
}

void UgarbberComponet::Grab()
{
	auto HitResult = GetFirstPhysicsBodyInReach();
	auto ComponentToGrab = HitResult.GetComponent();
	auto ActorHit = HitResult.GetActor();

	if(ActorHit)
	{
PhysicsHandle -> GrabComponentAtLocation(ComponentToGrab, NAME_None, ComponentToGrab -> GetOwner() -> GetActorLocation());
	}
}
void UgarbberComponet::Release()
{

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

		FVector PlayerVeiwpointLocation;
	FRotator PlayerVeiwpointRotation;
		FCollisionQueryParams TracePerams = (FName(TEXT("")), false, GetOwner());
	const FVector LineTraceEnd = PlayerVeiwpointLocation + PlayerVeiwpointRotation.Vector() * Reach;
	FHitResult* Hit;
	GetWorld()->LineTraceSingleByObjectType(*Hit, PlayerVeiwpointLocation, LineTraceEnd, FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TracePerams);
	AActor *ActorHit = Hit->GetActor();
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();

	PhysicsHandle -> SetTargetLocation(LineTraceEnd);


	// ...
}
void UgarbberComponet::FindPhysicsHandleComponent()
{
		PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
}
	const FHitResult UgarbberComponet::GetFirstPhysicsBodyInReach()
	{
if (InputComponent)
	{
		InputComponent->BindAction("Grab", IE_Pressed, this, UgarbberComponet::Grab());
		InputComponent->BindAction("Grab", IE_Released, this, UgarbberComponet::Release());
	}
	else
	{
	}
		if(PhysicsHandle -> GrabbedComponent)
	{
			FVector PlayerVeiwpointLocation;
	FRotator PlayerVeiwpointRotation;
		FCollisionQueryParams TracePerams = (FName(TEXT("")), false, GetOwner());
	const FVector LineTraceEnd = PlayerVeiwpointLocation + PlayerVeiwpointRotation.Vector() * Reach;
	FHitResult* Hit;
	GetWorld()->LineTraceSingleByObjectType(*Hit, PlayerVeiwpointLocation, LineTraceEnd, FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TracePerams);
	AActor *ActorHit = Hit->GetActor();
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();

	PhysicsHandle -> SetTargetLocation(LineTraceEnd);
	return FHitResult();
	}

Thank You

I asked on Udemy for you to actually state what the problem here but formatting your post correctly I saw this

FHitResult* Hit;
GetWorld()->LineTraceSingleByObjectType(*Hit, PlayerVeiwpointLocation, LineTraceEnd, FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TracePerams);

Hit should not be a pointer, dereferencing it on the next line would most definitely be a crash since it’s not pointing to anything and also

return FHitResult();

Your returning a default initialised FHitResult object and not the variable which has the correct values (once the above issue is fixed)

Thank You, It seams to be working now

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

Privacy & Terms