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():


ACzastka::ACzastka()
{
	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()
{
	Super::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)
{
	Super::Tick(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.

2 Likes

“/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")));
return;
}

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

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

4 Likes

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());


11 Likes

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.

2 Likes

Awesome, thanks!!

I meet the same problem. I magrate my old project to new engine version UE 5.1, and still use C++ spawn blueprint actor. It works all in “Standalone Game” Mode, but after packaged, c++ can’t spawn blueprint by path.

I guess the may be the new engine version UE5.1 only package assets in world, but that blueprint actor only in content folder.

So I change the default project settings. Project Settings>Project>Packaging>Advanced>Cook everything in the project content directory.

After it works well in both editor and package.

It’s too late but anyway. Try to use ObjectToSpawn->GeneratedClass instead of ObjectToSpawn->StaticClass() It worked out for me on UE 5.3