Actor Hierarchy

I have a certain number of actors and I would like them to move together.
Looking around I found that it is possible to so using the AttachToActor.
However, when the parent Actor is destroyed, the attached objects keeps on living.
I know that UChildActorComponent is another option but I find a bit unconvinient since every time you have to call a method on the actor an explicit cast is needed.
Which is the best way to create a hierarchy of Actors so that children are destroyed together with their parent?

Actor have lot of delegates, events you can listen from most objects and functions as well as other delegates (you can bind delegate to delegate). One of them is OnDestroyed which is called when actor is about to destroyed, child actor can bind to parent’s delegate and destroy it self when it’s being called.

You can read about delegates here:

In blueprints Delegates are called Event Dispatchers, so if you used them you should be familiar with them. In fact you can turn delegate to blueprint usable event dispacher by using BlueprintAssignable in UPROPERTY of delegate variable

Thank you very much! Delegates seem to work fine except for the fact that ‘Destroy’ is not a UFUNCTION so I ended up using K2_Destroy. That being said, I’m still wondering whether this is the right way to create a hierarchy of actors: the fact that they don’t get automatically destroyed makes me think that maybe there is a better way of doing this? Do you have any suggestions?

I don’t know ue4 source code, but it seems to me that you have to either store references to the sub objects in the parent, or register delegates for each sub object and then on parent destroy call an event dispatcher like shadow river is saying.

Maybe there are other ways too. The answers to this question will be the same as “How does the parent object know which other objects belong to it so it can destroy them when its destructor is called?”

Thats what Child Actor Component is for, to make actor part of other actor and auto destroy it, without it Epic can’t assume if you intent to destroy attached actor, maybe you don’t, that why you need to destroy yourself. You don’t need to be worried as engine would so exact same code, so it really makes much difference.

Also you can use AddRaw (or BindRaw if non-multicast) to bind non UFUNCTION function

Thank you both for your answers. I guess UChildActorComponent is the way to go in this case since they handle the child actor destruction automatically which is pretty convenient. I tried delegates too but having to remember to bind a function for every new spawned actor can be a bit cumbersome.