How to use a trigger volume with c++?

I have only found one place that describes how to use a trigger volume in c++ and it’s a youtube video with lots of outdated code. How do I use a trigger volume in c++ with UE4.7.2?

You have to use the delegates that such components provide. Here is an example that registeres to a delegate of a BoxComponent used as a trigger volume:

void AMyActorWhichRegisteresWithABox::RegisterDelegate()
{
    if (!MyBoxComponent->OnComponentBeginOverlap.IsAlreadyBound(this, &AMyActorWhichRegisteresWithABox::OnBeginTriggerOverlap))
    {
        MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AMyActorWhichRegisteresWithABox::OnBeginTriggerOverlap);
    }
}

void AMyActorWhichRegisteresWithABox::OnBeginTriggerOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    // This gets called when an actor begins to overlap with MyBoxComponent
}

The basic idea is to register a function to a given event, in our case the BeginOverlap event of the BoxComponent. There are lots of event for both components and actor. One thing to mention is that you should not add the delegate twice and you have to take care of removing the delegate to avoid a memory leak when unloading your current level. Removing the above delegate would be as simple as:

void AMyActorWhichRegisteresWithABox::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    if (MyBoxComponent->OnComponentBeginOverlap.IsAlreadyBound(this, &AMyActorWhichRegisteresWithABox::OnBeginTriggerOverlap))
    {
        MyBoxComponent->OnComponentBeginOverlap.RemoveDynamic(this, &AMyActorWhichRegisteresWithABox::OnBeginTriggerOverlap);
    }
    Super::EndPlay(EndPlayReason);
}

The actor variants for those events are in for of OnActorBeginOverlap. This is exactly the event that you would require to hook into the overlap event of an actor being it a volume of anything else.

If you have more questions I’m more than open to help you out ^^

Cheers,
Moss

EDIT: I’ll resolve this for now, if you need any further assistance please reopen the question ^^

1 Like

Do you require any further assistance on your issue?

Does the function signature for the OnBeginTriggerOverlap() needs to be that, or I can just use a function with no parameters as a delegate?

Also, does the BoxComponent trigger volume require the use of dynamic multicast delegates?

You have to provide a valid function with the required signature. Regarding the other question, you can bind a delegate using different methods explained here. I normally use dynamic delegates in those cases because if the abillity to serialize them to disc.