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"

How to spawn a Blueprint Actor in C++?


I know there are a bunch of questions like this, but can't find a way to solve this issue.

I have a AWeapon C++ class that I used to create a Blueprint (Child) for every weapon I wanted to implement in the game.

I'd like to spawn the Pistol Blueprint inside the BeginPlay() function inside of the PlayerCharacter class.

I heard about

static ConstructorHelpers::FObjectFinder PistolBP(TEXT("Blueprint'/Game/FirstPersonCPP/Blueprints/Pickup/Pistol'"));

But can't understand how/where to use this template function.

Some of them say that should be put into a Constructor.

Please, I need help.

Product Version: UE 4.10
more ▼

asked Dec 14 '15 at 07:57 PM in C++ Programming

avatar image

127 38 37 51

avatar image AnimalMachine Dec 15 '15 at 03:54 PM

Is PlayerCharacter a C++ class or a blueprint?

avatar image gedamial Dec 15 '15 at 03:55 PM

C++ class

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

2 answers: sort voted first

Yes, it needs to go inside the constructor.

You probably already have a reference to the pistol actor in your class. If not, you can declare it in the header file like this:

 TSubclassOf<class AActor> MyPistol;

In the constructor, you need to tell FObjectFinder what type of class to expect, in this case UBlueprint. Then spawn an actor with the found object as template. You might want to add a check to make sure the object was actually found (in case you ever change the path):

 static ConstructorHelpers::FObjectFinder<UBlueprint> PistolBP(TEXT("Blueprint'/Game/FirstPersonCPP/Blueprints/Pickup/Pistol'"));
 if (PistolBP)
     MyPistol = GetWorld()->SpawnActor(PistolBP.Object->GetClass());

Actually, for Blueprints the path might have to look like this: Blueprint'/Game/FirstPersonCPP/Blueprints/Pickup/Pistol.Pistol'

You can also use the ClassFinder directly, which works pretty similar to the ObjectFinder:

 static ConstructorHelpers::FClassFinder<UBlueprint> PistolBP(TEXT("Blueprint'/Game/FirstPersonCPP/Blueprints/Pickup/Pistol'"));
 if (PistolBP)
     MyPistol = GetWorld()->SpawnActor(PistolBP.Class);
more ▼

answered Dec 15 '15 at 09:18 AM

avatar image

1.8k 69 28 114

avatar image gedamial Dec 15 '15 at 03:36 PM

Actually, the Pistol blueprint inherits from Weapon_Base BP which inherits from AWeapon C++ class wich inherits from Actor.

Since I have an Array of **AWeapon**s, how do I add the Pistol Blueprint found into that array?

The SpawnActor spawns an Actor, not a AWeapon :S

avatar image erinacea Dec 16 '15 at 09:02 AM

Right, sorry. You need to cast the spawned actor to the type of the variable.

 MyPistol = Cast<AWeapon>(GetWorld()->SpawnActor(PistolBP.Class))

avatar image gedamial Dec 16 '15 at 02:05 PM

I did this:

 TSubclassOf<class AWeapon*> InitialPistol;

alt text

But it gives these errors alt text

2.png (16.5 kB)
1.png (50.8 kB)
avatar image gedamial Dec 17 '15 at 07:43 PM

@erinacea any tip?

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

Okay, from what I gather is that you have a c++ PlayerCharacter class and that you setup your weapons as a c++ AWeapon class that you then create blueprints for.

Why not just create a member in the PlayerCharater class and expose it to blueprints. Then create a blueprint for PlayerCharacter, and then set the weapons there.

For example, in PlayerCharacter:

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Guns, meta=(AllowPrivateAccess = "true"))
 class UClass* Weapon;

Then when you have BP_PlayerCharacter, you should see the property in the editor window.

If it is really something you need to spawn, then do something like:

 FVector SpawnLocation = GetActorLocation();
 UWorld* const World = GetWorld();
 if (World != NULL) {
     if (Weapon) {
     World->SpawnActor<AWeapon>(Weapon, SpawnLocation, GetActorRotation());

This is basically what I do for weapon projectiles.

more ▼

answered Dec 15 '15 at 04:04 PM

avatar image

71 1 4 8

avatar image gedamial Dec 15 '15 at 04:17 PM

In the Inventory (array) of AWeapon I can't add the Pistol created using this method.

alt text

1.png (61.3 kB)
(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