Get Blueprint class by String in C++

I have multiple blueprint classes in different folders in the content browser. I wish to make a reference to a blueprint class dynamically from a string in C++, how would I achieve this?

The string is dynamic so the reference needs to be set in runtime (So constructor helpers wont do since there are a lot of blueprints and making references to each one in the constructor portion of the class wouldn’t be either modular and a big waste of time, not to mention adding a reference each time i make a blueprint).

All the blueprints are parented from a C++ class.

I was thinking along the line of a get class by name function but can’t seem to find something like that in C++.

FClass* FClasses::FindClass(const TCHAR* ClassName) const
{
check(ClassName);

	UObject* ClassPackage = ANY_PACKAGE;

	if (UClass* Result = FindObject<UClass>(ClassPackage, ClassName))
		return (FClass*)Result;

	if (UObjectRedirector* RenamedClassRedirector = FindObject<UObjectRedirector>(ClassPackage, ClassName))
		return (FClass*)CastChecked<UClass>(RenamedClassRedirector->DestinationObject);

	return nullptr;
}

So check this out:

UObject* ClassPackage = ANY_PACKAGE;
UClass* Result = FindObject<UClass>(ClassPackage, ClassName);

I’ll give it a shot and let you know how it went tomorrow.

I have tried the function but to no avail. The function always returns Null no matter what ClassName i specify. I tried the blueprint name, the reference from the content browser, and more but no luck.

You can use the FObjectFinder helper to do this. Here’s an example:

	static ConstructorHelpers::FObjectFinder<UTexture2D> HealthBarTextureObj(TEXT("Texture2D'/Game/UI/HealthBar.HealthBar'"));
	HealthBarTexture = HealthBarTextureObj.Object;

So, what about:

UClass* StaticLoadClass( UClass* BaseClass, UObject* InOuter, const TCHAR* InName, const TCHAR* Filename, uint32 LoadFlags, UPackageMap* Sandbox )


UClass* GameModeParamClass = StaticLoadClass(AGameMode::StaticClass(), NULL, *GameClassName, NULL, LOAD_None, NULL);

or 


TSubclassOf<AParticleEventManager> ParticleEventManagerClass = Cast<UClass>(StaticLoadObject(UClass::StaticClass(), NULL, *GEngine->ParticleEventManagerClassPath, NULL, LOAD_NoWarn, NULL));


or 

USkeletalMesh* PreviewMesh = PreviewSkeletalMesh.Get();
PreviewMesh = Cast<USkeletalMesh>(StaticLoadObject(USkeletalMesh::StaticClass(), NULL, *PreviewMeshStringRef.ToString(), NULL, LOAD_None, NULL));

or

USoundBase* Sound = Cast<USoundBase>( StaticFindObject( USoundBase::StaticClass(), NULL, *SoundAssetName ) );
		if( Sound == NULL )
		{
			Sound = Cast<USoundBase>( StaticLoadObject( USoundBase::StaticClass(), NULL, *SoundAssetName ) );
		}



or something ;0

That still does not answer my question. The reference (“Texture2D’/Game/UI/HealthBar.HealthBar’”) has to be dynamic, and this operation is only doable in the constructor. I want to build a reference during runtime, not on construction of the editor.

It’ll take me a while to test all of those :stuck_out_tongue:

But I feel like you are linking to me classes or objects that have been created in C++, not blueprint, is that the case?

LogUObjectGlobals:Warning: Failed to find object ‘Class None.*****’

fails to find object, where ***** is the name of the blueprint. Using the staticloadclass. What name should I use for the InName?

You have the whole engine code, you can put a breakpoint in one of the places listed by me or search them yourself, then you will learn how use this functions:)

If you just gave me an example of the string you are using for these I might get somewhere.

FindObject with Uclass finally worked. Did not know I had to append a _C at the end that was what I was missing, Thanks for bearing with me on this one.

Can you give an example of how you wrote out the blueprint class name?

AH! I got it. If your blueprint reference looks like this:
Blueprint’/Game/Environments/AsteroidBaseMocks/AsteroidHall_L/Base_A_AsteroidHall_L_Straight.Base_A_AsteroidHall_L_Straight’

Then the string is:
/Game/Environments/AsteroidBaseMocks/AsteroidHall_L/Base_A_AsteroidHall_L_Straight.Base_A_AsteroidHall_L_Straight_C

Take off the Blueprint part and the single quotes and end it in _C

What is FClass? Is that old code that won’t compile anymore? I’m havinv the same problem now, but the solution doesn’t compile.

Thanks guys this was helpful! I’ll just post a final format which worked for me:

UClass* blueprintClass = FindObject(ANY_PACKAGE, TEXT("/Game/Blueprints/GameState/GameMode_Parent.GameMode_Parent_C"));

Hi, does this still work for you? the function for me needs an outer object but I cannot seem to find one that works.

Updated version:

UClass* UUtility::FindClass(const FString& ClassName)
{
check(*ClassName);

 UObject* ClassPackage = ANY_PACKAGE;

 if (UClass* Result = FindObject<UClass>(ClassPackage, *ClassName))
	 return Result;

 if (UObjectRedirector* RenamedClassRedirector = FindObject<UObjectRedirector>(ClassPackage, *ClassName))
	 return CastChecked<UClass>(RenamedClassRedirector->DestinationObject);

 return nullptr;

}

Niubility!! And note that no prefix for the string of the class name, namely ‘Car’ not ‘ACar’

Update to the update :slight_smile:

To get rid of the intelisense error you can change

#include "CoreMinimal.h"

to

#include "EngineMinimal.h"

Then you can just use:

UClass* Result = FindObject<UClass>(ANY_PACKAGE, *ClassName)

Source: Issues with ANY_PACKAGE used in FindObject - Programming & Scripting - Unreal Engine Forums

Also note that if the engine hasn’t loaded the class, this might not find it. In that case you will want to use this to load the class:

UClass* Result = StaticLoadClass(UObject::StaticClass(), nullptr, *ClassName, nullptr, LOAD_None, nullptr);

Source: An issue with runtime saving/loading of Blueprint Classes - Blueprint - Unreal Engine Forums

This is the answer that worked for me in version 4.25.