C++ ConstructionScript Created Components are not visible in editor

Hello.

I am trying to implement ConstructionScript on C++, everything is working ok except: The procedurally added stuff are not visible in the components of the pawn. Here is the code I use on OnConstruction:

Shelve = ConstructObject<UStaticMeshComponent>(UStaticMeshComponent::StaticClass(), this, FName(TEXT("ShelveMesh")));
Shelve->CreationMethod = EComponentCreationMethod::UserConstructionScript;
Shelve->AttachTo(GetRootComponent());
Shelve->SetRelativeRotation(FRotator(0, 90, 0));
Shelve->SetStaticMesh(ShelveMesh);
Shelve->RegisterComponent();

this runs perfectly and does everything I ask for it to do but as I said, when I click on the pawn, I cannot see this component on the components tab. Creating it on constructor attaches it okay and makes it visible.

On top I am wondering why I just cannot create this object on the construction script then just call Shelve->SetStaticMesh(ShelveMesh); This results in access violation error.

Edit: Reordering stuff did not yield any good results (RegisterComponent especially)

Up, had no replies for so long:(

I assume you have declared a UPROPERTY for Shelve in the header file and set its properties to be blueprint-editable?

That’s not how you should create component subobjects natively. The ConstructionScript (Simple or User) is a complex construction step really meant to integrate blueprint-created components in as transparent a fashion as possible. When working with C++, you don’t need to do any of this. Use the constructor instead:


AMyActor::AMyActor()
{
	Shelve = CreateDefaultSubobject<UStaticMeshComponent>( "ShelveMesh" );
	Shelve->AttachParent = GetRootComponent();
	Shelve->RelativeRotation = FRotator(0, 90, 0));	
	Shelve->SetStaticMesh(ShelveMesh);
}

Then, as trojanfoe mentions, make sure this Shelve mesh is identified as a UPROPERTY and make sure it is VisibleAnywhere:


	UPROPERTY( Category=MyActor, VisibleAnywhere, BlueprintReadOnly )
	UStaticMeshComponent* Shelve;

Slightly OT but… welcome back cmartel :slight_smile:

There’s also “NewObject” method in case where component must be created in some place other than Constructor. In that case “RegisterComponentWithWorld” call will be also necessary.