How to Get Class Defaults in C++?

woah, that’s a good one, gonna try and find out.

1 Like

You need to Get the Default Object for that class, it returns an Object you can cast to a specific class, then retrieve values from it, they’ll all be the defaults.

AMyActor* DefaultActor = Cast<AMyActor>(AMyActor::StaticClass()->GetDefaultObject(true));

DefaultActor->SomeVariable;

Hello,

I was simply wondering, how I do something equivalent in C++ to the blueprint-function “GetClassDefaults”?
I have a variable that is a subclass of my UObject-class UPrimalItemBase, which is called “ItemClass”, that is a class-reference to the class I need access to:

	UPROPERTY(BlueprintReadWrite, Category = "Inventory")
	TSubclassOf<UPrimalItemBase> ItemClass;

I need this because I need to get some variables from my ItemClass. Here’s an image of the blueprint-function I need to convert to C++:

263380-screenshot-13.png

How could I do the same thing in C++? Thanks in advance, help is highly appreciated! :slight_smile:

Stefan

1 Like

Hello Evigmae,

I know I can access default variables this way, however, I also need to be able to access variables from classes that are subclasses of my superclass. If you could provide me a solution to this that would be great! :slight_smile:
However, I’m not even sure if this is possible.

Thanks!

can’t you just Cast to the subclass instead?

Not really, because I will probably have around 10 subclasses. But I guess this is not actually possible, so I’m marking this post as resolved.

Thanks for your attention and help! :slight_smile:

if the sub-classes inherit the variable from the same actor, casting to the parent class will get you the default.

eg: You can get the default values of AActor from any actor.

if all 10 classes have their own variables, then you would have to check individually.

Aah, that’s what I thought but wasn’t completely sure of.

FWIW we have the GetDefault and GetMutableDefault functions that are templated and return the CDO (Class Default Object) cast to the type you specify.

2 Likes

TSubclassOf template has a function GetDefaultObject.

TSubclassOf<ABaseClass> Subclass;
ABaseClass* DefaultObject = Subclass.GetDefaultObject();
2 Likes