Spawn Blueprint Class C++ Runtime

Hi,

I have a BP_FloorTile static mesh, want to spawn it at runtime in C++, C++ claims type is undefined however if I use the Blueprint SpawnActor function it shows BP_FloorTile available from the Class selection. Any ideas? I am a C++ engineer so I can’t stand drag and drop stuff, Thanks.

This is a bit unclear due to the name? Is it a blueprint or is it a static mesh. Assuming you do in fact mean a blueprint then you would declare a member

TSubclassOf<AActor> BPToSpawn;

Then in the constructor

#include "ConstructorHelpers.h"

AExample::Example()
{
    // "/Game/" is the content folder
    ConstructorHelpers::FClassFinder<AActor> BPFinder(TEXT("/Game/Path/To/BP"));
    BPToSpawn = BPFinder.Class;
}

Then used like

GetWorld()->SpawnActor(BPToSpawn, ...);

Thanks DanM,

Sorry meant to say blueprint, which contains static mesh, quickly figured out the had to use FClassFinder in a constructor :wink:

Was previously using
EngineUtils::FindOrLoadAssetsByPath(*path.ToString(), uAssets, EngineUtils::ATL_Class);

Which worked but failed to cast to UBlueprint* or AActor* , your FClassFinder solutions worked though.

Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.