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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

C++ LineTraceMultiByObjectType

Hi. I have set up a blueprint that I am trying to replicate in C++. I can do the trace fine, but I'm struggling to get Hit information from the array as shown in the blueprint. Without it I cant use FHitResult.GetActor() or anything.

alt text

here is the code I have so far

 TArray<FHitResult> HitResults;
     FHitResult HitData(ForceInit);
     FCollisionObjectQueryParams ObjectList;
     ObjectList.AddObjectTypesToQuery(ECC_Pawn);
     FCollisionQueryParams TraceParams = FCollisionQueryParams(FName(TEXT("TraceParams")), false, this);
 
 
 //Do trace
         GetWorld()->LineTraceMultiByObjectType(HitResults, Start, End, ObjectList, TraceParams);
             
                //For each loop
         for (int i = 0; i < HitResults.Num(); i++) 
         {
             
             if (HitData.GetActor()->IsA(AEnemy::StaticClass()))
             {
 
                 AEnemy* enemy = Cast<AEnemy>(HitData.GetActor());
 
                 if (!enemy) return;
 
                 enemy->Damage(10);
             }
         }

Thanks in advance

Product Version: UE 4.12
Tags:
bp.png (142.3 kB)
more ▼

asked Apr 11 '17 at 01:03 AM in C++ Programming

avatar image

thehex
1 1 1 2

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

2 answers: sort voted first

Are you getting a Crash or something? If so Please tell what it says.

Try this instead the foor loop:

 UE_LOG(LogTemp,Warning,TEXT("MyTrace Num Hits: %d"), HitResults.Num() ); // Will appear in your Logs just for debuging if hit anything at all
              for (FHitResult& HitData : HitResults) 
              {
                  
                  if (HitData.GetActor()) // pointer check
                  {
      
                      AEnemy* enemy = Cast<AEnemy>(HitData.GetActor()); // Type Check
      
                      if (!enemy) return; // Pointer Check
      
                      enemy->Damage(10);
                  }
              }

Aside from that make sure GetWorld() is valid everything else looks fine to me.

more ▼

answered Apr 11 '17 at 01:39 AM

avatar image

Nachtmahr
7k 143 28 172

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

ah I actually just fixed this but thanks for the reply!

Basically, I had my array of FHitresults called "HitResults" and a FHitResult variable called HitData.

I just needed to add the following line so Hitdata was fed the information from HitResults on every iteration of the loop.

HitData = HitResults[i];

Caused my game to crash before, but this fixed it. Cant see it being a problem if I extend to multiple enemy types either.

Unless I'm missing something? Just means I will have to cast to every enemy type and declare its ->Damage() function.

more ▼

answered Apr 11 '17 at 02:35 AM

avatar image

thehex
1 1 1 2

(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