x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.16
Tags:
more ▼

asked May 28 '17 at 12:46 PM in C++ Programming

avatar image

gundz
32 4 7 9

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first
 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 =)

more ▼

answered May 28 '17 at 01:04 PM

avatar image

Nachtmahr
6.9k 143 26 172

avatar image gundz May 28 '17 at 04:14 PM

Thanks worked perfectly !

avatar image gundz May 29 '17 at 12:43 PM

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

avatar image Nachtmahr May 29 '17 at 01:45 PM

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

avatar image gundz May 29 '17 at 01:52 PM

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.

avatar image gundz May 29 '17 at 02:14 PM

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 !

(comments are locked)
10|2000 characters needed characters left

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<ApsItemActor>(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.

2) 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>(ApsItemActor::StaticClass(), newlocation, GetActorRotation(), SpawnInfo);

 UStaticMeshComponent* MyMeshComponent = NewObject<UStaticMeshComponent>(obj, UStaticMeshComponent::StaticClass(), TEXT("Mesh"));

 UStaticMesh* MeshAsset = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("StaticMesh'/Game/Weapons/axes/doubleaxe02abc.doubleaxe02abc'")));
 UMaterial* MaterialAsset = Cast<UMaterial>(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

more ▼

answered Apr 02 '18 at 10:38 AM

avatar image

Talad
106 4 8 12

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question