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"

Crash when Iterating through a TArray whith for loop

I have been following an Unreal Engine course on Udemy and at a point in the course, we were required to get the mass of all the actors that overlap with a certain Trigger Volume(namely PressurePlate). We achieved this by using GetOverlappingActors() member of TriggerVolume and then iterating through each item to get mass.

But when I try to iterate through the members of the TArray(out arg of GetOverlappingActors), the editor crashes on play. The log says that there is an Unhandled Exception is the line with the for loop.

The function Defination -:

 float UOpenDoor::GetTotalMassOfActorsOnPlate()

{

     float TotalMass = 60.0f;
      TArray<AActor*> OverlappingActors;
 
 // Find all the overlaping actors
 PressurePlate->GetOverlappingActors(OUT OverlappingActors);

 // Iterate through their masses
 for (const auto& Actor : OverlappingActors)
 {
     TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();

     UE_LOG(LogTemp, Warning, TEXT("%s is on pressure plate."), *Actor->GetName())
 }
 
 return TotalMass;

}

The Function call -:

  if (GetTotalMassOfActorsOnPlate() > MassThreshold) 
 {
     OpenDoor(); 
 }

Any help would be appreciated.

EDIT1: The crash report-:

[1]: /storage/temp/274255-crash-report.txt

EDIT2: Spelling errors.

Product Version: UE 4.22
Tags:
more ▼

asked Apr 14 '19 at 07:22 PM in C++ Programming

avatar image

Blonteractor
1 1 1

avatar image Lardo Deepdelver Apr 14 '19 at 07:43 PM

The crash stack trace would make it way easier to help you out.

Atm random best guesses

Actor->FindComponentByClass() is null for whatever strange reason the actor has not Primitive component and you try to GetMass() on a nullptr object.

const auto& Actor is of type AActor* const& which is weird but not a crash reason

avatar image Blonteractor Apr 15 '19 at 01:52 PM

I attached a text file containing everything is the Unreal Crash Reporter.

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

1 answer: sort voted first

Without knowing exactly what is on line 75 of opendoor.cpp, I'm going to guess it's this one:

  TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();

Here you're assuming that every overlapping actor has a UPrimitiveComponent. If an actor doesn't have a primitive component (which is quite possible) then you'll be trying to GetMass from a null pointer, which would cause the error you see.

So, to fix you could check that the component exists before attempting to GetMass. Quick way is to do something like:

 UPrimitiveComponent* comp =  Actor->FindComponentByClass<UPrimitiveComponent>();
     if(comp != nullptr)
     {
        TotalMass += comp->GetMass();
     }

But personally I'd look at trying a different method which is a bit more optimal. FindComponentByClass can be expensive to run multiple times per tick so perhaps using an Interface or a custom Actor class which you can query / filter more directly would be beneficial. It depends on what you're trying to do though.

more ▼

answered Apr 15 '19 at 02:11 PM

avatar image

phil_me_up
1.5k 6 6 12

avatar image Blonteractor Apr 15 '19 at 02:40 PM

Line 75 of opendoor.cpp is the start of the for-loop. And Yes, I did try putting NULL value checkers wherever I could (on every step basically) but the SAME problem persists.

avatar image phil_me_up Apr 15 '19 at 04:39 PM

Just try commenting out the line I thought was line 75 (where you increase the value of TotalMass) and see if you still get the error.

if the error is cleared, you know the issue is with a null actor or a null return from FindComponentByClass so re-implement your null safety checks and ensure you're catching every case.

If you still get the error then check that GetOverlapping Actors is working as expected (again, you could just comment it out for now). I never use OUT in that way but I assume its just a convinient way to pass by reference

(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