How destroy actor works?

Hi all I have a question I am learning unreal engine for one year today I stuck in a problem I want to know how unreal made this I have two classes one is A that is inherit from Actor and other is B that is inherit from A when I call Destroy Actor in class A unreal also destroy child class B I want know how can we make such a function in parent class that will destroy all its object starting from child class like in above problem destroy actor function is made in actor class?

Hi MaroonBerats,

Feel free to re-phrase if I’m missing what you’re asking, but focusing in on the idea of an actor destroying all of its children then destroying itself. (Sounds morbid when taken out of context :grin::hocho: :child:)

I just wired this up, and I think it shows the behaviour you’re after. (All children destroyed instantly, then 5 seconds later parent class destroyed, this code is in the parent class (A))

Hope that helps, welcome to The Forums!

1 Like

Do you mean?

:slight_smile:

2 Likes

After re-reading the question a few times I guess I was parsing “Destroy all its object starting from child class” As in ultimately everything will be destroyed, but the destruction will start with the children and then end with parents.

1 Like

Hi @MaroonBerats !

I’m having trouble understanding your question, but I think you are asking about the destruction of actors.

In Unreal an Actor lifecycle is described right here

As seen above, there are plenty of ways for an Actor to be destroyed, after it is signaled to be destroyed, it begins the EndPlay phase, then the actor is marked as pending kill, then it’s removed from the array of all actors (i.e. GetAllActors), then the garbage collector is going to remove all references to that actor, and finally BeginDestroy, IsReadyForFinishDestroy and FinishDestroy.

In Unreal an Actor only destroys itself and its components (NOT their child actors, and neither do the actors that inherit from the same class). As for your question, I think you are trying to destroy first a B actor, then an A actor, and in C++.

For that there are plenty of ways, but the easiest would be to override the OnBeginDestroy on B and then inside it

B.h

virtual void BeginDestroy() override;

B.cpp

void AB::BeginDestroy()
{
	A->Destroy(); //assuming you have a reference to A on B

	Super::BeginDestroy();
}

This will begin destroying A after destroying B.