Download

Discovering blueprints that extend c++ base class

I’d really like to be able to define a base class in c++ and be able to automatically discovery any blueprints that extend the base c++ class. This would let me introduce menus that could be used to pick any of the new subclasses added by content developers without boilerplate or placing in the world.

I have tried using PCIP.GetClass() to grab subclasses when they are constructed via the base class ctor, and it works but only reliably for c++ subclasses. Subclass blueprints of the c++ base class will execute the base class ctor but only if you open them in the editor or place them in the word. If you package and run the game, they will not at all unless placed in the world. I have also tried the object iterator which will only detect those blueprints upon editing or placing in the world.

How do I go about automatically discovering all blueprint subclasses of a target c++ class in UE4?

As far as i know, Blueprints can’t be accessed until you spawn them. I could be wrong on that.

The editor is aware of them though, it would be nice if that functionality could be reproduced in c++/blueprint.

Ok so since my question hasn’t garnered much of a response, I should ask a different one.
I want to create a whole lot of equippable unique inventory items. They need to be pickable from a player loadout menu before the player can spawn with them. What is the unreal way of doing this?

This post is asking essentially the same thing. You will find some leads there.

The editor is aware of these classes because it is able to work with classes and objects that are not already loaded into memory. In order for something to be loaded in memory, you either need to load it manually or load something else that refers to it.

The approach is the latter – loading an existing reference. The simplest way to do this would be to include an array of TSubclassOf<UMyInventoryItemBase> as an editable property in a central blueprinted class like your GameMode. Any time you add a new type of item, just add a new array entry and specify that new item type. Then your loadout menu can access that same array to know which item types will be available for your player to spawn with.

If that is still too much boilerplate to your liking, I’m afraid it gets considerably more complicated. As you’ve noticed, the object iterator only includes blueprint classes once they’ve been used in the world. In order to load them, you will have to use the asset registry, the same mechanism the editor uses to be “aware” of those unloaded blueprints. As mentioned by Marc in the linked post, a good example to base yourself on is FClassHierarchy::PopulateClassHierarchy, which you will find in SClassViewer.cpp .

On top of that, this will only get you to load the missing blueprints. Once you are ready to prepare your final cooked build, you will likely have a bad surprise waiting when most of your blueprints fail to load. Like all other assets, in order for blueprints to be included in a cooked build, you need to reference them somewhere so the cooker finds them. You would do this by having the aforementioned TSubclassOf<UMyInventoryItemBase> array property. But instead of manually populating it yourself by making the array property editable, you would use your newly minted asset registry loader to populate the array.

This is a really good explanation, thanks a bunch!