x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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++.

Product Version: Not Selected
Tags:
more ▼

asked Sep 03 '14 at 05:37 PM in C++ Programming

avatar image

BaderThanBad
664 21 18 177

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first
 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);
more ▼

answered Sep 03 '14 at 07:50 PM

avatar image

Pierdek
2.6k 73 49 131

avatar image BaderThanBad Sep 03 '14 at 08:58 PM

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

avatar image BaderThanBad Sep 04 '14 at 04:35 PM

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.

avatar image BaderThanBad Sep 08 '14 at 03:39 PM

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.

avatar image illYay Feb 21 '15 at 06:43 AM

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

avatar image Henrik Oct 14 '15 at 09:05 AM

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

avatar image Burnrate Dec 13 '15 at 02:59 AM

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

avatar image Wallenstein Sep 14 '15 at 11:59 AM

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.

avatar image Yunze Gu Jan 06 '17 at 10:01 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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;
 
more ▼

answered Sep 04 '14 at 06:24 PM

avatar image

joeGraf STAFF
1.5k 35 12 69

avatar image BaderThanBad Sep 07 '14 at 05:47 PM

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.

avatar image Pierdek Sep 07 '14 at 06:07 PM

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
 
avatar image BaderThanBad Sep 07 '14 at 06:16 PM

It'll take me a while to test all of those :P

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

avatar image BaderThanBad Sep 07 '14 at 06:55 PM

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?

avatar image Pierdek Sep 07 '14 at 08:49 PM

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

avatar image BaderThanBad Sep 08 '14 at 02:22 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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;

}

more ▼

answered May 15 '16 at 03:34 PM

avatar image

xCPx
6 1 2

avatar image pr4wl Sep 13 '17 at 07:10 PM

Update to the update :)

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: https://answers.unrealengine.com/questions/688127/issues-with-any-package-used-in-findobject.html

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: https://answers.unrealengine.com/questions/330309/an-issue-with-runtime-savingloading-of-blueprint-c.html

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question