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!

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?

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.

1 Like

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.

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();
 }

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.

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

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.