x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.15
Tags:
more ▼

asked May 06 '17 at 07:22 PM in C++ Programming

avatar image

J0K3R_12QQ
94 21 24 28

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered May 07 '17 at 02:42 PM

avatar image

DennyR
1.1k 31 12 47

avatar image J0K3R_12QQ May 07 '17 at 05:31 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question