当玩家触及NPC时,虚幻4崩溃

时间:2018-04-16 14:01:17

标签: unreal-engine4

所以我正在使用虚幻4的C ++进行测试程序,其中我有一个随方向键移动的角色,我有一个NPC角色。我想这样做,当玩家到达NPC的碰撞舱时,它会显示一条消息“嗨,我是欧文”。但是一旦玩家这样做,Unreal崩溃了,我就退出了。我认为这与我提出的NPC代码有关,但我没有看到问题。有人可以帮忙吗?

Crash Report NPC.h

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Components/SphereComponent.h"
#include "Engine/Canvas.h"
#include "Engine/Font.h"
#include "Engine/World.h"
#include "NPC.generated.h"



UCLASS()
class GOLDEGG_API ANPC : public ACharacter
{
GENERATED_BODY()


public:
// Sets default values for this character's properties
ANPC(const FObjectInitializer& ObjectInitializer);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
    FString NpcMessage;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
    USphereComponent* ProxSphere;

UFUNCTION(BlueprintNativeEvent, Category = "Collision")
    void Prox(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, 
UPrimitiveComponent* OtherComp,
        int32 OtherBodyIndex, bool bFromSweep, const FHitResult& 
SweepResult);

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
    FString name;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
    UTexture2D* Face;

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* 
PlayerInputComponent) override;
};

NPC.cpp

#include "NPC.h"
#include "Engine/Canvas.h"
#include "Engine/Font.h"
#include "MyHUD.h"
#include "Avatar.h"



// Sets default values
ANPC::ANPC(const FObjectInitializer& ObjectInitializer) : 
Super(ObjectInitializer)
{
// Set this character to call Tick() every frame.  You can turn this off to 
improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
ProxSphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent> 
(this, TEXT("Proximity Sphere"));
ProxSphere->AttachToComponent(RootComponent, 
FAttachmentTransformRules::KeepWorldTransform);
ProxSphere->SetSphereRadius(150.0f);
ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::Prox);
NpcMessage = "Hi, I'm Owen";

}

// Called when the game starts or when spawned
void ANPC::BeginPlay()
{
Super::BeginPlay();

}

 // Called every frame
void ANPC::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void ANPC::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);

}

void ANPC::Prox_Implementation(UPrimitiveComponent* OverlappedComponent, 
AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (Cast<AAvatar>(OtherActor) == nullptr)
{
    return;
}
APlayerController* PController = GetWorld()->GetFirstPlayerController();
if (PController)
{
    AMyHUD * hud = Cast<AMyHUD>(PController->GetHUD());
    hud->addMessage(Message(NpcMessage, 5.0f, FColor::White));
    //name + FString(":") + message
}
}

1 个答案:

答案 0 :(得分:0)

看起来hud->addMessage()调用特别错误。可能hud为空或PController->GetHUD()为空吗?

此外,ANPC::Prox()定义在哪里?我注意到这里引用了ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::Prox);