Download

Spawning a Blueprint *outside* of the Constructor in C++ (Version 4.1.1)

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?

Firstly, you need to obtain reference to the blueprint in constructor and save it.

In your class .h file:



TSubclassOf<class AMyCharacter> MyCharacterReference;


In you constructor:



static ConstructorHelpers::FObjectFinder<UBlueprint> MyCharacterBlueprint(TEXT("Blueprint'/Path/To/Blueprint/Asset'")); //You can obtain this reference from editor
		
if(MyCharacterBlueprint.Object)
{
	MyCharacterReference = (UClass*)MyCharacterBlueprint.Object->GeneratedClass;
}


Then you can spawn this blueprint anywhere you like using this code:



FActorSpawnParameters SpawnParameters;

//Set up spawn parameters
SpawnParameters.Instigator = GetPawn();

//Spawn actor
AMyCharacter* CreatedObject = CurrentWorld->SpawnActor<AMyCharacter>(MyCharacterReference, Location, Rotation, SpawnParameters);

if(CreatedObject)
{
	//Object successfully spawned
}


Hope this helps. :slight_smile:

Worked like a charm, thanks!

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!

Can you provide the code sample? :smiley:



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.

Cheers!

Hey i made this static function to spawn Blueprints outside the constructor.



/** 
	@name: SpawnBlueprintFromPath<T>(UWorld* MyWorld, const FString PathToBlueprint, const FVector SpawnLocation, FRotator SpawnRotation, FActorSpawnParameters SpawnInfo)
	@param: Pointer to loaded world.
	@param: Path to the Blueprint we want to spawn.
	@param: Spawn Loaction for the new Blueprint actor.
	@param: Spawn Rotation for the new Blueprint actor.
	@param: FActorSpawnParameters for the new Blueprint actor.
	@Description: Spawn a Blueprint from a refrence path in PathToBlueprint.
*/
template <typename T>
static FORCEINLINE T* SpawnBlueprintFromPath(UWorld* MyWorld, const FString PathToBlueprint, const FVector SpawnLocation, FRotator SpawnRotation, FActorSpawnParameters SpawnInfo)
{
	FStringAssetReference ItemToReference(PathToBlueprint);
	UObject* ItemObject = ItemToReference.ResolveObject();
	if ((ItemObject) && (MyWorld))
	{
		UBlueprint* GeneratedBP = Cast<UBlueprint>(ItemObject);
		return MyWorld->SpawnActor<T>(GeneratedBP->GeneratedClass, SpawnLocation, SpawnRotation, SpawnInfo);
	}
	else {
		return NULL;
	}
}

You can include this function in a StaticLibrary / header file and spawn any Blueprint on the fly.
You just need the path to it of course, credits to iniside](https://forums.unrealengine.com/member.php?80-iniside) for helping me find](https://forums.unrealengine.com/showthread.php?14928-Spawn-Blueprint-at-runtime-from-code-using-path-of-blueprint) this solution.
Hope it help.

WCode

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.

They do, but that’s bound to change.

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;
}

Both approaches work equally anyhow.

-Camille