How to spawn actor from BP?

Can you advice me how to spawn actor from BP?

Today I have the whole day trying to solve this problem and has not yet been able to solve it.

 auto cls = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/Cube_Blueprint"));
 UBlueprint * bp = Cast<UBlueprint>(cls);

How to spawn bp object?

Do you want to spawn Object or Actor?
If actor use:

World->SpawnActor(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);

This code is from: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

If object, use:

UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);

UObject* tempObject = ConstructObject(UC);

EDIT:
I just realized what do you want to achieve. Look at this code:

ConstructorHelpers::FObjectFinder BlueprintReference(TEXT("SomePath")); ConstructObject(BlueprintReference.Object->GeneratedClass);

I lost 2 days to solve this problem…

But I made solution!

void AMyNativePlayerController::MyLoadClassByName10(const FString& Path)
{


	auto cls = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/Wood_Table_Blueprint"));
	if (!cls)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load UClass " + Path));
	else
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("UClass LOADED!!!!"));

	UBlueprint * bp = Cast<UBlueprint>(cls);

	if (!bp)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Failed to load UClass 2  " + Path));
	else
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("UClass LOADED!!!! 2 " + bp->GetName()));

	TSubclassOf<class UObject> MyItemBlueprint;

	MyItemBlueprint = (UClass*)bp->GeneratedClass;
	UWorld* const World = GWorld->GetWorld();
	if (World){

		FActorSpawnParameters SpawnParams;
		//SpawnParams.Instigator = this;
		UObject* DroppedItem = World->SpawnActor<UObject>(MyItemBlueprint, { 0, 0, 0 }, { 0, 0, 0 }, SpawnParams);

	}
	else {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("NO WORLD!!!!"));
	}

}

That worked perfectly for me, thanks a lot.
Here’s s copy&paste version for anyone:

TSubclassOf<class UObject> Common::FindOrLoadBluePrintClass(const TCHAR* path)
{
	// finds or loads a blueprint class. Needed to create BP based objects in C++.
	/*
		// usage:
		TSubclassOf<class UObject> BP_Item3DActor= FindOrLoadBluePrintClass(TEXT("/Game/FirstPerson/Blueprints/BP_Item3DActor"));

		AItem3DActor* t = Common::world->SpawnActor<AItem3DActor>(BP_Item3DActor, wPos, SpawnRotation, SpawnParams);
		// 't' actually points to a BP class derived from C++ class 'AItem3DActor'
	*/

	UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, path);
	UBlueprint* bp = Cast<UBlueprint>(something);
	TSubclassOf<class UObject> MyItemBlueprint;
	MyItemBlueprint = (UClass*)bp->GeneratedClass;

	return MyItemBlueprint;
}

Hi,

I tried this and it works in editor, but when I package the game it can’t find the BP class. Any ideas?

I had the same issue and struggled a bit to get the solution from the first answer to work from blueprints - so for everyone who has the same problem. First of all i created a c++ blueprintfunction library, here the header:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

UCLASS()
class PICKDROPPROTOTYP_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
   UFUNCTION(BlueprintCallable, BlueprintPure, Category="SunShine")
   static TSubclassOf<class UObject> FindOrLoadBluePrintClass(FString path);
};

Here the cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "PickDropPrototyp.h"
#include "MyBlueprintFunctionLibrary.h"

TSubclassOf<class UObject> UMyBlueprintFunctionLibrary::FindOrLoadBluePrintClass(FString path) //const TCHAR* path)
{
   TSubclassOf<class UObject> MyItemBlueprint = NULL;

   UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, *path);
   if(something != NULL)
   {
      UBlueprint* bp = Cast<UBlueprint>(something);
      if(bp != NULL)
      {
         if(GEngine)
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("found some ■■■■")));
         MyItemBlueprint = (UClass*)bp->GeneratedClass;
      }
      else
      {
         if(GEngine)
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("bp is NULL")));
      }
   }
   else
   {
      if(GEngine)
         GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("something is NULL")));
   }

   return MyItemBlueprint;
}

And here a BP example how to use it (the path you can just get by the content browser, right-click on some StaticMeshActor and select “Copy Reference”)

The spawn location is just some static thing which i add to the camera, you can get the location from where ever you like to spawn a new object :wink:

I’m having the same issue too. The object cannot be found in the packaged version, but works fine in standalone and editor.

In case somebody is looking for this. If you need the “Reference” to an object, you can just find it in your UEditor browser, right click, COPY REFERENCE → It will return something that you can paste in your code: example: Blueprint’/Game/Blueprints/BPPortal.BPPortal’