Why is my OnBeginOverlapDelegate not working:

I tried everything. It seems no matter what i do i cant make my OnDelegateExampleOverlap work.
What am i doing wrong?

Video:

Code:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "ActorCollisionTest.generated.h"

UCLASS()
class CPP_TEST_COLLISION_API AActorCollisionTest : public AActor
{
	GENERATED_BODY()
public:	
	AActorCollisionTest(const FObjectInitializer& objectInitializer);
	void OnDelegateExampleOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	
	USphereComponent* collision1;
protected:
	virtual void BeginPlay() override;
public:	
	virtual void Tick(float DeltaTime) override;

};


//////////////
//////////////

#include "ActorCollisionTest.h"

AActorCollisionTest::AActorCollisionTest(const FObjectInitializer& objectInitializer)
	:Super(objectInitializer)
{
	PrimaryActorTick.bCanEverTick = true;
	collision1 = CreateDefaultSubobject<USphereComponent>(TEXT("RootCollision"));
	collision1->SetSphereRadius(300.f);
	collision1->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	collision1->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
	collision1->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
	collision1->SetHiddenInGame(false);
	RootComponent = collision1;
}
void AActorCollisionTest::BeginPlay()
{
	Super::BeginPlay();
	collision1->SetGenerateOverlapEvents(true);

	collision1->OnComponentBeginOverlap.AddDynamic(this, &AActorCollisionTest::OnDelegateExampleOverlap);
	FTimerHandle timerrr;
	GetWorld()->GetTimerManager().SetTimer(timerrr, [&]()
		{
	FHitResult HResult;
	collision1->SetWorldLocation(FVector(0, 0, 0), true, &HResult, ETeleportType::None);
	TArray<AActor*> OverlappingActors;
	collision1->GetOverlappingActors(OverlappingActors);
	if (OverlappingActors.Num())
		GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Cyan, "Bruh 2");
		}, 4, false);
	//if (collision1->UpdateOverlaps())
	//	GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Cyan, "Bruh 2");



}
void AActorCollisionTest::OnDelegateExampleOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 4.0f, FColor::Cyan, "Bruh");
}
void AActorCollisionTest::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

1- So the first issue is that the function must have the macro UFUNCTION()
Else it wont work.

2- The reason why it doesnt detect the overlaps is because it is detecting the overlap too close to when you are binding the Delegate. So apparently that will mess things up.

Solution:
Use:
ClearComponentOverlaps();
After whatever first overlaps you have at BeginPlay(). And give it some time before you move your actor.

Or start with:
collision1->SetGenerateOverlapEvents(false);
And after a while set it to true.

1 Like