Cross posted with answers here, here, here, here
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.
Goals
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:
Variant B
please also see attached example code files - myactor.[h|cpp] and mychildactor.[h|cpp]
I use UChildActorComponent on the parent actor. This also works to some extent. However:
Variant B code snippet
Variant B screenshot
The screenshot below shows a MyActor which spawns a MyChildActor during runtime. However, selection of MyChildActor is not possible in the Outliner or in the Viewport. Properties are also not editable.
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.
Goals
- 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.
- This should work procedurally (at runtime via c++) and from within the UE4-Editor.
- All child actors should be grouped below the parent actor within the world outliner, to denote that this is a logical group.
- Each child actor must still be working as an independent actor (Tick() for example).
- If a child actor gets changed (deleted for example) this event must be propagated to the parent actor, so it may react to this change.
- All child actors should move accordingly if the parent actor is moved. But not vice versa.
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 present, without their parents
- 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.
Code:
ACustomActor* NewACustomActor = GetWorld()->SpawnActor(ACustomActor::StaticClass(), SpawnParams); NewACustomActor->SetActorLocation(Position); NewACustomActor->SetActorLabel(Name); //NewACustomActor->SetOwner(this); NewACustomActor->AttachToActor(this, FAttachmentTransformRules::KeepWorldTransform);
please also see attached example code files - myactor.[h|cpp] and mychildactor.[h|cpp]
I use UChildActorComponent on the parent actor. This also works to some extent. However:
- I could not find a solution to dynamically spawn UChildActorComponents while also beeing able to edit the spawned Actors/UChildActorComponents within the UE4-Editor. A spawned Actor/UChildActorComponent is always labeled as "Native components when declared as a UPROPERTY in c++". However, UPROPERTY() is normally set statically on class attributes, which do not exist at runtime.
- Furthermore I even cannot select any child actor from the UE4-Editor-Viewport or World-Outliner.
- Also, this approach seems to be a bit of a crutch as it introduces an layer of indirection which increases code complexity?
Variant B code snippet
Code:
UChildActorComponent* NewComp1 = NewObject<UChildActorComponent>(this); NewComp1->bEditableWhenInherited = true; NewComp1->RegisterComponent(); NewComp1->SetChildActorClass(AMyChildActor::StaticClass()); NewComp1->CreateChildActor();
The screenshot below shows a MyActor which spawns a MyChildActor during runtime. However, selection of MyChildActor is not possible in the Outliner or in the Viewport. Properties are also not editable.
Comment