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 Blueprints from C++

Hello everyone,

I already searched the whole day on how to spawn a Blueprint from C++ and I have found a few answers. However, nothing described there works for me, maybe because the answers are already a few month old.

It would be very nice if someone could give me an example on how to do it. Thanks a lot!

Product Version: Not Selected
Tags:
more ▼

asked Feb 16 '15 at 12:09 AM in C++ Programming

avatar image

holzlag0r
39 10 11 14

avatar image Shadowriver Feb 16 '15 at 12:40 AM

What methods did you use and why they didn't work

avatar image BatavianIsmail Feb 16 '15 at 01:29 AM

Did you already try using ConstructorHelpers::FClassFinder, then use GetWorld()->SpawnActor to spawn your blueprint? What blueprint did you actually want to spawn? An actor, component, or UObject? You can also put this into consideration when you want to access your blueprint

avatar image holzlag0r Feb 16 '15 at 02:33 PM

As far as I know, I can only use ConstructorHelpers in my Constructor, right? But I want to spawn them outside of the Constructor. My blueprint was created from a custom class, which is an AActor.

avatar image MickD777 Feb 16 '15 at 02:26 AM

On what version of the engine are you? I do it often by exposing a TSubclassOf public property and then using GetWorld()->SpawnActor or the spawning function of UGameplayStatics.

avatar image BatavianIsmail Feb 16 '15 at 02:51 AM

I use 4.6. The ConstructorHelpers::FClassFinder is used to get the class of the blueprint I want to spawn. Then, we pass the class as argument to the SpawnActor. If you already use the TSubClassOf and set the value of the variable, and set the ActorSpawnParameters, it should be spawned. Maybe the location of spawn is far from your Character.

avatar image MickD777 Feb 16 '15 at 02:07 PM

I was asking the one who asked the question. I know how to spawn a Blueprint, was just trying to help out :)

avatar image BatavianIsmail Feb 17 '15 at 01:39 AM

My mistake.. I thought that you are the one who ask the question. I didn't see the username, first. My bad.., sorry :)

avatar image holzlag0r Feb 16 '15 at 02:28 PM

I am using 4.6. Could you maybe provide a short example on how to I already tried using TSubclassOf, but I didn't get it to work:


.h

 /** Blueprint Test Spawn */
 UPROPERTY(EditDefaultsOnly, Category = Mesh)
 TSubclassOf<class AAttachment> Blueprint;



.cpp

 GetWorld()->SpawnActor<AAttachment>(Blueprint, GetActorLocation(), GetActorRotation(), FActorSpawnParameters());


(This even leads to an editor crash - Access violation)

avatar image MickD777 Feb 16 '15 at 03:02 PM

Here is an example of how I do it :

 void UFXManager::SpawnCameraBasedActors(TSubclassOf<AActor> aActorToSpawn, FVector aSpawnLocation)
 {
     FVector cameraLocation = UJotunnMain::Instance->GetCamera()->GetCameraPosition();
 
         FTransform spawnTransform;
         spawnTransform.SetComponents(FRotator::ZeroRotator.Quaternion(), aSpawnLocation, FVector(1.0f, 1.0f, 1.0f));
 
         AActor* spawnedActor = UGameplayStatics::BeginSpawningActorFromClass(UJotunnMain::Instance->GetWorld(), aActorToSpawn, spawnTransform);
         UGameplayStatics::FinishSpawningActor(spawnedActor, spawnTransform);
 }

Hope this helps.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Alright, thanks to your help I got it working!


Omega3Pawn.cpp

 void AOmega3Pawn::SetAttachmentForSlot(int Slot, TSubclassOf<AAttachment> &Attachment)
 {
     if (Slots[Slot] != nullptr)
     {
         UE_LOG(LogOmega, Log, TEXT("Destroying %s in slot %d"), *Slots[Slot]->GetName(), Slot);
         Slots[Slot]->Destroy();
     }
 
     Slots[Slot] = GetWorld()->SpawnActor<AAttachment>(Blueprint, FVector(400.f, 200.f, 0.f), FRotator(0.f, 0.f, 0.f), FActorSpawnParameters());
     Slots[Slot]->AttachRootComponentTo(RootComponent);
     UE_LOG(LogOmega, Log, TEXT("Spawned %s in slot %d"), *Slots[Slot]->GetName(), Slot);
 }

Omega3Pawn.h

 /** Blueprint to Spawn */
 UPROPERTY(EditDefaultsOnly, Category = Attachments)
 TSubclassOf<class AAttachment> Blueprint;
 public:
     /** Set Attachment for Slot */
     void SetAttachmentForSlot(int Slot, TSubclassOf<AAttachment> &Attachment);
 
 private:
     /** Blueprint Slots */
     AAttachment* Slots[2];

Now I can just call

     SetAttachmentForSlot(1, Blueprint);

and it works!

Thank you very much! (:

more ▼

answered Feb 16 '15 at 03:44 PM

avatar image

holzlag0r
39 10 11 14

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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