Hey, I’m trying to spawn a blueprint in my game from code. However this blueprint will be called from code after the constructor.
And well, I’m not quite sure how to do this… SpawnActor() doesn’t seem to be spawning the blueprinted version of the class, which I suppose makes sense since its referring to the ACharacter I’ve created and note necessarily the blueprinted version?
Essentially my question is how do I spawn a blueprint in C++, particularly outside of the constructor?
One small suggestion I would make, find the class rather than the blueprint when you want to spawn something in the game. We are slowly moving towards not having to load the Blueprint object itself in the game at all!
static ConstructorHelpers::FObjectFinder<UClass> MyBlueprintClass(TEXT("/Game/Path/To/MyBlueprint.MyBlueprint_C"));
if (MyBlueprintClass.Object)
{
MyCharacterReference = MyBlueprintClass.Object;
}
“MyBlueprint_C” is the name of the blueprint-generated class inside the “MyBlueprint.uasset” package. You don’t need to specify the type in the asset reference anymore.
From what I understand, UBlueprint objects do not get added to the Shipping build type. Therein, this code will fail. To fix it, replace the last ’ on the asset ref with _C’ and change UBlueprint to UClass. And the generatedclass bit.
I went ahead and corrected the wiki articles I could find that still directly refer to UBlueprint GeneratedClasses. That only leaves the forums, hopefully people will catch on and stop repeating this approach before it gets deprecated in a breaking fashion.
As far as I can tell, it was never necessary. It gets ignored with barely a warning, it’s only there if you want to identify your asset type at a glance from its path.
Also, for classes, there is a separate constructor helper which can help in returning strongly typed subclasses:
static ConstructorHelpers::FClassFinder<AMyBlueprintBase> MyBlueprintClass(TEXT("/Game/Path/To/MyBlueprint.MyBlueprint_C"));
if (MyBlueprintClass.Class)
{
MyCharacterReference = MyBlueprintClass.Class;
}