Search code examples
c++unreal-engine4

UE4 Casting OtherActor on OnComponentBeginOverlap


Am new at UE4. Now i am colliding two classes. a pawn and a character class. My OnComponentBeginOverlap dynamic is in pawn class. That means i am trying to get OtherActor from my pawn class. OtherActor should be a ACharacter class. Now let's see the parameter signature:

void Adenn_pawn::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)

Look at the second parameter. it defines AActor class but i dont want access it's parent classes. I can use the AActor functions or properties. Actually i want to use ACharacter because it is a ACharacter Class. Any ideas about casting this "OtherActor" as its own class. Or how can i change to a subclass?

Thanks in advance.


Solution

  • You can try casting OtherActor to a variable of type ACharacter* using the Cast function. Luckily the function will check if the actor overlapped is of type ACharacter, so this is helpful when you're not sure if OtherActor is of type ACharacter. You can call the Cast function and do what you wish with the casted actor like so,

    if (ACharacter* OtherCharacter = Cast<ACharacter>(OtherActor))
    {
        // do stuff with OtherCharacter
    }
    else
    {
        // OtherActor does not point to an object of type ACharacter
    }