Spawn blueprint from C++

I would like to spawn just a simple, solid cube at a set location. I have tried many examples and have been through all of the questions/answers, but am still unable to do it.

Can someone please provide me with a full working example?

I have created a blueprint via the editor and called it ‘Cube-Blueprint’. I know I can get the reference to the blueprint and have tried to use it… unsuccessfully.

I have also tried without using the blueprint… and here it is:

Here is my actor .h

#pragma once

#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"

UCLASS()
class EARTHQUAKES_API AFloatingActor : public AActor
{
	GENERATED_BODY()
	
public:	

	// Sets default values for this component's properties
	AFloatingActor(const class FObjectInitializer& PCIP);

};

Here is my actor .cpp

#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor(const class FObjectInitializer& PCIP)
	
{
	SetActorHiddenInGame(false);
}

In another class, within the BeginPlay(), I do this:

void AMapWall::BeginPlay()
{
	Super::BeginPlay();

	
	UWorld* const World = GetWorld();
	if (World) {
		for (int i = 0; i < 5; i++)
		{

			UStaticMesh* newComp = ConstructObject<UStaticMesh>(UStaticMesh::StaticClass(), this, FName("A_BOX"));
		
			FVector Location = FVector(-746.0, 30, 50 * i);
			AActor* actor = World->SpawnActor(AFloatingActor::StaticClass(), &Location);
			actor->AddComponent(FName("A_BOX"), false, FTransform(), NULL);
		}

	}

}

I get my 5 actors created and visible in the “World Outliner” window, but they are not visible in the game and they do not have an mesh or anything else…

What am I doing wrong? Can I just instantiate the blueprint “Cube_Blueprint” that I created with the editor?

Hello rich2020,

I already had this put together from setting it up for another user but it seems like it’ll be useful here as well.

http:///

This contains 2 classes, MySpawner and MyActor. When it comes to spawning a blueprint, with the current setup, MySpawner is set to spawn anything that is based off the AMyActor class (line 84/85). When you make a blueprint based off the MySpawner class and drag an instance into the level, you can select what it should spawn in the Details panel.

Hope this helps!

Thank you.

lines 84/85 and 112 are eyes opener… thank your very much