# How to use a trigger volume with c++?

**URL:** <https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 9, 2015, 7:05am UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093 "2015-03-09T07:05:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nineclicks](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nineclicks](https://forums.unrealengine.com/u/nineclicks)\
**Post date:** [March 9, 2015, 7:05am UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093/1 "2015-03-09T07:05:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [March 9, 2015, 2:41pm UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093/2 "2015-03-09T14:41:41Z")

</div>

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 ^^

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [March 12, 2015, 5:51pm UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093/3 "2015-03-12T17:51:19Z")

</div>

Do you require any further assistance on your issue?

---

<div class="post-metadata">

**Author:** ![asperatology](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@asperatology](https://forums.unrealengine.com/u/asperatology)\
**Post date:** [March 22, 2015, 1:56am UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093/4 "2015-03-22T01:56:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [March 22, 2015, 6:56pm UTC](https://forums.unrealengine.com/t/how-to-use-a-trigger-volume-with-c/310093/5 "2015-03-22T18:56:29Z")

</div>

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](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Delegates/index.html). I normally use dynamic delegates in those cases because if the abillity to serialize them to disc.
