Spawning Built-in Cube in C++

I know versions of this question have been asked before, but either they do not cover my use case or (the more probable case) they do but I just do not understand UE4 enough to see what is happening. I have an actor class that I want to use to spawn in a number of cubes as the level loads (before the gameplay starts). These cubes will be used as obstacles, so I just need to spawn them in particular locations and leave them; no custom functionality is needed. I see that UE4’s editor has a built-in cube mesh under “Basic”, which looks perfect for my needs.

Is it possible to get at this built-in cube and spawn a number of them? I know of UWorld::SpawnActor, but that requires a class that I do not know (as far as I can tell, ACube is not a thing).

I was able to accomplish this by using the following code inside the constructor of an Actor class (i.e. ACube):

// Create a static mesh component
UStaticMeshComponent* cubeMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Cube"));

// Load the Cube mesh
UStaticMesh* cubeMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")).Object;

// Set the component's mesh
cubeMeshComponent->SetStaticMesh(cubeMesh);

// Set as root component
RootComponent = cubeMeshComponent;

Once you have the mesh component, you can use SetWorldScale3D to reshape it if you want something other than a perfect cube.

1 Like