How to spawn Actor from FAssetData

Hello,
I have UObjectLibrary in which load some data about my blueprints. Like that

ShipsLibrary = UObjectLibrary::CreateLibrary(AShip::StaticClass(), true, GIsEditor);
ShipsLibrary->AddToRoot();
int32 loaded = ShipsLibrary->LoadBlueprintAssetDataFromPath(TEXT("/Game/Ships"));

Then I get FAssetData for loaded blueprints.

TArray<FAssetData> Assets;
ShipsLibrary->GetAssetDataList(Assets);

And now i want to spawn an actor from FAssetData that I got from ShipsLibrary.
I try it that way:

auto wld = GetWorld();
auto res = wld->SpawnActorDeferred<AActor>(Assets[0].GetClass(), loc, GetActorRotation());
if (!res)
    UShipUtils::TestPrintMsg(TEXT("Spawn Failed!")); //Just my test log

But i always get “Spawn Failed!”. I Also tried Assets[0].GetAsset()->StaticClass() - same result.

Is there a way to spawn Actor(blueprint) from FAssetData ?

I know this is an old question, but in case anyone else stumbles across it, when using SpawnActorDeferred() you must call UGameplayStatics::FinishSpawningActor() afterwards.

There’s also a new SpawnActorDeferred that takes an FTransform (rather than an FVector) as the second parameter.

1 Like