Spawn Blueprint from path in c++

Hi,

I’m trying to spawn an Blueprint Actor directly from C++ code, i found many answers to this question but none works (the only one was to add _C at the end of the path, but it doesn’t work when the game is packaged

I Have a “TargetItem” actor class and a blueprint parented from it. And i want to spawn it from another class

Spawner.h

UPROPERTY()
TSubclassOf<class AKryptos_TargetItem>	targetItemClass;

Spawner.cpp (in constructor)

ConstructorHelpers::FObjectFinder<UBlueprint> ItemBlueprint(TEXT("Blueprint'/Kryptos/BP/Targets/BP_TargetItem.BP_TargetItem'"));
if (ItemBlueprint.Object)
{
    UE_LOG(LogTemp, Warning, TEXT("OK"));
    targetItemClass = (UClass *)ItemBlueprint.Object;
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("NOT OK"));
}

Spawner.cpp (in another function)

AKryptos_TargetItem *item = _world->SpawnActor<AKryptos_TargetItem>(targetItemClass, vectors[i], FRotator(0, 0, 0), FActorSpawnParameters())
if (item)
{
    UE_LOG(LogTemp, Warning, TEXT("Actor spawned !");
}

For now i get this error when i try to spawn: SpawnActor failed because Class is not an actor class

I’m working on a plugin and i get the path from the right click “Get reference”

Thanks in advance for your help.

static ConstructorHelpers::FClassFinder<AKryptos_TargetItem> ItemClass(TEXT("/Game/Kryptos/BP/Targets/BP_TargetItem"));
if (ItemClass.Class != NULL)
{
targetItemClass= ItemClass.Class;
}

Edit: sry just saw you do a Plugin you might not need “/Game/” in your Path =)

Thanks worked perfectly !

Hi,

After packaging my game i get an error (for the two classes where i used your solution) on launch.

It work fine in-editor or in-editor standalone.

Error: CDO Constructor (Kryptos_TargetDisplayer): Failed to find /Kryptos/BP/Targets/BP_TargetItem.BP_TargetItem_C

Can you give me a Up-to-date snippet of your Code please?

I tried to package the plugin in a new project.
Still the same issue, and a lot of warning disapeared, but i have still a lot.

I will try to solve them before maybe it will fix the issue.

I was able to fix all the warning and my CDO Constructor issue by simply changing values to:

[pluginName].uplugin file

"Type": "Runtime", "LoadingPhase": "PreDefault"

I set the post back to solved, thanks again for your quick reply !

Hi,
I had the same problem, and I found two possible solutions.

  1. If your actor is created in the Unreal Editor, then you can simply spawn it by code like this:

    UClass* MyItemBlueprintClass = StaticLoadClass(UObject::StaticClass(), NULL, TEXT("/Game/Weapons/axes/DoubleAxeActor.DoubleAxeActor_C"), NULL, LOAD_None, NULL);
    FActorSpawnParameters SpawnInfo;
    SpawnInfo.Owner = this;
    SpawnInfo.Instigator = Instigator;
    ApsItemActor* obj = spawnManager->currentWorld->SpawnActor(MyItemBlueprintClass, newlocation, GetActorRotation(), SpawnInfo);

The pre-requisite is that your actor is replicated. Please note that actors deriving from AActor are not replicated by default, so you need to add in their constructor:

bReplicates = true;

or within the editor you should flag your actor as “Replicates”. It is one of the properties in the details panel.

  1. If you want to create your actor completely within C++, and you have only the StaticMesh and the Material in the Editor (this was my case), you can create it like this:

    FActorSpawnParameters SpawnInfo;
    SpawnInfo.Owner = this;
    SpawnInfo.Instigator = Instigator;
    ApsItemActor* obj = GetWorld()->SpawnActor(ApsItemActor::StaticClass(), newlocation, GetActorRotation(), SpawnInfo);

    UStaticMeshComponent* MyMeshComponent = NewObject(obj, UStaticMeshComponent::StaticClass(), TEXT(“Mesh”));

    UStaticMesh* MeshAsset = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT(“StaticMesh’/Game/Weapons/axes/doubleaxe02abc.doubleaxe02abc’”)));
    UMaterial* MaterialAsset = Cast(StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT(“Material’/Game/Weapons/axes/doubleaxe02c_Mat.doubleaxe02c_Mat’”)));

    MyMeshComponent->SetStaticMesh(MeshAsset);
    MyMeshComponent->SetMaterial(0, MaterialAsset);
    MyMeshComponent->SetWorldLocation(newlocation);
    MyMeshComponent->SetIsReplicated(true);

    MyMeshComponent->RegisterComponent();
    obj->AddOwnedComponent(MyMeshComponent);
    obj->SetRootComponent(MyMeshComponent);

Also in this case the pre-requisite is that your actor is replicated. Same case for case 1.

If your actor is invisible client side, means you didn’t replicate it, or you didn’t set the position properly. Please note the SetWorldLocation call despite the fact you are already giving the location in the spawnactor command. For me it works only if I call explicitely SetWorldLocation