Damaging an Actor from a custom NPC class in Player class when hit by a line trace

Hi.
I created a simple weapon that fires using line trace. Since there’s no way to detect if an object has been hit with a line trace (if there is let me know) I have to create the damaging system in the player class. I tried this:

    			if (FireHit.Actor->GetClass()->IsChildOf(ANPC::StaticClass()))
    			{
    				ANPC* HitNPC = FireHit.Actor;
    				HitNPC->Damage(10.0f);
    			}

But we cannot just assume that the actor is a child of ANPC class so that doesn’t work.
I honestly have no idea how to do this.

If you know how to help me, then please post an answer to my question.
Thanks!

Hey, it looks like you are looking for casts.

You have to do something like the following:

if (FireHit.Actor.IsValid())
{
    ANPC* HitNPC = Cast<ANPC>(FireHit.Actor.Get());
    if(HitNPC)
    {
         HitNPC->Damage(10.0f);
    }
}

Cast “converts” the AActor to your ActorType and you can call your functions on it. If the cast fails it returns a nullptr, so you have to do the nullcheck before using it.

Thank you, I actually thought, that cast exists only in blueprints.