[> Please see my related forum post][1]
How do I properly setup parent-child relationships between two AActors in C++? Or in other words: How do I setup an Actor A to be the child of an Actor B?
An in-depth explanation of this relation is appreciated.
Background
- I want to auto-configure/layout a large set of actors which are in principle independent but there positions. Manual placement here is too much work and also this is intended to be part of a procedural level setup.
- I want to re-arrange the whole actor set if certain aspects of one child actor change from within the UE4-Editor, such as position-changes.
I tried two major approaches:
Variant A
I use “AActor::AttachToActor()” which works to some extent. The NewCustomActor is attached to the current (this) AActor, which is also visible in the UE4-Editor-Outliner. However:
- If I delete the parent AActor (this) within the editor, all child ACustomActors remain without its parent.
- If I change an aspect in any of the child actors, this event is not propageted back to the parent actor and therefore AActor::OnConstruction() is not called, which I require. I do not want to implement a manual event propagation here, as CustomActor must also work independently of a parent-child relationship.
ACustomActor* NewACustomActor = GetWorld()->SpawnActor(ACustomActor::StaticClass(), SpawnParams); NewACustomActor->SetActorLocation(Position); NewACustomActor->SetActorLabel(Name); NewACustomActor->AttachToActor(this, FAttachmentTransformRules::KeepWorldTransform);
Variant B
I use UChildActorComponent on the parent actor. This also works to some extent. However:
-
I could not find a solution for dynamicaly spawning UChildActorComponents as the number of objects will be only known at runtime. Using a TArray does not work as UE4-Editor then just treats it as an array and spawning / selection /editing of the child actors is not possible.
-
I cannot select any child actor from the UE4-Editor-Viewport. I always have to select the parent actor, go to its details panel, scroll down and manually select a child actor from thre. This is not practical for me, as I have hundreds of child actors and everyone of them might require manual tweaking/configuration.
-
This approach seems to be a bit of a crutch and not really intended for my purpose as it introduces an layer of indirectness which adds complexity?
TEST = CreateDefaultSubobject(TEXT("TestX1")); TEST->SetupAttachment(this->root); TEST->SetChildActorClass(NewACustomActor::StaticClass()); TEST->CreateChildActor();