C++ Overlap events don't work

Hi, I have some problems with c++. I tried to copy overlap events tutourials from various sources, but none of them worked. I copied this video 1:1: UE4 c++ OnComponentOverlap and OnComponentHit syntax - YouTube. The problem is this line of code: _collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap); and the “.” between BeginOverlap and AddDynamic is red underlined. Did something change between the versions or why doesn’t it work? Here is my whole code:

HeaderFile:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor3.generated.h"

UCLASS()
class YOUTUBE_API AMyActor3 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor3(const FObjectInitializer& objectInitializer);

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

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

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	class USphereComponent* _collision;

	void OnDelegateExampleOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult& SweepResult);
};

cpp file:

#include "Youtube.h"
#include "Components/SphereComponent.h"
#include "MyActor3.h"


// Sets default values
AMyActor3::AMyActor3(const FObjectInitializer& objectInitializer)
	:Super(objectInitializer)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	_collision = CreateDefaultSubobject<USphereComponent>(TEXT("RootCollision"));

	_collision->SetSphereRadius(250.f);
	_collision->SetHiddenInGame(false);
	_collision->OnComponentBeginOverlap.AddDynamic(this, &AMyActor3::OnDelegateExampleOverlap);

	RootComponent = _collision;

}

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

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

}

void AMyActor3::OnDelegateExampleOverlap(AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult & SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Something overlapped");
}

And I have another question I dont understand: I always have to include " #include “Components/SphereComponent.h”" to my cpp file to use sphere/box/capsule collisions. In the tutourials, the never have to implement this. Why?

Hi there,

The likely cause is that the delegate signature is incorrect. The tutorial you referenced looks to be outdated a bit and the new signature you’re looking for, found in PrimitiveComponent.h still, includes an extra “OverlappedComponent” pointer like so:

(UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult)

So your function body should read

void OnDelegateExampleOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bfromSweep, const FHitResult& SweepResult);

If that doesn’t fix the issue then it could be a problem with where you’re binding to the function (I’ve had some weird problems with binding it in the constructor in the past), so you could try binding it on begin play instead. If not that then it could be that the collision settings are such that overlap events aren’t triggered, but if you’re just using the default settings from a new project then that shouldn’t be an issue. Or even having bGenerateOverlapEvents to true is necessary. But try it in that order first and see if it works.

I hope that helps!

To answer your second question though, as of 4.16 the engine now uses an “Include What You Use” system for compiling dependencies so compile times can be faster, which means you have to include what you need explicitly since many header files that used to be included by default are no longer included. The documentation explains it a lot better than I can. Link

Thx for the reply. I could solve it and now I can contiue working on my game. :slight_smile: