Can't set OnComponentEndOverlap in constructor c++

Hi there.

I’m working on an infinite runner project in C++. I’m working on destroying the tiles when the player leaves them. To do so, I have a BoxComponent on every tile, and when the character leaves this volume, I destroy the tile. I’m setting a OnComponentEndOverlap delegate method in my code, but I have a problem with this line :

DestroyVolume->OnComponentEndOverlap.AddDynamic(this, &ATileBase::OnLeaveDestroyVolume);

If I call it in the constructor, nothing happens, but if I call it in the BeginPlay() everything works fine. I think it’s ok to call it in BeginPlay but I would like to understand why it’s not working in the constructor as I’ve seen plenty of codes doing it in it.

Here is the code I try to call in the constructor (I set a root component and an arrow component before these lines) :

DestroyVolume = CreateDefaultSubobject<UBoxComponent>(FName("DestroyVolume"));
DestroyVolume->AttachTo(RootComponent);
DestroyVolume->bGenerateOverlapEvents = true;
// Setup collision of the destroyVolume
DestroyVolume->OnComponentEndOverlap.AddDynamic(this, &ATileBase::OnLeaveDestroyVolume);

and here is my delegate method :

void ATileBase::OnLeaveDestroyVolume(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex)
{
	if (Cast<ARunnerProjectCharacter>(OtherActor))
	{
		Destroy();
	}
}

Thanks in advance if you can help :slight_smile:

You need to declare UFUNCTION(). Put UFUNCTION() before delegate method

It is already a UFUNCTION in the .h, forgot to past it in the post though :

UFUNCTION()
void OnLeaveDestroyVolume(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

Do I need some keywords in the UFUNCTION() macro ?

No, it should work fine without keywords.
Put it under private, AFAIK it creates problems when not declared as private

and try this for declaration:

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

And change it in .cpp of course.
Also check if your character is the same collision channel as your actor.

Making it private worked. Is there a place in the doc where I can find this kind of information or did you work it out through experimentation ?
Thanks a lot for the help =)