Dynamic Mesh Spawning in UE4

In the eternal struggle to accumulate reputation on StackOverflow, I noticed a question that seemed easily solvable (by the standards of SO at least).

The link to the question:


The question asker was requesting for a concrete example of how to create a static mesh/ quad (as he puts it) at runtime.
It is also required that this quad be rendered with a texture on it.

Textures in Unreal 4 are implemented through the use of Materials (which allow for the composition of multiple textures into a single texture when applied to an object, and are a really handy tool)

When I first saw this question, it seemed quite cut and dry. Unreal Blueprints make it simple to achieve a lot of tasks without resorting to actual coding, and I made a suggestion towards the same.

However the asker said that he had yet to see a single actual example in code that allowed one to do this either in the documentation or on the Unreal 4 AnswerHub and after some Googling I found that this was indeed true.

This pretty much hooked me right in. I had to figure it out, and I felt bad for giving the asker the same vague high-level answer that he had found everywhere else. I resolved to have something in the way of an actual code example to help him on his way.

2 hours later, this was the final result:

A quad created an runtime with a texture on it.


I set it up so that an instance of my class ‘DynamicMeshSpawner’ would be spawned everytime I hit ‘P’ on the keyboard. When the instance of this class is created, it calls the constructor, which spawns the cube with the material applied. I did the class instance spawning stuff in Blueprints using the SpawnActor node.


     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;

    // Using a SphereComponent is not particularly necessary or relevant, but the cube refused to spawn without a root component to attach to, or so I surmise. Yay Unreal. =/
    USphereComponent* CubeComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = CubeComponent;

    // Create and position a mesh component so we can see where our cube is
    UStaticMeshComponent* CubeVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
    if (SphereVisualAsset.Succeeded())
        CubeVisual->SetRelativeLocation(FVector(-200.0f, 0.0f, 100.0f));
    // Create a material to be applied on the StaticMeshComponent
    static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/StarterContent/Materials/M_Tech_Hex_Tile_Pulse.M_Tech_Hex_Tile_Pulse'"));

    if (Material.Object != NULL)
        TheMaterial = (UMaterial*)Material.Object;

    CubeVisual->SetMaterial(0, TheMaterial);

This was the headerfile:

class MYPROJECT_API ADynamicMeshSpawner : public AActor

    // Sets default values for this actor's properties

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;
    // Pointer to the material that needs to be used
    UMaterial* TheMaterial;

Thats pretty much it!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s