So I’m doing a test program with the C++ use of Unreal 4 where I have a character that moves with the directional keys and I have an NPC character. I want to make it so when the player reaches the NPC’s collision capsule, it shows a message saying “Hi, I’m Owen”. But once the player does that, Unreal crashes and I’m exited out. I think it has to do with the NPC code I came up with, but I just don’t see the issue. Could someone please help?
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
}
}
MyHUD.h
#include "Engine/World.h"
#include "Engine/Canvas.h"
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"
/**
*
*/
struct Message
{
FString message;
float time = 5.f;
FColor color = FColor::White;
UTexture2D* tex;
Message(FString iMessage, float iTime, FColor iColor)
{
message = iMessage;
time = iTime;
color = iColor;
}
};
UCLASS()
class GOLDEGG_API AMyHUD : public AHUD
{
GENERATED_BODY()
public:
void addMessage(Message mg);
protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont) UFont* hudFont;
TArray<Message>messages;
virtual void DrawHUD() override;
};
MyHUD.cpp
#include "MyHUD.h"
#include "Engine/Canvas.h"
#include "Engine/Font.h"
#include "Kismet/GameplayStatics.h"
#include "Avatar.h"
void AMyHUD::DrawHUD()
{
DrawHUD();
for (int c = messages.Num() - 1; c >= 0; c--)
{
float outputWidth, outputHeight, pad = 10.f;
GetTextSize(messages[c].message, outputWidth, outputHeight, hudFont, 1.f);
float messageH = outputHeight + 2.f*pad;
float x = 0.f, y = c * messageH;
DrawRect(FLinearColor::Black, x, y, Canvas->SizeX, messageH);
DrawText(messages[c].message, messages[c].color, x + pad, y + pad, hudFont);
messages[c].time -= GetWorld()->GetDeltaSeconds();
if (messages[c].time < 0)
{
messages.RemoveAt(c);
}
}
DrawLine(200, 300, 400, 500, FLinearColor::Blue);
DrawText("Greetings from Unreal!", FColor::White, 50, 50, hudFont);
}
void AMyHUD::addMessage(Message msg)
{
messages.Add(msg);
}