How to spawn a blueprint actor via C++

Hello! I’m a beginner in Unreal Engine and want to spawn an Actor declared in a blueprint. I’m using UE 4.13 and VS 2015. This is my current code of a C++ Actor which should spawn a blueprint actor on BeginPlay():

	PrimaryActorTick.bCanEverTick = true;

	//UStaticMesh* meshToUse = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube.1M_Cube'")));
	//jednostka = meshToUse->GetBounds().GetBox().GetSize();

	//this->SetActorScale3D(3 * jednostka);

void ACzastka::BeginPlay()

	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("BeginPlay called"));
	UObject * ClassPackage = ANY_PACKAGE;

	UObject* ObjectToSpawn;

	ObjectToSpawn = FindObject<UObject>(ClassPackage, TEXT("/Game/CubicColony/Blueprints/Zabawa/ZabawnaKostka.ZabawnaKostka"));
	UClass * ClassToSpawn = ObjectToSpawn->StaticClass();
	const FVector Location = { 0, 20, 0 };
	const FRotator Rotation = FRotator(0, 0, 0);
	AActor * NewActor = GetWorld()->SpawnActor(ClassToSpawn, &Location, &Rotation);

	//FString a = NewActor->GetActorLocation().ToString();
	//GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, a);

void ACzastka::Tick(float DeltaTime)


The code does absolutely nothing except printing “BeginPlay called” and if i uncomment the lines:

//FString a = NewActor->GetActorLocation().ToString();
	//GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, a);

build the C++ source and play the game in the Editor, UE crashes. I suppose I’m doing something wrong with loading and/or spawning the blueprint. I also have the same problem with the first commented lines (with the UStaticMesh). Can somebody help me?

Most probably the NewActor reference is null, can you do a “if” branch after spawning the actor, to print a string if the NewActor is indeed null?
If that’s the case, then also add a if to check if the UClass *ClassToSpawn is null also, that could be your problem.

If the UClass is null, then the find object returned nothing, try to right click your blueprint (the one you want to spawn) and “Get Reference” or something like that, that will give you the full path of the blueprint,

  • Edit, i wrote no the wrong post-

You should avoid using strings to reference your assets. I have written a guide on how to spawn a Blueprint class through C++ code in this blog post.

“/Game/CubicColony/Blueprints/Zabawa/ZabawnaKostka.ZabawnaKostka” is not a Package. It is a class. “/Game/CubicColony/Blueprints/Zabawa/ZabawnaKostka” Is a package. Your code looks for a class it can cast to a package. Which will always return null. And Do Nothing.

4.24 it Work. Good Luck

UObject* SpawnActor = Cast<UObject>(StaticLoadObject(UObject::StaticClass(), NULL, TEXT("/Game/DEXIED/Foliage/Tree/BP_TreeDestroyed_Style_1.BP_TreeDestroyed_Style_1")));

UBlueprint* GeneratedBP = Cast<UBlueprint>(SpawnActor);
if (!SpawnActor)
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("CANT FIND OBJECT TO SPAWN")));

UClass* SpawnClass = SpawnActor->StaticClass();
if (SpawnClass == NULL)
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("CLASS == NULL")));

UWorld* World = GetWorld();
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
World->SpawnActor<AActor>(GeneratedBP->GeneratedClass, GetActorLocation(), GetActorRotation(), SpawnParams);

Very useful

Nice thanks, it works in Editor. But in packed game i get “CANT FIND OBJECT TO SPAWN”. Looks like it cant find the blueprints. Can someone help?

I think you need to specify the files for packing in the package settings.

Just use TSubclassOf<AActor> (or whatever class you want), way easier, less hard coded, and more user friendly.

// Pseudo code...

// Weapon.h
// In the editor, you'll be able to select an actor class from a drop-down menu, including BP classes.

UPROPERTY(EditDefaultsOnly, Category = "Parameters")
TSubclassOf<AActor> ProjectileClass;

// Weapon.cpp
GetWorld().SpawnActor<AActor>(ProjectileClass, GetActorLocation(), GetActorRotation());


This works for me also, thanks.

How could one spawn it using BeginDeferredActorSpawnFromClass? I would like to initialise the spawned actor with some parameters before spawning but cant quite figure out how to do so.

for anyone having trouble in the future, this video show you exactly what to do.