GrabTool.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "GrabTool.h"
// Sets default values for this component's properties
UGrabTool::UGrabTool()
{
// 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 UGrabTool::BeginPlay()
{
Super::BeginPlay();
player = static_cast<APawn*> (GetOwner());
UInputComponent *ic = player->InputComponent;
ic->BindAction("GR", IE_Pressed, this, &UGrabTool::GR);
ph = player->FindComponentByClass<UPhysicsHandleComponent>();
if (ph == nullptr) {
UE_LOG(LogTemp, Warning, TEXT("Physics handle not found."));
UE_LOG(LogTemp, Warning, TEXT("There will be some errors."));
}
else {
}
}
// Called every frame
void UGrabTool::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
DrawDebuggers();
}
void UGrabTool::DrawDebuggers() const
{
DrawDebugLine(GetWorld(),
player->GetActorLocation(),
((player->GetActorLocation()) + //one
((player->GetViewRotation().Vector()) * GrabDistance)), //parameter
FColorList::Red,
false,
0.0f,
10.0f,
5.0f);
}
void UGrabTool::GR()
{
FHitResult hit;
FCollisionObjectQueryParams fcoqp = FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody);
FCollisionQueryParams fcqp = FCollisionQueryParams(FName(TEXT("")), false, player);
GetWorld()->LineTraceSingleByObjectType(hit,
player->GetActorLocation(),
((player->GetActorLocation()) + //one
((player->GetViewRotation().Vector()) * GrabDistance)), //parameter
fcoqp,
fcqp);
UE_LOG(LogTemp, Warning, TEXT("Object name is %s"), *(hit.GetActor()->GetName()));
}
**GrabTool.h/B]
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
#include "Components/ActorComponent.h"
#include "GrabTool.generated.h"
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class DEADEYE_FIGHT_API UGrabTool : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabTool();
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:
UPROPERTY(Category = "Grab Tool", EditAnywhere)
float GrabDistance = 100.f;
bool IsGrabbing = false;
void DrawDebuggers() const;
void GR();
APawn* player;
UPhysicsHandleComponent *ph;
};
When i run this code the ray hits my object and outputs the name correctly.But,if pressed my button when there is no actor the engine crashes.
Another question:
when i put this code in the constructor
player = static_cast<APawn*> (GetOwner());
UInputComponent *ic = player->InputComponent;
ic->BindAction("GR", IE_Pressed, this, &UGrabTool::GR);
ph = player->FindComponentByClass<UPhysicsHandleComponent>();
if (ph == nullptr) {
UE_LOG(LogTemp, Warning, TEXT("Physics handle not found."));
UE_LOG(LogTemp, Warning, TEXT("There will be some errors."));
}
else {
}
the engine crashes after the game lunches.