Using child actor component in c++ gives a few problems

Hello, i needed to use child actor component in an actor and i’ve noticed some potential problems with it.

The first one is that if i do this:

ChildActor = CreateDefaultSubobject<UChildActorComponent>("ChildActor");
ChildActor->SetupAttachment(Mesh);
ChildActor->SetChildActorClass(AWhateverActor::StaticClass());
ChildActor->CreateChildActor();

I get this breakpoint:

The second problem is that in the class i have the child actor component i also have a Static Mesh Component name Mesh (let’s call it Original Mesh and it’s the parent of the child actor component). If i set the child actor class to an actor class that also has a static mesh component named Mesh (let’s call it Child Actor Mesh) and on the template options i set the Static Mesh property of the Child Actor Mesh to whatever value and compile, the Original Mesh gets broken and stops having details information and visual representation.

Hey -

I tested your issue in 4.14 and 4.15 by copying the code you provided into an actor class and did not run into any break point locally. There were some issue with Child Actor Components that were addressed in 4.15. Can you let me know you’re still seeing these issues in the latest version? If so, please include detailed reproduction steps to help me reproduce the issue on my end.

Right now i can’t update to 4.15 because i’m waiting for substance and another custom plugin to update versions.

Please let us know when you are able to provide further information or reproduction steps. Without additional information I am unable to continue investigating this issue and am marking it as resolved for the time being. If you do come across this issue in 4.15, please feel free to comment here to reopen the post for further investigation.

I’m using 4.15.2 and I’m having a similar issue.

Hey kcmonkey-

Can you provide details about the behavior you’re seeing? Are you using Child Actor Components in code? If so, please provide the code being used.

In 4.19 it’s the same.

This is also true for the editor, when setting the name, you are given an error when trying to give a component an existing name.

The reason for this, as I see, is that any child component has to have a unique name in order to use it’s functionality like “GetComponentByName”.
Of course this is just a getter which could return an array of all components with the same name, so the core reason could be a much more serious one.

Reproduce in 19.2

In 19.2, you can reproduce the first issue by meeting these conditions (1) calling CreateChildActor() in the constructor. In such a case, the following happens:

  1. CreateChildActor(), of course, calls "CreateChildActor()" (in ChildActorComponent.cpp)
  2. At line 561 of ChildActorComponent.cpp, if the child actor's class has been successfully spawned, the code will call the following:(
  3.  ChildActor->AttachToComponent(this, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
    
  4. In Actor.cpp at line 1508, this in turn calls: RootComponent->AttachToComponent(Parent, AttachmentRules, SocketName).
  5. Now since the RootComponent is a sceneComponent and this method is not virtual, then "USceneComponent::AttachToComponent" will be called.
  6. The problem:

    Now since the above ChildActorComponent code unconditionally called AttachToComponent with the attachment rule: “SnapToTargetNotIncludingScale”, the second ensureMsgf in the screenshot will fail since it is “ensuring” that the attachment rules are keepRelative. . (SceneComponent.cpp at line 1678). Specifically, this fails:

    ensureMsgf(AttachmentRules.LocationRule == EAttachmentRule::KeepRelative && AttachmentRules.RotationRule == EAttachmentRule::KeepRelative && AttachmentRules.ScaleRule == EAttachmentRule::KeepRelative, TEXT("AttachToComponent when called from a constructor is only setting up attachment and will always be treated as KeepRelative. Consider calling SetupAttachment directly instead."));
    

Let me know if I missed anything or need further caveats to reproduce.