Unable to create overlapping event c++

Hi all, i would like to fix some problems here:

  1. I would be able to overlap a collision box that i’ve created and run a function

  2. I would like to have a text over the head of the 2d art

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

    #include “NPC.h”

    // Sets default values
    ANPC::ANPC()
    {
    // 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;

     TextRenderer = CreateDefaultSubobject<UTextRenderComponent>(TEXT("TextRenderer"));
     TextRenderer->SetText(FText::FromString("Text"));
     TextRenderer->SetTextRenderColor(FColor::Yellow);
     TextRenderer->SetXScale(1.0f);
     TextRenderer->SetYScale(1.0f);
     TextRenderer->SetHiddenInGame(true); //not working
    
    
     BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollision"));
     BoxComponent->SetRelativeScale3D(FVector(3.0f, 3.0f, 3.0f));
     BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ANPC::OnOverlapBegin);
    

    }

    // 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::Interact() {

    }

    void ANPC::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
    {

     UE_LOG(LogTemp, Warning, TEXT("Activated"));
     TextRenderer->SetText("aksldnalksdnalsnkdkl");
    

    }

This is the code of the class that i’ve created. But the function OnOverlapBegin never run.

And here is a screenshot of the problem:

Why is the collision box that far from the 2d art? In the blueprint editor it show the collision box in another position.

Thanks,

Davide.

Hey Davide,

I will try to address the problems you faced as best i can. So first up, the overlap event didn’t fire probably because you don’t have the custom function “OnOverlapBegin” setup as a UFUNCTION() like so

Next thing is you should attach your components to the root right after you create them, that would fix the scaling problem and the box being far from your character.

281933-2.png

I can see that you have a root component already so just ignore the SceneComponent lines, and btw the SetHiddenInGame(true) for the TextRenderer should work just fine, it did for me.
Also i recommend using InitBoxExtent instead of the scaling method, but scaling will work as well.

Finally, here is the overlap event and the result

281934-3.png

Thanks for the reply. I’ve made some changes and now seems to works, but when the function is called the editor crash, i suppose the TextRenderer is null. Can you find the error?

.h

UCLASS(Blueprintable, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class SCROLLERGAME_API ANPC : public AActor, public IIInteract
{
	GENERATED_BODY()

public:	

	ANPC();

	UPROPERTY(EditAnywhere, Category = TextRenderer, meta = (AllowPrivateAccess = "true"))
	class UTextRenderComponent* TextRenderer;

	UPROPERTY(EditAnywhere, Category = BoxComponent, meta = (AllowPrivateAccess = "true"))
	class UBoxComponent* BoxComponent;

	UPROPERTY(EditAnywhere, Category = IdleAnimation, meta = (AllowPrivateAccess = "true"))
	class UPaperFlipbookComponent* IdleAnimation;

	UPROPERTY(EditAnywhere, Category = SceneRoot, meta = (AllowPrivateAccess = "true"))
	class USceneComponent* SceneRoot;

	UFUNCTION()
	void OnOverlapBegin(..);
};

.cpp

ANPC::ANPC()
{
	SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("ROOT"));
	RootComponent = SceneRoot;

	IdleAnimation = CreateDefaultSubobject<UPaperFlipbookComponent>(TEXT("IdleAnimation"));
	IdleAnimation->SetupAttachment(RootComponent);

	TextRenderer = CreateDefaultSubobject<UTextRenderComponent>(TEXT("TextRenderer"));
	TextRenderer->SetupAttachment(RootComponent);
	TextRenderer->SetText(FText::FromString("Text"));
	TextRenderer->SetTextRenderColor(FColor::Yellow);
	TextRenderer->SetXScale(1.0f);
	TextRenderer->SetYScale(1.0f);
	TextRenderer->SetHiddenInGame(true);

	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollision"));
	BoxComponent->SetupAttachment(RootComponent);
	BoxComponent->InitBoxExtent(FVector(100, 100, 100));
	BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ANPC::OnOverlapBegin);
}
  
void ANPC::OnOverlapBegin(..)
{
	UE_LOG(LogTemp, Warning, TEXT("Activated"));
	TextRenderer->SetHiddenInGame(false);
}

I’m not seeing any reason for it to crash, can you try removing the actor from the scene and placing it again after compiling the blueprint? sometimes placed actors don’t update when you change its components.
If that doesn’t work, please take a screenshot of the crash log.

Steps done:

  1. Rebuilded the solution
  2. Compiled in the editor
  3. Recompiled the blueprint
  4. Placed again the actor

Result: Still Crash

Well, you were right about TextRenderer being null, uhm is there any custom logic in the blueprint at all? Can you check if the TextRenderer is correctly set up inside the blueprint? Maybe you could try deleting the blueprint completely and making a new one?

Deleting the blueprint and creating a new one based on the c++ class solved the problem. I suppose… it’s a bug? Thanks for the help!

Sometimes changing components and the root in C++ messes up already existing blueprints so keep that in mind while working in the future in case something doesn’t behave the way you think it should. No problem mate, glad i could help :smiley: