C++ Dynamically create multiple instances of a specific static mesh

Hi all…

I have a UStaticMesh in my content drawer that I want to dynamically create at runtime in a loop. For simplicity, lets say that the UStaticMesh is named SM_MyMesh;

In a for loop, I need to create a new instance of SM_MyMesh;

Maybe I’m just burned out from the day, but doing this seems to be escaping me. Seems like all the examples I am finding are show examples of loading it up in the constructor and attaching it to the root… But that is not what I need. I just need to create multiple instances of it and specify it’s location with an FVector.

Any help would be greatly appreciated.

	#include "Engine/StaticMeshActor.h"
	// ...
	check(GetWorld());
	UWorld* World = GetWorld();

	// Path to Static Mesh.
	FString SM_FilePath = TEXT("/Engine/VREditor/BasicMeshes/SM_Cube_01.SM_Cube_01");

	for(size_t i = 0; i < 4; ++i)
	{
		/* Static Mesh Actor */
		// Transform
		FTransform SpawnTransform = GetActorTransform();
		SpawnTransform.SetLocation(SpawnTransform.GetLocation() + FVector(150.0f * i, 0.f, 0.f));
		// Spawn parameters
		FActorSpawnParameters SpawnParams;
		// Spawn Static Mesh Actor
		TObjectPtr<AStaticMeshActor> SpawnedStaticMeshActor = World->SpawnActor<AStaticMeshActor>( /*Class*/ AStaticMeshActor::StaticClass(), /*FTransform*/ SpawnTransform, /*FActorSpawnParameters*/ SpawnParams);
		// Check for nullptr.
		checkf(SpawnedStaticMeshActor, TEXT("Static Mesh Actor failed to spawn."));
			
		/* Static Mesh */
		// Load Static Mesh.
		TObjectPtr<UStaticMesh> SpawnedMesh = LoadObject<UStaticMesh>(World, *SM_FilePath);
		// Check for nullptr.
		checkf(SpawnedMesh, TEXT("Static Mesh failed to load."));
		// Set Static Mesh.
		SpawnedStaticMeshActor->GetComponentByClass<UStaticMeshComponent>()->SetStaticMesh(SpawnedMesh);
	}