SphereComponent implementation

Hello guys, I am having a trouble with a blueprintnativeevent (I’m following this book: Learning C++ by Creating Games with UE4). My classes look like this:
NPC.cpp →

// Fill out your copyright notice in the Description page of Project Settings.
#include "MyFirstCpp.h"
#include "NPC.h"
#include "MyHUD.h"
#include "Avatar.h"

ANPC::ANPC(const FObjectInitializer& PCIP) : Super(PCIP)
{
	ProxSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("Proximity Sphere"));
	ProxSphere ->AttachParent = RootComponent;
	ProxSphere->SetSphereRadius(32.f);
	//Code to make ANPC::Prox() run when this proximity sphere overlaps
	//another actor.
	ProxSphere->OnComponentBeginOverlap.AddDynamic(this, &ANPC::Prox);
	NpcMessage = "Hi, I'm a NPC";
}

void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Calling Prox Event"));
	}

	if (Cast<AAvatar>(OtherActor) == nullptr)
	{
		return;
	}

	APlayerController* PController = GetWorld()->GetFirstPlayerController();
	if (PController)
	{
		AMyHUD* hud = Cast<AMyHUD>(PController->GetHUD());
		hud -> addMessage(Message(NpcMessage, 5.f, FColor::White));
	}
}

NPC…h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Character.h"
#include "NPC.generated.h"

UCLASS()
class MYFIRSTCPP_API ANPC : public ACharacter
{
	GENERATED_BODY()
	// Sets default values for this character's properties
public:
	ANPC(const FObjectInitializer& ObjectInitializer);
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	USphereComponent* ProxSphere;
	// This is the NPC's message that he has to tell us.
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	FString NpcMessage;
	// When you create a blueprint from this class, you want to be
	// able to edit that message in blueprints,
	// that's why we have the EditAnywhere and BlueprintReadWrite
	// properties.
	UFUNCTION(BlueprintNativeEvent, Category = Collision)
	void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
	
	virtual void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
};

HUD.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyFirstCpp.h"
#include "MyHUD.h"

void AMyHUD::DrawMessages()
{
	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);
		}
	}
}

void AMyHUD::DrawHUD()
{
	Super::DrawHUD();
	//DrawText("Greetings from Unreal!", FVector2D(1, 1), hudFont, FVector2D(3, 3), FColor::White);
	DrawMessages();
}

void AMyHUD::addMessage(Message msg)
{
	messages.Add(msg);
}

HUD.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"

/**
 * 
 */
struct Message
{
	FString message;
	float time;
	FColor color;
	Message()
	{
		time = 5.f;
		color = FColor::White;
	}
	Message(FString iMessage, float iTime, FColor iColor)
	{
		message = iMessage;
		time = iTime;
		color = iColor;
	}
};

UCLASS()
class MYFIRSTCPP_API AMyHUD : public AHUD
{
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont)
	UFont* hudFont;

	TArray<Message> messages;

	virtual void DrawHUD() override;

	void DrawMessages();
	
	void addMessage(Message msg);

	
};

Now my problem is that the sphere does nothing. I don’t know if it isn’t implemented well or something and I am searching through internet for answer but can’t find a solution.
I’ll give you some photos of the principal assets.

Hello,

I have a few questions regarding this issue:

  • Are you using a class based on Character?
  • If so, have you tried using a class based on Pawn instead?
  • What exactly are you expecting the Sphere Component to do?

I have the NPCs with the Sphere Component in the same BP. What I want is to “print” in the HUD the string “NPCMessage” when I overlap them.

What’s the problem with classes based on Character?

Character-based classes that use the character movement component require the capsule component to function properly. This means that no other collisions that are added to that class will work, because at this time the character class only supports the basic capsule collision by default.
If you try using a pawn class, I’d be curious to hear the results.

Wow!! That was fast, thank you very much I’m still a newbie in unreal engine :smiley: