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. 

How can you destroy attached actors when parent is destroyed?

I'm creating a patrol path actor that has a bunch of target points attached to it to mark the path and I'd like to destroy those attached children when you destroyed the parent. I tried this, but the attach list is empty at this point.

 void APatrolPath::BeginDestroy()
 {
     TArray<Actor*> temp;
     GetAttachedActors(temp);
 
     for (int i = 0; i < temp.Num(); ++i)
     {
         temp->Destroy();
     }
 
     Super::BeginDestroy();
 }

Any ideas?

Thanks!

Product Version: UE 4.10
Tags:
more ▼

asked Aug 03 '16 at 07:49 PM in C++ Programming

avatar image

Ryan Darcey
836 47 43 70

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

2 answers: sort voted first

For the record, the reason your code is incorrect is that BeginDestroy is called after the actor is removed from gameplay. According the Actor Lifecycle, the logic belongs in EndPlay instead:

BeginDestroy - This is the object's chance to free up memory and handle other multithreaded resources (ie: graphics thread proxy objects). Most gameplay functionality related to being destroyed should have been handled earlier, in EndPlay.

This sounds like a good place to use ChildActorComponent, whose actor will be destroyed when the parent is. Even better, do the target points need to be Actors at all? If instead you convert them to some type of Component, it will probably result in a cleaner design.

more ▼

answered Aug 04 '16 at 04:55 AM

avatar image

Digi Labs Dan
566 16 12 34

avatar image Ryan Darcey Aug 04 '16 at 05:03 AM

Interesting...haven't come across that component type yet. I'm manipulating the patrol points directly in the editor when creating the path, so they'd need to be actors in that case, yeah?

avatar image Digi Labs Dan Aug 04 '16 at 05:09 AM

No, you can add instance components to a single actor and move them around, without editing that Actor's Blueprint. See the "Add Component" button in the Details Panel.

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

I think components might be an option though just looking over your current code I think the initial problem is you're trying to call destroy on the TArray multiple times instead of the contents of the array below I think is more accurate? Also should it be an "AActor" class not just "Actor" though i'd guess that should error if you typed it wrong?

I'm a bit of a newbie so i might just not understand a feature of C++\unreal if what you had should have worked...

 void APatrolPath::BeginDestroy()
  {
      TArray<Actor*> temp;
      GetAttachedActors(temp);
  
      // auto iterate though all the actors you already have in the TArray
      for (Actor* CActor : temp)
      {
          CActor->Destroy();
      }
  
      Super::BeginDestroy();
  }
more ▼

answered Aug 04 '16 at 06:22 AM

avatar image

Fishy418
48 6 6 8

avatar image Shohei Aug 04 '16 at 07:58 AM

This answer is correct. You forgot the square bracket operator. Temp[i]->Destroy();

avatar image Ryan Darcey Aug 04 '16 at 03:59 PM

Sorry...I actually had the line Temp[i]->Destroy(). This was a hasty post where I had deleted my old code, then quickly rewritten for this post. The main problem was that at the time this function is called, the "temp" array is empty. It seems children are detached by the time BeginDestroy is called.

avatar image Fishy418 Aug 04 '16 at 08:18 AM

ah of course I knew something wouldn't of worked with the original code adding the array index [i] should fix it too. As long as the Actors are attached correctly and the temp array isn't actually empty like you think it is.

I've been doing some pathfinding code myself and find having a parent actor like you have then just have an arrary of USTRUCT data containing the fields for each waypoint you have, I think actor or even component for each waypoint is probably overkill for that sort of info. From my understanding USTRUCTs should also clear themselves from memory when the APatrolPath is destroyed as long as you empty the array storing them in the destroy function.

(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