There are very few information or documentation about how to create and attach static mesh components in construction scripts using C++. I was able to make almost everything work, but, since the mobility of the mesh is set to “static”, every time the editor is restarted, the light build is lost. However, when creating static meshes in construction scripts using Blueprint, it works fine and the light build issue does not happen.
This is the code that I’m using to create the static mesh, given a “Mesh” asset. This code is running inside the OnConstruction function:
UStaticMeshComponent* MeshComponent = NewObject<UStaticMeshComponent>(this);
MeshComponent->SetStaticMesh(Mesh);
MeshComponent->SetMobility(EComponentMobility::Static);
MeshComponent->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
MeshComponent->RegisterComponent();
MeshComponent->CreationMethod = EComponentCreationMethod::UserConstructionScript;
Meshes.Add(MeshComponent); // Meshes is a UPROPERTY declared in the .h file
Is something missing?