Greetings,
Something (part of my code?) is suddenly causing a crash in Unreal. I’ve made sure the pawn_BP has its PhysicsHandle component. The code compiles OK in Unreal with no visible errors (builds OK in VS as well with no errors). Now, if I press Play the game launches, and I can eject and fly around if I want to - but as soon as I possess again and try to move or look around, Unreal crashes immediately. I have had zero crashes up until this point.
VS 15.95
Unreal 4.19.2
Win10
Grabber.cpp:
// Copywrite Steve Murray 2019
#include "Grabber.h"
#include "Engine/World.h"
#include "GameFramework/Actor.h"
#include "DrawDebugHelpers.h"
#include "Components/PrimitiveComponent.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();
FindPhysicsHandleComponent();
SetupInputComponent();
}
/// Look for attached Physics Handle
void UGrabber::FindPhysicsHandleComponent()
{
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle)
{
// Physics handle is found
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s missing physics handle component"), *GetOwner()->GetName());
}
}
/// Look for attached Input Component (only appears at runtime)
void UGrabber::SetupInputComponent()
{
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent)
{
UE_LOG(LogTemp, Warning, TEXT("Input component found"))
/// Bind the input axis
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s missing input component"), *GetOwner()->GetName());
}
}
void UGrabber::Grab()
{
UE_LOG(LogTemp, Warning, TEXT("Grab pressed"));
// LINE TRACE and see if we reach any actors with physics body collision channel set
auto HitResult = GetFirstPhysicsBodyInReach();
auto ComponentToGrab = HitResult.GetComponent();
// If we hit something, then attach a physics handle
// TODO attach physics handle
PhysicsHandle->GrabComponentAtLocationWithRotation(
ComponentToGrab, // component to grab
NAME_None, // grab what bone name, if any
ComponentToGrab->GetOwner()->GetActorLocation(),// grab location
ComponentToGrab->GetOwner()->GetActorRotation() // grab rotation
);
}
void UGrabber::Release()
{
UE_LOG(LogTemp, Warning, TEXT("Grab released"));
// TODO release physics handle
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// if the physics handle is attached
// move the object that we're holding
}
const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
{
/// Get player viewpoint this tick
FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);
/*UE_LOG(LogTemp, Warning, TEXT("Location: %s Rotation: %s"), *PlayerViewPointLocation.ToString(), *PlayerViewPointRotation.ToString())*/
FVector LineTraceEnd = PlayerViewPointLocation + (PlayerViewPointRotation.Vector() * Reach);
/// Setup query parameters
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
/// Line-trace out to reach distance
FHitResult Hit;
GetWorld()->LineTraceSingleByObjectType(
OUT Hit,
PlayerViewPointLocation,
LineTraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);
// See what we hit
AActor* ActorHit = Hit.GetActor();
if (ActorHit)
{
UE_LOG(LogTemp, Warning, TEXT("Line trace hit: %s"), *(ActorHit->GetName()))
}
return Hit;
}
Grabber.h:
// Copywrite Steve Murray 2019
#pragma once
#include "CoreMinimal.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Components/ActorComponent.h"
#include "Components/InputComponent.h"
#include "Grabber.generated.h"
class UInputComponent;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UGrabber : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabber();
//protected:
// Called when the game starts
virtual void BeginPlay() override;
//public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
// How far ahead the player can reach in cm
float Reach = 100.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* InputComponent = nullptr;
// Ray-cast and grab what's in reach
void Grab();
// Called when grab is released
void Release();
// Find attached physics handle
void FindPhysicsHandleComponent();
// Setup (assumed) attached input component
void SetupInputComponent();
// Return hit for first physics body in reach
const FHitResult GetFirstPhysicsBodyInReach();
};