How to get C++ class name of object?

There is some property (UPROPERTY) in some object.
Type of this property named USomeCppType
But in the Blueprint, type of this property overriden as SomeBlueprintType (derived from USomeCppType).

And now I want to know what name of CPP-ancestor of this object?

Code:

// definition code of property
public:
    UPROPERTY()
    UMinimap* Minimap;

In some virtual UMinimap method (called from Blueprint):

// the object want to know his C++ type name
this->GetMyCPPType();  // Should return the "UMinimap" string

71814-screen.png

Are there ways to got this type name string?

P.S. “UProperty” type has method GetCPPType, but this can be used only with TFieldIterator (another case)

To get the FName this should do the trick:

this->GetClass()->GetFName();

If you need a FString instead:

this->GetClass()->GetName();

If the final instance might inherit from UMinimap it will return the leaf name.

As @anonymous_user_f5a50610 said you can actually get the native class names, so it really depends on your situation. Using the class name will be a bit less flexible against adding a method that returns the name you want (an FName), so it would be solving your case only with pure inheritance and manual setup or with reflection and done all automatically.

1 Like

Method of Blueprint type inherited from UMinimap will return his name (not UMinimap). I need get the C++ ancestor name (UMinimap).

71814-screen.png

Yes as I said it will give you the leave type, may I ask a few questipns?

  • Could you provide a use case?
  • What if UMiniMap has another child in Child in C++, what oitput do you need?

Once I’m back on my machine I can post some examples.

  • Manager of widgets. Every widget in this manager can be created only once and only by this manager (like a singletons). And all this widgets can be refer to this manager. And can also find himself in. The case: invalidation on specified widget. I call method Invalidate on widget, and widget sets self to nullptr on the Manager using reflection (TFieldIterator). How to found self in manager? Using C++ ancestor typename for example (the blueprint widget is inherited from C++ widget).
  • No other childs… But this is interesting too. The parent tree of clasees.

You got GetPrefixCPP() you can add it to prefixless name

You can also get UProperty from here:

To get parent (which sometimes is called Super) UClass use this function:

In reflection system UClass is extantion of UStruct

1 Like

Ok so it I understand it correctly you need a string that matches exactly the name of the last native class of a given hirachy and the whole hirachy is inheriting from a common native class let’s say UBaseWidget?

In that case I would just add a virtual function to `UBaseWidget`` that returns the string of the last native class, the function could be a BlueprintCallable so you can not re-implement it in BP.

I may be a bit late, I got into the same problem that you, I needed to get the name of the CPP class from a Blueprint call. The accepted answer doesn’t really solve the problem, so here you are, an easy and clean solution:

If you want to get the CPP class, you can just use StaticClass()

85460-imggg.png

I hope this helps in the future to people that get into the same problem.

1 Like

Chiming in on this even further in the future. This answer is only sort of correct. StaticClass is not a virtual function and so will give you the class of the pointer type you are using. Using the example above (ignore the fact that these would be null/uninitialized):

URollLocalizationBank* Bank;
Bank->StaticClass();

In that case, it would return the class for URollLocalizationBank.

URollLocalizationBank* Bank;
UObject* BankObj = Bank;
BankObj ->StaticClass();

In this case, it would return UObject since that’s the type of the pointer being accessed.