Unreal Engine 4.25.4 - CreateDefaultSubobject inside a child component attached to an actor loses values in blueprint editor

I have the following actor:

UCLASS()
class X_API AMoveableRigidBodyActor : public AActor
{
	GENERATED_BODY()
	
public:	

	// Sets default values for this actor's properties
	AMoveableRigidBodyActor();

public:	

	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Custom")
		USceneComponent* SceneComponent;

	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Custom")
		UMoveableRigidBodyComponent* MoveableRigidBodyComponent;

};

This Actor makes a reference to a UMoveableRigidBodyComponent and a USceneComponent which I run CreateDefaultSubobject on and attach in the following fashion in the constructor:

AMoveableRigidBodyActor::AMoveableRigidBodyActor()
{

	SceneComponent = CreateDefaultSubobject<USceneComponent>(FName("AMoveableRigidBodyActor_SceneComponent"));
	SetRootComponent(SceneComponent);

	MoveableRigidBodyComponent = CreateDefaultSubobject<UMoveableRigidBodyComponent>(FName("AMoveableRigidBodyActor_MoveableRigidBodyComponent"));
	MoveableRigidBodyComponent->SetupAttachment(SceneComponent);

}

So far, so good.

Now inside my UMoveableRigidBodyComponent I instantiate a few components that are relevant to that component. I expect these components to be visible and manageable from within my blueprint:

UCLASS()
class X_API UMoveableRigidBodyComponent : public USceneComponent
{
	GENERATED_BODY()

public:	

	// Sets default values for this component's properties
	UMoveableRigidBodyComponent();

override;

	UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Custom")
		USceneComponent* SceneComponent;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Custom")
		UStaticMeshComponent* StaticMeshComponent;
		
};

Here I use CreateDefaultSuboject to attach everything to this component:

UMoveableRigidBodyComponent::UMoveableRigidBodyComponent()
{    
	SceneComponent = CreateDefaultSubobject<USceneComponent>(FName("UMoveableRigidBodyComponent_SceneComponent"));
	SceneComponent->SetupAttachment(this);

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(FName("UMoveableRigidBodyComponent_StaticMeshComponent"));
	StaticMeshComponent->SetupAttachment(SceneComponent);
}

So, all of this feels pretty normal so far. So I go into my editor, create a blueprint that’s based off of the AMoveableRigidBodyActor and I assign a static mesh to my component:

Still good. But then I hit the compile button:

The selected static mesh has now vanished from the editor but still seems to be filling the value correctly.

Furthermore, if I put this into the game world you can see that the static mesh sure enough isn’t there in the instance:

What am I misunderstanding here?

Anything I can do to make this question clearer? Thanks in advance. It has gotten me quite stuck on how to proceed.

Any thoughts? I still have this problem.

bump. Any insights would be super appreciated.

I’ve never seen this kind of issue, I see two things you could check :

  • StaticMeshComponent is declared as BlueprintReadWrite, this should be BlueprintReadonly as you create it in your C++ code (note that BlueprintReadonly will let you modify StaticMeshComponent’s properties in blueprint)
    maybe there is a blueprint constructor or something else in blueprint that somehow erase the static mesh pointer ?
  • I guess ACube is a standard mesh provided by the engine, but maybe it is not accessible somehow at runtime ?

I’m having an odd issue as well attempting something very similar to what you are doing.
Attempting to create sub components and using the C++ CustomComponent directly on a BP Actor attaches the Component and the new ones correctly to the Actors root, but creating a BP derived from CustomComponent and using that BP Custom Component and the new Component roots are at origin and won’t move from there.

Not to hijack but talking to the community and research all points to a component making new components can only reliably do so at runtime. I’m stubbornly still trying to see if I can get it to be consistent though. Subbing to this because I think the answer here might give me insight to my issue.
Exact same engine version.

If it helps, if you take a look at any component inside unreal engine’s code except from a uscenecomponent itself… none of them actually initialise a uscenecomponent inside themselves. On the other hand, the uscenecomponent itself does some initialisation in postregister method and then seems to use special serialisation code to get it to save correctly - I admit, I have given up on this in the end because of the lack of examples. I’ve moved now to everything being registered inside the owning actor itself’s constructor. When you do this, you don’t have the same problems. I had to change 86 classes and redo 7 levels because of it because I said “this problem is for future James” lol. Sigh. The downsides of this way of working is that I have quite a bit of repeated initialisation code in my constructors although creating components does allow you to atleast have no-repeated logic. big mood.

1 Like

I am stuck on the same issue as well :confused:
It seems building complex nested hierarchies of Components inside of other Components - is not possible?
How to deal with building such structures then? Always put everything directly to the Actor? Seems wonky…

When you compile, the instance shown in the BP viewport and in the level is respawned to refresh the instance.
A solution to this is to have a variable for the static mesh asset and in the constructor, after spawning the default static mesh component, set the static mesh asset. That way, when compiling, your static mesh component will always be generated with the static mesh asset you provided it.

Though, I don’t know what are the possible drawbacks in memory, so you might wanna have a soft object reference to the asset, so when loading the class data in memory, it shouldn’t load the asset as well.