How to spawn a Blueprint Actor in C++?

Hello.

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.

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);
}

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 AWeapons, how do I add the Pistol Blueprint found into that array?

The SpawnActor spawns an Actor, not a AWeapon :S

Is PlayerCharacter a C++ class or a blueprint?

C++ class

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.

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

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

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

I did this:

TSubclassOf<class AWeapon*> InitialPistol;

But it gives these errors

@ any tip?