How to use BP defined class in c++ function declaration?

Hi there,

We want to define a BlueprintCallable function and use a BP defined Class as Input.

How do we get a reference to the BP defined Class in order to use it in a declaration like this:

OurBPClass = ??

 

UCLASS()
class VIZIONDEMO_API UVizionCBPFnLib : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
    UFUNCTION(BlueprintCallable, Category = "EntityData", meta = (WorldContext = "WorldContextObject", DeterminesOutputType = "In", DynamicOutputParam = "Out"))
        static void GetTest(const UObject* WorldContextObject, TSubclassOf<OurBPClass> In, TArray<UObject*>& Out);

};

You can’t, blueprint classes are generated at runtime so they are not known at compile time. Use TSubclassOf with the most-derived native parent class instead.

If your BPs extend actor or another very common class, you can make an intermediate c++ class inbetween Actor and the Bp class, just for the sake of filtering.

You can check at runtime if the supplied class is a blueprint class :

if (Cast<UBlueprintGeneratedClass>(In))
{
	// is a blueprint class
}

You can even check if the supplied class is a subclass of a specific blueprint class, but that means hardcoding a string reference which is not recommended :

UClass* BpClassRef = FindObject<UClass>(nullptr, TEXT("/Game/Blueprints/MyBlueprint.MyBlueprint_C"));
if (BpClassRef && In && In->IsChildOf(BpClassRef))
{
	// is a child class of BpClassRef
}

If you can, I would make a variable in header file, make it BlueprintReadWrite and set it in blueprint before calling a c++ function.

Hi Chatouille,

thank you for the insides, really appreciate it!

Your explanation is plausible to us. BP classes are not known at compile time.
We hoped to use the derived _C class somehow, for which you provided us the last code snippet. Thanks for that! Not ideal of course.

Mostly we wanted to use this, to be able to only input a subset of bp classes into a BP function.
Something like UObject → OurParentBPClass → OurChildBPClassXY
And only allow sub classes of OurParentBPClass.

I think this is not a common problem, or is resolved by defining those classes in C++ in the first place.

Anyway, thanks again. :slight_smile:

Thank you Goodman, you’re a good man. :slight_smile:
We tried this and could be a solution for us too. We are rethinking our architecture now. :smile:

Cheers!