(UE5) How to spawn blueprint actor with C++?

Hello. I’ve been stucked because of spawning actor by using C++.

I know I can use ‘ClassName::StaticClass’ for the common C++ actor,
but what I want to spawn is ‘Blueprint Actor’

This is path for my blueprint.

‘/Game/BluePrints/Game/BP_EditorNote.BP_EditorNote’

I found a lot of QnA about this, but all of them doesn’t work.

I tried…

FName path = TEXT("Class'/Game/BluePrints/Game/BP_EditorNote.BP_EditorNote_C'");
UClass* GeneratedBP = Cast<UClass>(StaticLoadObject(UClass::StaticClass(), NULL, *path.ToString()));
GetWorld()->SpawnActor<AActor>(GeneratedBP, transform);
FName path = TEXT("Blueprint'/Game/BluePrints/Game/BP_EditorNote.BP_EditorNote'");
UBlueprint* ObjectToSpawn =
Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), NULL, *path.ToString()));
GetWorld->SpawnActor<AActor>(ObjectToSpawn->GeneratedClass)

in h

UPROPERTY(EditAnywhere)
TSubclassOf<class AActor> NoteBP;

In CPP
in constructor

static ConstructorHelpers::FObjectFinder<UBlueprint> Note(TEXT("Blueprint/Game/BluePrints/Game/BP_EditorNote.BP_EditorNote'"));
if (Note.Object)
{
	NoteBP = (UClass*)Note.Object->GeneratedClass;
}
GetWorld->SpawnActor<AActor>(NoteBP )

all of these are not working.

Also, for the location of each function, could you tell me where should I call them?
(like ConstructorHelpers in constructor)

I have problem with this about three days, but still got problem…

3 Likes

You must define a TSubclass of the blueprint you want to spawn and add the EditDefaultsOnly UPROPERTY like this:

UPROPERTY(EditDefaultsOnly)
TSubclassOf<myBlueprintBaseClass> my_BP_Class;

EditDefaultsOnly lets you pick the class in the editor. So If you do this and compile, then go back to the editor and you will see a slot select your actual blueprint class

then for spawning do this:

myBlueprintBaseClass* mySpawn = GetWorld().SpawnActor<myBlueprintBaseClass>(my_BP_Class,pos,rot,SpawnParams);

Good luck!

Dany

12 Likes

Thank you so much for your help!
But it still doesn’t work in my project…
Could you check my code…?

I am trying to spawn an actor with the Widget button

Widget File

void UEditorWidget::CreateNoteClicked()
{
	m_editorPlayer->SpawnNote();
}

Player File
h file

public:
	UPROPERTY(EditDefaultsOnly)
		TSubclassOf < AEditorNote> m_noteToCreate;

CPP

void AEditorPlayer::SpawnNote()
{
	GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Orange, FString("EditorPlayer Spawn Note!"));
	GetWorld()->SpawnActor<AEditorNote>(m_noteToCreate);
}

Also, I selected my bp file in the editor…

2 Likes

maybe you also need the position, rotation and spawnparameters? something like this?

GetWorld()->SpawnActor<AEditorNote>(m_noteToCreate,pos,rot,SpawnParams);
3 Likes

OH MY GOD…

It didn’t work even if I copied it, so I refreshed it, rebuilt it, regenerated the project, and it is suddenly WORKING. Maybe this is the answer.

You saved my life.

2 Likes

Also, in my case of problem, if there is bp object already at the location you want to spawn, spawning is failed. So before you test, you have to change the location of that object.