What is the proper way to cast during overlap events?

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:

#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!

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:

Except you would have for example

Execute_AddCoin

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