Properly instantiating a Blueprint at runtime using C++

First: I tried almost all answers to questions similar to this, and all made the engine crash in a different manner, or not spawn anything at all.

What I’m trying to do:
I have my door mesh. I click on “Create BluePrint using this”, and UE makes a BluePrint Class (Parent Class is Actor), I then add the behavior I want, and finally save the BP.

Now I need to load it at runtime from C++.

I’m doing some tests/learning inside the GameMode.cpp/h, I tried dozens of combinations, but as I said, all of them made the engine crash or do nothing. For example I tried something like this:

  • inside the constructor “ATestProjectGameMode::ATestProjectGameMode()”

    static ConstructorHelpers::FObjectFinder doorBP(TEXT(“/Game/TopDownCPP/Blueprints/doorBP.doorBP”)); /* also tried with FClassFinder but nothing */
    if (doorBP.Object != NULL)
    {
    door = portaBP.Object;
    }

  • inside the BeginPlay “void ATestProjectGameMode::BeginPlay()”

    UWorld * const World = ();
    if (World)
    {
    //here I tried World->SpawnActor<… but nothing
    //or NewObject(… but nothing

Should anyone teach me the proper way to do it, or point me to the right part of the documentation, I would be really grateful.

FObjectFinder can only get assets and UClass is not a asset… either Blueprint. Blueprint which a instance that contains scripts is UBlueprint class no UClass. UClass is part of reflection system and UClass generated from blueprint is added once it is loaded. UBlueprint contains GeneratedClass varable which contains UClass generated from that blueprint, which you can use to spawn.

If you can, you should avoid directly referencing assets in C++, insted you should use UClass* or TSubclassOf or diffrent asset class and make editor user (whatever it is you or you friend you work with) able to select default properties or settings windows. This is how UE4 it self deals with it, if you look around you can see a lot of class selectors. This also guaranty that you won’t have any reference issues in pakageing, since UE4 only package assets that are reference to each other and FObjectFinder don’t do that

Hi , thanks for the explanation!
So, if Blueprint is not an asset, what I’m trying to do is not referencing an asset, so I shouldn’t have any problem with cooked/packaged builds, correct?

Then, in the constructor could I use this?

static ConstructorHelpers::FClassFinder<UBlueprint> doorBPFinder(TEXT("/Game/TopDownCPP/Blueprints/doorBP.doorBP"));

if this is correct, how to I then spawn this outside of the constructor?
Thanks again.

Edit: someone on the forum helped me understand that the extra .doorBP added by the ‘Copy Reference’ was causing the SpawnActor not to work. Without it, it works. But I think I still have the problem that hardcoded paths are a bad practice in relation to packaging, correct?

Don’t know exacly how this works, but if you got asset in level or any asset that reference to that set on level it should work. You can still do it, best will be if you test packaging right away, but best practice id (again, if you can) by design make it varable that can be set in editor. Do your doorBP will have subclasses?

Ah i forgot to mention, if you have a crash look to the logs, usally you have details what happened there

Thanks , I’ll check packaging asap. And yes, I learned right away to read the crash reports, very useful. :wink:

I’m linking the solution provided in the forum here, to mark the thread as solved.
Bye!

Solution received in the forum