First Method
I can instantiate MyStaticMeshComponent in C++ as follows.
UCLASS()
class NOTES_API AMyActor : public AActor
{
GENERATED_BODY()
// Others are removed for the sake of simplicity.
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = MyActorSettings)
UStaticMeshComponent* MyStaticMeshComponent;
};
and
AMyActor::AMyActor()
{
// Others are removed for the sake of simplicity.
MyStaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("MyStaticMeshComponent");
static ConstructorHelpers::FObjectFinder<UStaticMesh> MyAsset(TEXT("StaticMesh'/Game/StarterContent/Props/SM_Rock.SM_Rock'"));
if (MyAsset.Succeeded())
{
MyStaticMeshComponent->SetStaticMesh(MyAsset.Object);
}
}
The actor above can be instantiated on the level just by dragging it onto the level without having to create its blueprint first.
Second Method
- Remove the
MyStaticMeshComponentmember variable as well as its instantiation statements. - Create a blueprint for
AMyActor. LetBP_MyActorbe the blueprint name. - Add
StaticMeshcomponent by pressingAdd Componentbutton provided in the Blueprint Editor. And assignSM_ROCKto theStatic Meshproperty shown in theDetailspanel. - Done.
Third Method
For the sake of completeness, I want to understand whether it is possible to initialize MyStaticMeshComponent member variable but from Blueprint graph.
Could you explain how to do so?
