Get Blueprint class by String in C++

This method works only in editor :

TLDR : If your BP class is named “BP_Tree” and the reference path to it is : '/Game/ThirdPerson/Blueprints/BP_Tree.BP_Tree'

This would be the command to retrieve it

UClass* Result = StaticLoadClass(UObject::StaticClass(), nullptr, TEXT("/Game/ThirdPerson/Blueprints/BP_Tree.BP_Tree_C"), nullptr, LOAD_None, nullptr);

Notice the added “_C” at the end of the reference path that’s mandatory to find the class.
StaticLoadClass loads from disk.


Here is a fully detailed answer on how to achieve this with an example :

Let’s say your BP classes are stored inside an array of structures and you want to spawn on the map (Load from disk) the BP Classes stored as Strings in the structures.

In your .h file, you’ll have this struct declared

//In .h file
...
struct AssetStruct
{
	GENERATED_BODY()

public :
	UPROPERTY()
	int Id; //This field is not used in the example
	
	UPROPERTY()
	FString BPClassName;
};
...

The below image shows the two BPs whos names will be written inside the array of Asset Structs :
image

You’ll need to get the reference path to this folder :

In this case, a reference path to ‘BP_Tree’ would be /Script/Engine.Blueprint'/Game/ThirdPerson/Blueprints/BP_Tree.BP_Tree'

Notice how ‘BP_Tree’ is repeated Twice at the end of the path and seperated by a “.” (point).

In your .cpp code, you’ll perform the following :

...
//In .cpp file
TArray<AssetStruct> Assets;

AssetStruct BP01;
BP01.BPClassName= "BP_Tree";

AssetStruct BP02;
BP02.BPClassName= "BP_SimpleCube";

Assets.Add(BP01);
Assets.Add(BP02);

const FString SpawnablesPath = "/Game/ThirdPerson/Blueprints/";
const FString ClassNameSuffix = "_C";

for (int i = 0; i < Assets.Num(); i++)
{
	FString ReferencePathS = SpawnablesPath + Assets[i].BPClassName+ "." + Assets[i].BPClassName+ ClassNameSuffix;
	const TCHAR* ReferencePath = *ReferencePathS;
	UClass* Result = StaticLoadClass(UObject::StaticClass(), nullptr, ReferencePath, nullptr, LOAD_None, nullptr);
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Result->GetName());
}

The catch of this method is that you need to know the path to the folder containing those BPs whos names are in the structs (SpawnablesPath). The most important part is that you’ll need to append the class name from the struct (BPClassName) two times and separate these two with a “.” because that’s how a reference path is formatted. The “_C” suffix is added to the actual reference path so that “StaticLoadClass” can find them and load them from disk.. Finally, you’ll need to convert the FString containing the reference path to the BP class to a const TCHAR before calling “StaticLoadClass” and passing it as a variable.

Don’t hesitate to ask any questions regarding this.

3 Likes

For this use TSoftClassPtr. This way you can choose those classes in the editor and then use them in C++.
If you really want to work with string paths in C++, then you can do this:

#include "UObject/ConstructorHelpers.h"

		FString AssetPath = TEXT("/Game/Blueprints/BP_TargetItem");
		UClass* BP_TargetItemClass = ConstructorHelpersInternal::FindOrLoadClass(AssetPath, UObject::StaticClass());

This is the most universal and easy to use way to load blueprint classes in C++. Works in constructors and outside. You don’t need the .BP_TargetItem_C after package path.

You need to add CoreUObject to the dependencies of your project.

1 Like