Question related to my Building Escape code after Reducing 'Hot Loops'

My question is that, as you can see below, my code after reducing hot loops code is a bit different from the lecture’s code. So, I am a little confused as I created many different functions in my code than the lecture’s code, which code will be faster, mine or yours. And if my code will be slower then is it just because of more functions in my code or something else is also the reason.

I am really curious to know the answer :upside_down_face:

Header File:-

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

#pragma once

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


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ESCAPEBUILDING2_API UGrabber : public UActorComponent
{
	GENERATED_BODY()

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

	// 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 Reach = 100.f;
	UPhysicsHandleComponent* PhysicsHandle = nullptr;
	UInputComponent* InputKeyBind = nullptr;
	FHitResult Hit; //Hit Result

	//User Defined Functions
	void GrabObject();
	void ReleaseObject();
	void FindPhysicsHandle();
	void SetupInputComponent();
	void DrawDebugLineFunction(FVector PlayerViewPointLocation, FVector TraceLineEnd);
	void ViewPointUpdater(FVector &PlayerViewPointLocation, FVector &TraceLineEnd, FRotator &PlayerViewPointRotation);
	void CollisionWithObjects(FVector &PlayerViewPointLocation, FVector &TraceLineEnd);
	void CollidedObjectNameLog();
};

CPP file:-

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

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

#define OUT

// Sets default values for this component's properties
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;
}

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

	FindPhysicsHandle();
	SetupInputComponent();
}

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

	//Varaibles for trace line
	FVector PlayerViewPointLocation;
	FRotator PlayerViewPointRotation;
	FVector TraceLineEnd;
	
	// Function for updating trace line's variables
	ViewPointUpdater(PlayerViewPointLocation, TraceLineEnd, PlayerViewPointRotation);

	// Function For Debbug Line
	DrawDebugLineFunction(PlayerViewPointLocation, TraceLineEnd);

	//Check if the pawn collides with any actor?
	CollisionWithObjects(PlayerViewPointLocation, TraceLineEnd);
}

void UGrabber::GrabObject() // Function called for grabbing
{
	UE_LOG(LogTemp, Warning, TEXT("Grabbed!"))
	CollidedObjectNameLog();
}

void UGrabber::ReleaseObject() // Function called for releasing grabbed object
{
	UE_LOG(LogTemp, Warning, TEXT("Released!"))
}

void UGrabber::FindPhysicsHandle() // Physics Handle Check
{
	PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();

	if (PhysicsHandle)
	{
		// Physics Handle Found
	}

	else
	{
		UE_LOG(LogTemp, Error, TEXT("%s does not have PhysicsHandle set to it."), *GetOwner()->GetName()) // Physics Handle Not Found
	}
}

void UGrabber::SetupInputComponent() // Input Bind Check & Input Bind Functions Calling

{
	InputKeyBind = GetOwner()->FindComponentByClass<UInputComponent>();

	if (InputKeyBind)
	{
		// Input Bind Found
		InputKeyBind->BindAction("Grab", IE_Pressed, this, &UGrabber::GrabObject);	   // Function For Grabbing
		InputKeyBind->BindAction("Grab", IE_Released, this, &UGrabber::ReleaseObject); // Function For Releasing Grabbed Object
	}
}

void UGrabber::ViewPointUpdater(FVector &PlayerViewPointLocation, FVector &TraceLineEnd, FRotator &PlayerViewPointRotation)  //Updates Parameters as refrence
{
	GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);  //Updates giver OUT parameters
	TraceLineEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;  //Updates TraceLineEnd
}

void UGrabber::DrawDebugLineFunction(FVector PlayerViewPointLocation, FVector TraceLineEnd)  //Function which draws debug line
{
	DrawDebugLine(  //Draws Debug Line
		GetWorld(),
		PlayerViewPointLocation,
		TraceLineEnd,
		FColor(0, 0, 255),
		false,
		0.f,
		0,
		5);
}

void UGrabber::CollisionWithObjects(FVector &PlayerViewPointLocation, FVector &TraceLineEnd) //Trace a ray against the world using object types and return the first blocking hit
{
	FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());  //Additional parameters used for the trace

	//Trace a ray against the world using object types and return the first blocking hit
	GetWorld()->LineTraceSingleByObjectType(  //Returns: TRUE if any hit is found
		OUT Hit,
		PlayerViewPointLocation,
		TraceLineEnd,
		FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
		TraceParams);
}

void UGrabber::CollidedObjectNameLog()  //Log out the name of actor colliding with pawn while pressing E
{
	AActor *CollidingActor = Hit.GetActor(); //Actor Which got hit by pawn

	if (CollidingActor) //If pawn gets hit by any actor
	{
		UE_LOG(LogTemp, Warning, TEXT("You Collided With %s."), *CollidingActor->GetName());
	}
}

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

Privacy & Terms