# What is the proper way to cast during overlap events?

**URL:** <https://forums.unrealengine.com/t/what-is-the-proper-way-to-cast-during-overlap-events/1754370>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question, CPP\
**Created:** [March 15, 2024, 5:53pm UTC](https://forums.unrealengine.com/t/what-is-the-proper-way-to-cast-during-overlap-events/1754370 "2024-03-15T17:53:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Redsss429](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Redsss429](https://forums.unrealengine.com/u/Redsss429)\
**Post date:** [March 15, 2024, 5:53pm UTC](https://forums.unrealengine.com/t/what-is-the-proper-way-to-cast-during-overlap-events/1754370/1 "2024-03-15T17:53:20Z")

</div>

Hi! I’m experimenting with c++ in unreal and trying to figure out the best way to program in an overlap event.

So far, I have the overlap functioning, but now I’m at the stage where I want to the overlap to identify the other actor and run a function. The problem being, im unsure how to properly cast in this situation. My current code functions as such:

```auto
#include "ShooterCharacter.h"

void ACoin::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	AShooterCharacter* tmp = Cast<AShooterCharacter>(OtherActor);
	tmp->AddCoin();
	this->Destroy();

}

```

As you can see, it casts the other actor to the class I want to identify, and then attempts to run a function on said actor. To do this, I have to include the header file for the actor, which feels wrong. Is this the correct way to do this or is there some other method I’m missing.

Any advice would be appriciated!

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [March 15, 2024, 9:07pm UTC](https://forums.unrealengine.com/t/what-is-the-proper-way-to-cast-during-overlap-events/1754370/2 "2024-03-15T21:07:32Z")

</div>

Have ShooterCharacter implement an interface that has the function AddCoin. No need to cast in that case just check if the OtherActor implements the interface and call AddCoin on it.

Here is a good example:

> [@Calling Interface Functions In C++](https://forums.unrealengine.com/t/calling-interface-functions-in-c/323521/9):
>
> I came across this topic when trying to solve a specific UInterface related issue and I thought I might add one important thing to the discussion. There are actually 2 scenarios in which we’re calling UInterface functions in C++: When interface is inherited by C++ class When interface is added in the blueprint Class Settings Each of those requires different handling in C++ when calling an interface function. First scenario: if (Cast\<IInterface\_MyInterface\>(MyActor)) { Cast\<IInterface\_MyIn…

Except you would have for example

```auto
Execute_AddCoin

```

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [April 14, 2024, 9:07pm UTC](https://forums.unrealengine.com/t/what-is-the-proper-way-to-cast-during-overlap-events/1754370/3 "2024-04-14T21:07:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
