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
MyStaticMeshComponent
member variable as well as its instantiation statements. - Create a blueprint for
AMyActor
. LetBP_MyActor
be the blueprint name. - Add
StaticMesh
component by pressingAdd Component
button provided in the Blueprint Editor. And assignSM_ROCK
to theStatic Mesh
property shown in theDetails
panel. - 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?