How to use a trace to pickup Objects

I would like to use a trace in order to make sure the player is looking at the object to pickup. How do i check if the actor hit is a pickup?

if (Trace(HitActor, this, StartofTrace, EndofTrace))
{
SelectedActor = HitActor.GetActor();
APickUp* TestPickup = Cast(SelectedActor);
AFlashLightBattery* const FlashLightBatteryPU = Cast(SelectedActor);
if (TestPickup && !TestPickup->IsPendingKill() && TestPickup->IsActive())
{
TestPickup->OnPickedUp();
TestPickup->SetActive(false);
FlashLightBatteryPU->UpdateFlashLightBatteryLevel(this);
}
}
Here is my code i am using

You need some error checking in your code so it doesn’t crash:

if (Trace(HitActor, this, StartofTrace, EndofTrace))
 {
     SelectedActor = HitActor.GetActor();
     if(SelectedActor) { // you don't necessarily hit an actor
          APickUp* TestPickup = Cast<APickUp>(SelectedActor);
          if(TestPickup) { // Actor hit is not necessarily of type 'APickUp' or derived from it
               AFlashLightBattery* const FlashLightBatteryPU = Cast<AFlashLightBattery>(SelectedActor);
               if(FlashLightBatteryPU) { // 'APickUp' might not be a 'AFlashlightBattery'
                    if (!TestPickup->IsPendingKill() && TestPickup->IsActive())
                    {
                        TestPickup->OnPickedUp();
                        TestPickup->SetActive(false);
                        FlashLightBatteryPU->UpdateFlashLightBatteryLevel(this);
                    }
               }
          }
     }
 }

Hey Skullzz098-

Could you elaborate on what behavior you’re getting with the code you provided? Also, is AFlashLightBattery a child of APickUp? I ask because the two lines with “Cast” both cast the SelectedActor to the specified class. Unless AFlashLightBattery and APickUp have some sort of parent/child relation only one of the casts will be successful. Additionally, can you explain what class this code exists in? I’m not sure what “this” in your code is referring to (actor, character, etc).

Cheers

Hi Skullzz098,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.

this is what i am trying to do but the editor keeps crashing
Do a line Trace
{
if (Hit actor == disired class)
{
cast to the class on the hit actor;
use the hit actors functions;
}
}

What i am trying to do is use a line trace to interact with the objects i place in the world. In this case i am trying to pickup a flashlight battery and update the flashlights power level. As well as using the pickup function to destroy it so it wont be constantly spammed. I know i could use a spherecollider but i would rather the player having to look at it instead. BTW “this” just is taking the players current battery level to output the new power level.

Hello,

I noticed that you had an additional post that was recently answered and was similar to what you are reporting here. Is this the same issue, and was it resolved?