How to instantiate StaticMeshComponent in Blueprint nodes?

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. Let BP_MyActor be the blueprint name.
  • Add StaticMesh component by pressing Add Component button provided in the Blueprint Editor. And assign SM_ROCK to the Static Mesh property shown in the Details 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?

Solution

You instantiate any component blueprint using “Add Component” node, each component class have it’s own node.

Thank you very much. Problem solved.