I thank you for your advise to go and look at C++ more in-depth but I just can’t keep going back to zero all the time. I need to see that I can actually use C++ at all and I feel like I am making baby steps here. I won’t make perfect C++ code from the get-go just like how I didn’t learn how to make perfect Java or C# code. It’s okay to fail, as long as I learn from it
Now my code is a bit different, but still not working 100 %.
I can now get into the game but no WASD or Mouse Movement registers. I assume this is because the Basic Movement Input variable is not checked? If that’s the case, would I have to write my own code to make basic movement?
#include "BuildingEscape.h"
#include "BuildingEscapePawn.h"
#include "UseComponent.h"
// Sets default values
ABuildingEscapePawn::ABuildingEscapePawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ABuildingEscapePawn::BeginPlay()
{
Super::BeginPlay();
FindPhysicsHandleComponent();
}
// Called every frame
void ABuildingEscapePawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ABuildingEscapePawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
if (InputComponent)
{
InputComponent->BindAction("Use", EInputEvent::IE_Pressed, this, &ABuildingEscapePawn::Use);
InputComponent->BindAction("Grab", EInputEvent::IE_Pressed, this, &ABuildingEscapePawn::Grab);
InputComponent->BindAction("Grab", EInputEvent::IE_Released, this, &ABuildingEscapePawn::ReleaseGrab);
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s missing input component"), *(GetController()->GetPawn()->GetName()));
}
}
void ABuildingEscapePawn::Use()
{
if (PhysicsHandle)
{
if (!PhysicsHandle->GrabbedComponent)
{
FHitResult HitResult = GetFirstUseHandleInReach();
AActor* ActorHit = HitResult.GetActor();
if (ActorHit)
{
UUseComponent* UseComp = ActorHit->FindComponentByClass<UUseComponent>();
if (UseComp)
{
UseComp->FireUseEvent();
}
}
}
}
}
void ABuildingEscapePawn::Grab()
{
/// Line trace and see if we reach any actors with physics body collision channel set
FHitResult HitResult = GetFirstPhysicsBodyInReach();
UPrimitiveComponent* ComponentToGrab = HitResult.GetComponent();
AActor* ActorHit = HitResult.GetActor();
/// If we hit something, then attach a physics handle
if (ActorHit && PhysicsHandle)
{
PhysicsHandle->GrabComponent(ComponentToGrab, EName::NAME_None, ComponentToGrab->GetOwner()->GetActorLocation(), true); // true is to allow rotation
}
}
void ABuildingEscapePawn::ReleaseGrab()
{
PhysicsHandle->ReleaseComponent();
}
void ABuildingEscapePawn::FindPhysicsHandleComponent()
{
PhysicsHandle = GetController()->GetPawn()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *(GetController()->GetPawn()->GetName()));
}
}
const FHitResult ABuildingEscapePawn::GetFirstUseHandleInReach()
{
/// Line-trace (AKA ray-cast) out to reach
FHitResult HitResult;
// Setup Query parameters
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetController()->GetOwner());
GetWorld()->LineTraceSingleByChannel(HitResult, GetReachLineStart(), GetReachLineEnd(), ECollisionChannel::ECC_Visibility, TraceParameters);
return HitResult;
}
const FHitResult ABuildingEscapePawn::GetFirstPhysicsBodyInReach()
{
/// Line-trace (AKA ray-cast) out to reach
FHitResult HitResult;
// Setup query parameters
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetController()->GetOwner());
GetWorld()->LineTraceSingleByObjectType(OUT HitResult, GetReachLineStart(), GetReachLineEnd(), FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody), TraceParameters);
return HitResult;
}
FVector ABuildingEscapePawn::GetReachLineStart()
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
return PlayerViewPointLocation;
}
FVector ABuildingEscapePawn::GetReachLineEnd()
{
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewPointLocation, OUT PlayerViewPointRotation);
return PlayerViewPointLocation + (PlayerViewPointRotation.Vector() * Reach);
}