How to reference blueprint as a type in C++

How can a reference a blueprint (from a string) as a type such that I would be able to declare an object of type blueprint

#Reparent the BP

BP assets cannot be used as C++ types directly in code, unless they are a subclass of a C++ class.

Your Blueprint that you want to reference in C++ needs to have a C++ base class containing all the functionality you’d want to use in C++.

  1. Your Blueprint needs to be reparented to use this C++ base class.

  2. Since you want to use a string you can then use this code to obtain your BP, and it will work this way because your editor BP has been reparented to use your C++ base class.

    //some C++ class .h

    UPROPERTY()
    AYourBPCPPBaseClass* YourBPBaseClassPtr;

    //some C++ class constructor
    //Your BP of the base C++ class
    static ConstructorHelpers::FObjectFinder YourBPOb(TEXT(“Blueprint’/Game/Characters/YourBP.YourBP’”));
    if (YourBPOb.Object != NULL)
    {
    YourBPBaseClassPtr =
    Cast(YourBPOb.Object->GeneratedClass);
    }

3 Now in c++ you can use this pointer to your BP asset anywhere you want.

#Reparenting

Recall this all hinges on reparenting your BP to use your C++ Base class

And you can only access functions that have a presence in the C++

Since the C++ is a superclass of your BP asset in the Editor.

So all variables and functions you want to use must have a presence in the C++ class

#Recommended

Personally I dont recommend using string references, I recommend actually making a complete UPROPERTY and assigning it in the Editor.

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="MyBPForCPP")
TSubclassOf<AYourBPCPPBaseClass> YourBPBaseClassPtr;

This requires that your C++ class that is holding the reference already be exposed to BP, such as game mode or MyCharacter.

Rama

2 Likes

Thank you, to specify the reason I needed it as a type was so that I could assign it to a TSharedPtr through SAssignnew, based on what you said this probably isn’t possible, would there be a way to point to it through a TSharedPtr, if it helps my blueprint is a Widget for use in slate

#Extending UUserWidget

Are you really making a regular UCompoundWidget into a blueprint?!

Or are you using UUserWidget?

in the later case see this tutorial by WCode

A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums.

My response is still valid for anyone reading your question in regards to Bluepirnts in general

But for slate you should see the Slate tutorials on the wiki and the above tutorial

do I have to do that too to access my custom character class ?

TArray<AActor*> actors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), CharacterBaseClass::StaticClass(), actors);