Reputation:
I'm making a Simple escape the room game using unreal 4.20 and C++ on my Windows 10 Machine. The code builds/complies just fine but when I hit play the Engines crashes. I've tried restarting my computer, deleting all files files/directories except for config, content, source folders, and the .uproject file. I tried deleting the engine, but it won't let me due to administrator permissions. I'm currently on the "Open door" class
Here is my OpenDoor.h file:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine/TriggerVolume.h"
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UOpenDoor();
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
void OpenDoor();
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction) override;
public:
UPROPERTY(VisibleAnywhere)
float OpenAngle = 90.0f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
AActor* ActorThatOpens; // Remember pawn inherits from actor
};
Here is my OpenDoor.cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "OpenDoor.h"
// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
// 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 UOpenDoor::BeginPlay()
{
Super::BeginPlay();
// ...
}
void UOpenDoor::OpenDoor()
{
// Find the owning Actor
AActor* Owner = GetOwner();
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Poll the Trigger Volume
// If the ActorThatOpens is in the volume
if (PressurePlate->IsOverlappingActor(ActorThatOpens))
{
OpenDoor();
}
}
I'm new to Unreal and struggle with C++ as a language in General, so I wonder if its an issue with my code. Any help would be greatly appreciated. Thanks!
Upvotes: 0
Views: 680
Reputation: 11
You have to check the null pointers of the
PressurePlate
and the
ActorThatOpens
It's also a good practice to secure all pointers before using them so you don't get a crash for deferencing null pointers. Even so, your code should work, probably you are forgetting to set the reference on the editor.
I fixed that for you:
void UOpenDoor::OpenDoor()
{
// Find the owning Actor
if(!GetOwner()) return;
AActor* Owner = GetOwner();
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Poll the Trigger Volume
// If the ActorThatOpens is in the volume
if (PressurePlate == nullptr) //Checking for the PressurePlate
{
UE_LOG(LogTemp, Warning, TEXT("Missing PressurePlate");
return;
}
if(ActorThatOpens == nullptr) // Checking for the Door
{
UE_LOG(LogTemp, Warning, TEXT("Missing ActorThatOpens");
return;
}
if (PressurePlate->IsOverlappingActor(ActorThatOpens))
{
OpenDoor();
}
}
Upvotes: 1