UChildActorComponent problem in Construction Script

Hello! I’m trying to add a child actor component in my actor’s contruction script. I could easily do this in blueprints but simply cannot get the same exact behaviour in C++. I want the child actor to be attached to the parent object, and be cleared every time the construction script is run (like it does in BP’s).

Currently, every time the construction script is run it creates a new Room in the root hierarchy of the World Outliner (although it appears attached to the actor since I can’t select them individually). Previously in blueprints, the room would appear nested under the parent actor in the World Outliner and the child actor components are recreated, instead of remaining in the world as a new one is created.

Basically, previously in blueprints when the construction script was executed;

  • Child actor component is recreated
  • Child actor component appears nested under the parent object in the World Outliner

The C++ version with my current code;

  • A new child actor component is created every time, resulting in for example 10 child components if the construction script is ran 10 times.
  • Child actor components appear in the root hierarchy of the World Outliner, although I cannot select or move them

Do I need to manually delete all the child components in C++? This is my current code, any help would be greatly appreciated!

void ARandomLevelGenerator::OnConstruction(const FTransform& Transform)
{
        Super::OnConstruction(Transform);
        
        UChildActorComponent* newRoom = NULL;
        newRoom = NewObject<UChildActorComponent>(this);
        newRoom->SetChildActorClass(ARandomLevelGenerator_Room_Base::StaticClass());
        newRoom->RegisterComponent();
        newRoom->AttachToComponent(this->RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, true));
}

If you use the line:

newRoom->bCreatedByConstructionScript = true;

It should behave like in blueprints.

Don’t have access to UE4 at the moment, but I think I tried this but the property has a _DEPRECATED suffix on it, so I assumed I should not use it? As in, the property is now called;

bCreatedByConstructionScript_DEPRECATED

Might be, I haven’t tried it myself.

If there is no updated equivalent, then consider having the child component as an actual member of your class. Right now, the pointer only exists inside the construction script. If you have the pointer as a class variable, you can perform a null check before creating the object. The construction script gets run pretty frequently, and without destroying older created objects it is no wonder that they will accumulate.
Either delete those newly created actors or find the updated equivalent of the bCreatedByConstructionScript variable.

If you are interested, you can still try to use the deprecated variable and see if it behaves correctly. I do not know why your object is not parented to the actual parent though. It might be because you are registering the component before attaching it?

In my actual code, the Child Component IS a member class variable, but I simplified it for this post. Unfortunately it doesn’t change anything. I should mention that I can get around the multiple copies by manually deleting all children of the parent actor before running the construction script, but I hoped that the engine would automatically do this for me as it does for blueprints.

I’ve tried putting RegisterComponent() before, after, and in between all those other lines of code and it doesn’t make a different.

I will try bCreatedByConstructionScript and see if it fixes the issue, although I’m not comfortable using it even if it works if it’s deprecated.

Cheers!