Component created outside of constructor doesn't show in hierarchy

I am creating some StaticMeshComponents, if my character has a UPROPERTY bool set to true.

Since values set in the editor aren’t available yet in the c++ constructor, I can’t use CreateDefaultSubobject.

So, I am creating the components later on (I’ve tried both OnConstruction and PostInitializeComponents) with NewObject.

However, I can’t figure out how to get components created with NewObject to show up in the blueprint component hierarchy.

210614-hierarchy.png

I have tried a lot of different variations and haven’t been successful with getting a UStaticMeshComponent created via NewObject to show up there.

void AFPSCharacter::OnConstruction(const FTransform& Transform)
{
    Super::OnConstruction(Transform);

    if (bRenderClipCharges)
    {
        for (int32 i = 0; i < NumClipChargeMeshes; i++)
        {
            UStaticMeshComponent* ChargeMesh = NewObject<UStaticMeshComponent>(this, FName(*("ChargeMesh_" + FString::FromInt(i))));
            if (ClipChargeAttachSockets.IsValidIndex(i))
            {
                ChargeMesh->SetupAttachment(GetMesh(), ClipChargeAttachSockets[i]);
            }
            AddOwnedComponent(ChargeMesh);
            ChargeMesh->RegisterComponent();
            ClipChargeMeshes.Add(ChargeMesh);
        }
    }
}

I also tried changing the CreationMethod on the component to every different enum in EComponentCreationMethod and that didn’t help.

This seems like it should be simple and something that a lot of people would want…

Does anyone know how to get it to show up in the hierarchy?

Isn’t OnConstruction the equivalent to the BP ConstructionScript?
Let me know if you have any links to editing the CDO, I haven’t found much yet.

What your looking for is to edit the Class Default Object. And to be perfectly honest it’s anything but simple there is a few references to how to do it. And I’d recommend looking then up. You can get it to work but it’s quirky in editor at best. In game it seems to work just like you’d expect.

But in editor you have to manually work with the construction script to make things works like what your describing. The construction script is where the objects are stored to be created by the system during construction. We have weapon hardpoints on our characters that each child blueprint can edit to hold a default set of weapons. To handle this in editor we edotntje construction script to add the weapons dynamically. When a weapon is added, we have to manually call for a script update or they don’t show up until you close the blueprint and reopen it.

It’s worth it once you get it working but it is an area that needs a lot of improvement or at least better documentation about CDO and ConstructionScripts for C++.

No. The construction script in c++ is an tree attached to the CDO for that class. It holds descriptions of the subobjects to be created during construction. They are using the term in two different ways. In c++ it’s the tree object. On blueprints it’s working during the actual construction.

In the end we took apart the bp editor and traced everything back to the CDO in code. I’m not at my system or I’d send you the code to actually add a dynamic component.

https://answers.unrealengine.com/questions/216910/calling-construction-script-through-c.html

Another method that was far more tolerant for us in the end was to create a custom details panel that listed the added weapons and provided the interface for the weapon placement without them showing in the component panel at all. It was much easier to put in place and still allowed us to use OnConstruction like normal expectations by looping through the stored data and creating the components when the script ran. But they don’t show in the components panel and we didn’t use transform widget which may be a problem for you.

I know this is an old reply, but if you are still around, is the custom details panel code something you can share? Or at least a snippet? I haven’t tried such a “heavy” customization before but this already sounds exactly like what I could use. In our system we create lots of components dynamically and attach them to various actors. It is frustrating not being able to see or interact with these components in the editor details.