Help with C++ pointers or scope

Hello and thanks for the help!
I’m stuck on trying to understand a concept in C++. I believe it’s related to something simple I don’t understand about pointers or scope in C+. Can anyone help me understand what I am doing wrong?

I have a custom player controller which attaches a few copies of an ActorComponent to themselves. The component is supposed to do cursorhit traces for custom channels. Here is a working blueprint mockup of exactly what the component is supposed to do: https://i.imgur.com/PKZMwCa.jpg

A Blueprint Mockup of what i want the component to be doing: This does work and returns a hit but my C++ duplicate of this does not unless I do this code directly in the player controller. (This leads me to believe that my PlayerController reference in C+ is not correct, maybe an issue with scope or pointer?)

Thing.jpg

The player controller I call: EnvoyC
The actor component I call: CursorTraceComponent…

If I call the trace directly in my PlayerController class it works. If I call it from the ActorComponent or make the ActorComponent use a function in the PlayerController it doesn’t return a HIT.

  • Despite the hit tracing not connecting I don’t think my issue here has anything to do with hit tracing but instead me not understanding how to use references and scope correctly.

Last note is that my print() outputs confirm that the function is being called just the trace is not hitting. On the failed HITs it’s getting stuck on the internal part of the function (GetHitResultUnderCursorByChannel) of casting to LocalPlayer.

AEnvoyC.cpp

My extending of the PlayerController class.




AEnvoyC::AEnvoyC()
{


    RaytracerSelect = CreateDefaultSubobject<UCursorTraceComponentC>(TEXT ("SelectorTracer"));
    //Added the reference here to the component to complete the linetrace
    RaytracerSelect->PControllerRef = Cast<APlayerController>(this);
    RaytracerSelect->PEnvoyRef = this;

    RaytracerSelect->RegisterComponent();
    RaytracerSelect->TraceType = static_cast<ETraceTypeQuery>(2);    //ENUMWASUSED
}

void AEnvoyC::ClickLeft_Pressed()
{
    RaytracerSelect->ClickLeft();

    // As a bandaid I bypassed the component and call it directly here and it does register a hit CursorTrace()
    // CursorTrace()
}

void AEnvoyC::CursorTrace()
{
    //print("Trace");
    FHitResult TraceResult;
    if (GetHitResultUnderCursorByChannel(UEngineTypes::ConvertToTraceType(ECC_GameTraceChannel1), true, TraceResult))
        {
        //print("Trace hit something");
        }
}



CusorTraceComponentC.cpp

A modular component that the Envoy class will create and attach multiple copies of, to activate / deactive when certain custom traces are need for mouse hover.
(My game needs simultaneous traces of different channels depending on the situation.




void UCursorTraceComponentC::ClickLeft()
{
    /**
    Would like
    this code to be here but it doesn't register a HIT

    FHitResult TraceResult;

    if (PControllerRef->GetHitResultUnderCursorByChannel(UEngineTypes::ConvertToTraceType(ECC_GameTraceChannel1), true, TraceResult))
        {
        //print("Trace hit something");
        }

   */

   //Calling the function in the PlayerController as bandaid attempt to fix this also does not make the HIT happen.
   //AEnvoyC->CursorTrace();
}



The related headers in case this is needed to solve the problem:

[SPOILER]

EnvoyC.h




    UPROPERTY(BlueprintReadWrite, Meta = (DisplayName = "CursorTrace Select"), Category = "Cursor Traces")
    UCursorTraceComponentC* RaytracerSelect;
    UPROPERTY(BlueprintReadWrite, Meta = (DisplayName = "CursorTrace Articles"), Category = "Cursor Traces")
    UCursorTraceComponentC* RaytracerSpecialSelect;

    UFUNCTION()
    void CursorTrace();

private:
    UFUNCTION()
    void ClickLeft_Pressed();
    UFUNCTION()
    void ClickRight_Pressed();
    UFUNCTION()
    void ClickLeft_Released();
    UFUNCTION()
    void ClickRight_Released();
};




CusorTraceComponentC.h





    UPROPERTY()
    APlayerController* PControllerRef;

    UPROPERTY()
    AEnvoyC* PEnvoyRef;

    UPROPERTY(BlueprintReadWrite)
    TEnumAsByte<ETraceTypeQuery> TraceType;

    UFUNCTION()
    void ClickLeft();


[/SPOILER]

Lastly i did bust out the UE4 function GetHitResultUnderCursorByChannel to see why there was no hit and it wasn’t able to get a cast of the player to a LocalPlayer. This is what lead me to believe it’s a pointer/scope issue in that a copy of the PlayerController and not the actual one is being given (or something like that).

Thanks so much for the help!