Instantiate Blueprint class in C++

hello,

so i updated my code to UE 4.8 recently. I fixed up all the warnings and errors that came with the integration.
All except one :

this is the code I have a hard time to migrate to 4.8 without a warning

    static ConstructorHelpers::FObjectFinder<UClass> MyBPClass(TEXT("Class'/Game/Blueprints/MyCustomBP.MyCustomBP_C'"));
    if (MyBPClass.Object != NULL)
    {
        MySavedBPClass = Cast<UMyBPClass>(StaticConstructObject(MyBPClass.Object, this, TEXT("MyCustomBPClass")));
    }

this gives me the deprecation warning :

    warning C4996: 'StaticConstructObject': StaticConstructObject is deprecated, please use NewObject instead. For internal CoreUObject module usage, please use StaticConstructObject_Internal. Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.

so the question is, how do I convert this to the NewObject paradigm ? I tried simply doing :

MySavedBPClass = NewObject<UMyBPClass>(this, TEXT("MyCustomBPClass"), RF_NoFlags, MyBPClass.Object);

but that caused a crash/assert later at runtime saying that the template type does not match the class type that I am trying to instantiate.

What is the correct way to instantiate a BP class in C++ with UE 4.8 ?

going to try and bump this, would be cool if someone at Epic could give me a hint how to solve my warning correctly :slight_smile:

As of 4.8 you should not be spawning UObjects in class constructors, due to multi-threading.

Not sure what you’re trying to do entirely, but you can save off the class using UClass* and then spawn the Object in begin play.

.h

UPROPERTY()
UClass* YourBPClass;

//~~~

.cppp

static ConstructorHelpers::FObjectFinder<UClass> MyBPClass(TEXT("Class'/Game/Blueprints/MyCustomBP.MyCustomBP_C'"));
     if (MyBPClass.Object != NULL)
     {
         YourBPClass = MyBPClass.Object; 
     }

//end of constructor

....

//now in BeginPlay() you can use NewObject with YourBPClass

But keep in mind you cant refer to variables of a BP class easily in c++, still not sure what your overall goal is, but this should get you started :slight_smile:

#The Easy Way

You can avoid all of this by setting the class in BP, assuming the class that is storing this variable is already Blueprinted (Such as the Character class)

UPROPERTY(EditDefaultsOnly,BlueprintReadWrite,Category="BP Classes")
UClass* YourBPClass;

Rama