How do I get StaticClass() to work on my derived classes?

I would like to get StaticClass() to work using my derived classes.

I mean:
If I use any class from the framework it works without problems. For example AActor class.

const UStruct* SomeBase = AActor::StaticClass();

However when I use my own classes it doesn’t work… it usually crashes.

UMyClass::StaticClass(); → Boom!!

So the only way I have gotten the type_ptr of the class is by referencing it from a blueprint using TSubclassOf.

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf < UMyClass > MyClass;

So it would be great to be able to do it all from the code in C++.

Do I need to implement something special in my derived classes for StaticClass() to work?

Thank you so much!!

AFAIK it’s enough that they derive from Uobject, because it’s the class that implements UObject::StaticClass.

Are you including the correct headers and the modules in the build.cs file?

1 Like

I’m not sure…

Do you think something is missing there?

Thank you very much for your reply!!

It looks fine (If you are not writing code as an external module), can you post the declaration and the code that is giving you errors?

Ok… just now I’m using the other method for this reason…
but basically in the creation of all my Widgets I had this problem.
I had to get the references to using blueprints.

So the code crashed here:

So a generic case would be

UMyUserWidget::StaticClass(); -->Boom!!

Are you creating widgets in C++ or are you trying to reference blueprint widgets?

If they are BP widgets it’s pretty obvious why it’s not working (you can’t use the name of a BP class in C++, you can set them all to TSubclassOf<UUserWidget> and set their value in a child blueprint.

Let me know what’s the answer of the previous question to provide you better info.


Just for you to know, the names of the classes starting with “UC” are out of standard by the way. This is not your issue but it’s better to respect the naming convention:

  • Class derived from UObjects → UClassName
  • Class derived from AActor → AClassName
  • Class deirived from IInterface → IInterfaceName
  • Structs → FStructName
  • Enums → EEnumName
1 Like

Ok… I think now I understand what is happening…
Yes, I have all the Widget logic done in C++.

However, all the graphic design (layout) is done in the blueprint.

So, then I understood that in order to use StaticClass() the class must be made 100% in C++…

I feel like a fool!! :slight_smile:


Yes, I use C to know that it’s a class made in C++… at least temporarily, because I’m translating my bluprints to C++… it’s just a way to temporarily distinguish it

I forgot to mention that calling ::StaticClass() on a TSubclassOf<> is kinda nonsense, because it’s already returning a variable of type UClass, as you can see MyWidgetSubclass is purple, you can call ::StaticClass() on UObjects (Blue ones):

image

image

You can set this variables in a child blueprint and use them in C++, but remember to check if they are valid!

1 Like

Yes, my initial intention was to use for example

UCMessageBox::StaticClass()

Although it would be cooler in this case to be able to do it with the BluePrint class.

BPMessageBox::StaticClass()

Unfortunately there is no way to include a “BPMessageBox” definition in the header.


I am very grateful for your help!! :heart:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.