Spawn Blueprints from C++

Hello everyone,

I already searched the whole day on how to spawn a Blueprint from C++ and I have found a few answers.
However, nothing described there works for me, maybe because the answers are already a few month old.

It would be very nice if someone could give me an example on how to do it.
Thanks a lot!

What methods did you use and why they didn’t work

Did you already try using ConstructorHelpers::FClassFinder,
then use ()->SpawnActor to spawn your blueprint?
What blueprint did you actually want to spawn? An actor, component, or UObject?
You can also put this into consideration when you want to access your blueprint

On what version of the engine are you? I do it often by exposing a TSubclassOf public property and then using ()->SpawnActor or the spawning function of UGameplayStatics.

I use 4.6. The ConstructorHelpers::FClassFinder is used to get the class of the blueprint I want to spawn. Then, we pass the class as argument to the SpawnActor.
If you already use the TSubClassOf and set the value of the variable, and set the ActorSpawnParameters, it should be spawned. Maybe the location of spawn is far from your Character.

I was asking the one who asked the question. I know how to spawn a Blueprint, was just trying to help out :slight_smile:

I am using 4.6.
Could you maybe provide a short example on how to
I already tried using TSubclassOf, but I didn’t get it to work:


.h

/** Blueprint Test Spawn */
UPROPERTY(EditDefaultsOnly, Category = Mesh)
TSubclassOf<class AAttachment> Blueprint;

.cpp

()->SpawnActor<AAttachment>(Blueprint, GetActorLocation(), GetActorRotation(), FActorSpawnParameters());

(This even leads to an editor crash - Access violation)

Here is an example of how I do it :

void UFXManager::SpawnCameraBasedActors(TSubclassOf<AActor> aActorToSpawn, FVector aSpawnLocation)
{
	FVector cameraLocation = UJotunnMain::Instance->GetCamera()->GetCameraPosition();

		FTransform spawnTransform;
		spawnTransform.SetComponents(FRotator::ZeroRotator.Quaternion(), aSpawnLocation, FVector(1.0f, 1.0f, 1.0f));

		AActor* spawnedActor = UGameplayStatics::BeginSpawningActorFromClass(UJotunnMain::Instance->(), aActorToSpawn, spawnTransform);
		UGameplayStatics::FinishSpawningActor(spawnedActor, spawnTransform);
}

Hope this helps.

As far as I know,
I can only use ConstructorHelpers in my Constructor, right? But I want to spawn them outside of the Constructor.
My blueprint was created from a custom class, which is an AActor.

Alright, thanks to your help I got it working!


Omega3Pawn.cpp

void AOmega3Pawn::SetAttachmentForSlot(int Slot, TSubclassOf<AAttachment> &Attachment)
{
	if (Slots[Slot] != nullptr)
	{
		UE_LOG(LogOmega, Log, TEXT("Destroying %s in slot %d"), *Slots[Slot]->GetName(), Slot);
		Slots[Slot]->Destroy();
	}

	Slots[Slot] = ()->SpawnActor<AAttachment>(Blueprint, FVector(400.f, 200.f, 0.f), FRotator(0.f, 0.f, 0.f), FActorSpawnParameters());
	Slots[Slot]->AttachRootComponentTo(RootComponent);
	UE_LOG(LogOmega, Log, TEXT("Spawned %s in slot %d"), *Slots[Slot]->GetName(), Slot);
}

Omega3Pawn.h

/** Blueprint to Spawn */
UPROPERTY(EditDefaultsOnly, Category = Attachments)
TSubclassOf<class AAttachment> Blueprint;
public:
	/** Set Attachment for Slot */
	void SetAttachmentForSlot(int Slot, TSubclassOf<AAttachment> &Attachment);

private:
	/** Blueprint Slots */
	AAttachment* Slots[2];

Now I can just call

	SetAttachmentForSlot(1, Blueprint);

and it works!

Thank you very much! (:

My mistake… I thought that you are the one who ask the question. I didn’t see the username, first. My bad…, sorry :slight_smile: