game crashes when I press play
UE v 4.24
Visual studio 2019
dooropen.cpp
#include "DoorOpen.h"
// Sets default values for this component's properties
UDoorOpen::UDoorOpen()
{
// 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 UDoorOpen::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UDoorOpen::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (TotalMassOfActorsOnPlate() >= TriggerMass)
{
OpenDoor();
DoorOpenTime = GetWorld()->GetTimeSeconds();
}
if (GetWorld()->GetTimeSeconds() - DoorOpenTime > TimeDelay)
{
CloseDoor();
}
}
void UDoorOpen::OpenDoor()
{
Owner->SetActorRotation(FRotator(0.f, OpenAngle, 0.f));
}
void UDoorOpen::CloseDoor()
{
Owner->SetActorRotation(FRotator(0.f, 0.f, 0.f));
}
float UDoorOpen::TotalMassOfActorsOnPlate()
{
float TotalMass = 0.f;
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OverlappingActors);
for (auto &TActor : OverlappingActors)
{
TotalMass += TActor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
UE_LOG(LogTemp, Warning, TEXT("%s colliding"), *TActor->GetName())
}
return TotalMass;
}
DoorOpen.h
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/World.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/TriggerVolume.h"
#include "GameFramework/Actor.h"
#include "GameFramework/PlayerController.h"
#include "DoorOpen.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UDoorOpen : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UDoorOpen();
protected:
// Called when the game starts
virtual void BeginPlay() override;
void OpenDoor();
void CloseDoor();
float TotalMassOfActorsOnPlate();
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UPROPERTY(EditAnywhere)
float OpenAngle = -90.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
AActor* Opener;
UPROPERTY(EditAnywhere)
float TriggerMass = 60.f;
float DoorOpenTime;
UPROPERTY(EditAnywhere)
float TimeDelay = 1.f;
AActor* Owner;
};
grabber.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Grabber.h"
// 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();
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (PhysicsHandle->GrabbedComponent)
{
PhysicsHandle->SetTargetLocation(GetReachLineEnd());
}
}
void UGrabber::FindPhysicsHandleComponent()
{
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (!PhysicsHandle)
{
UE_LOG(LogTemp, Error, TEXT("Component not found add component to %s"), *(GetOwner()->GetName()))
}
}
/// finding the pawn's physics handle component and setting up grab and release
void UGrabber::SetupInputComponent()
{
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent)
{
InputComponent->BindAction(TEXT("Grab"), IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction(TEXT("Grab"), IE_Released, this, &UGrabber::Release);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Component not found add component to %s"), *(GetOwner()->GetName()))
}
}
///grabbing object
void UGrabber::Grab()
{
auto HitResult = GetFirstPhysicsBodyInReach();
if (HitResult.GetActor())
{
PhysicsHandle->GrabComponentAtLocationWithRotation(
HitResult.GetComponent(), //ComponentToGrab
NAME_None, //grab what bone name, if any
HitResult.GetComponent()->GetOwner()->GetActorLocation(), //grab location
HitResult.GetComponent()->GetOwner()->GetActorRotation()//grab rotation
);
}
}
///releasing object
void UGrabber::Release()
{
PhysicsHandle->ReleaseComponent();
}
//gets the physics body hit by line trace
const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
{
FCollisionQueryParams TraceParameters(TEXT(""), false, GetOwner());
FHitResult HitResult;
//Trace line and get actor which it hit
GetWorld()->LineTraceSingleByObjectType
(
OUT HitResult,
GetReachLineStart(),
GetReachLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);
return HitResult;
}
///Gets player location
FVector UGrabber::GetReachLineStart()
{
FVector PlayerViewLocation;
FRotator PlayerViewRotation;
// returns playerviewlocation and rotation
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewLocation, OUT PlayerViewRotation);
return PlayerViewLocation;
}
//gets end point for line trace
FVector UGrabber::GetReachLineEnd()
{
FVector PlayerViewLocation;
FRotator PlayerViewRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(OUT PlayerViewLocation, OUT PlayerViewRotation);
return PlayerViewLocation + PlayerViewRotation.Vector() * Reach;
}
grabber.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Components/PrimitiveComponent.h"
#include "PhysicsEngine/PhysicsHandleComponent.h"
#include "Components/InputComponent.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "Grabber.generated.h"
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;
float Reach = 150.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* InputComponent = nullptr;
FVector GetReachLineStart();
FVector GetReachLineEnd();
void Grab();
void Release();
void FindPhysicsHandleComponent();
void SetupInputComponent();
const FHitResult GetFirstPhysicsBodyInReach();
};
crash report
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000148
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll