How to use ConstructorHelpers::FObjectFinder and SpawnActor to spawn a blueprint

Well that was all rather painful. I could not see a way to keep the resources addressable by name in the pak files, but I did eventually get a spawning mechanism working:

Create a new cpp class that inherits from actor, I called mine Spawner (don’t forget to replace SPAWNBP_2_API with your project).

header:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Spawner.generated.h"

UCLASS()
class SPAWNBP_2_API ASpawner : public AActor
{
	GENERATED_BODY()
	
	UPROPERTY(EditAnywhere, Category = "Spawn Object")
	TSubclassOf<AActor> SpawnBP;

public:	
	ASpawner();

protected:
	virtual void BeginPlay() override;

private:
	void SpawnNow();
};

cpp

#include "Spawner.h"

ASpawner::ASpawner()
{
	PrimaryActorTick.bCanEverTick = false;
}

void ASpawner::BeginPlay()
{
	Super::BeginPlay();
	SpawnNow();
}

void ASpawner::SpawnNow()
{
	if (SpawnBP)
	{
		UWorld* world = GetWorld();
		world->SpawnActor<AActor>(SpawnBP, FVector(0, 0, 150), FRotator::ZeroRotator);
	}
}

You can then add the class to your level and in the details set the blueprint to spawn. This is really ugly for me as I have a large number of blueprints to add and I found UE often forgot that I had added them in a previous save and left them blank, hopefully this is just me.

I needed to delete my Binaries and Intermediate directory, and right click on my uproject to “Generate visual studio project files”. Under platforms I cooked content and then packaged. I find UE5 quite unstable and I often have to do this annoying loop, I’m not sure if this is just UE5 or my installation, but the blueprint was not appearing in the package build until I did this.

I should add that I’m a complete UE noob, so I may well not be doing this the correct way - I would be happy to hear a better way to use some procedural cpp to spawn blueprint “tiles” into a level.