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