我正在Windows 10机器上使用虚幻4.20和C ++进行简单的房间游戏逃生。代码可以很好地构建/编译,但是当我点击播放时,引擎崩溃了。我尝试重新启动计算机,删除配置,内容,源文件夹和.uproject文件以外的所有文件文件/目录。我尝试删除引擎,但是由于管理员权限,它不允许我使用。我目前在“开门”课程中
这是我的OpenDoor.h文件:
#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
};
这是我的OpenDoor.cpp文件:
// 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();
}
}
我是Unreal的新手,并且在一般情况下都将C ++作为一种语言而苦苦挣扎,所以我想知道这是否与我的代码有关。任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
您必须检查
的空指针PressurePlate
和
ActorThatOpens
这也是在使用所有指针之前保护所有指针的一种好习惯,这样就不会因延迟空指针而导致崩溃。即使这样,您的代码也应该可以工作,可能您忘记了在编辑器上设置引用。
我为您修复了该问题
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();
}
}