Possess on Linetrace Hit. From BP to C++

AController* PossingController = this->GetController();
...
if (HasHit)
    PossessAI(PossingController);
...
PossessingController->Possess(this);

You are possessing yourself after any successful trace, that doesn’t make sense.
It would make more sense to check the hit result actor, and possess it if it is a pawn. Something like :

auto HitPawn = Cast<APawn>(HitResult.GetActor());
if (HitPawn)
    PossingController->Possess(HitPawn);

If you want to use an interface it’s very similar but your have to use c++ specifics for interfaces. Something like :

auto IPossess = Cast<IPossessInterface(HitResult.GetActor());
if (IPossess)
    IPossessInterface::Execute_PossessAI(IPossess, PossingController);

This of course requires you implement the interface in C++.


If this is too much of a burden you can call the blueprint interface but that’s probably not recommended :

if (APawn* P = Cast<APawn>(HitResult.GetActor()))
{
    if (UFunction* Func = P->FindFunction("PossessAI"))
    {
        struct temp { UObject* a; APlayerController* b; } Parms = { HitResult.GetActor(), PossessController };
        P->ProcessEvent(Func, &Parms);
    }
}
1 Like