Download

How do I spawn a BP class of Pawn instead of the original static mesh?

Hey there,

So I have a class here that spawns my UE4 ship.


AFlightPawn::AFlightPawn(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UStaticMesh>ShipMesh;
		FConstructorStatics() : ShipMesh(TEXT("/Game/Ships/Engineer.Engineer")){}
	};

	static FConstructorStatics ConstructorStatics;
	ShipMeshComp = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ShipMesh"));
	ShipMeshComp->SetStaticMesh(ConstructorStatics.ShipMesh.Get());
	RootComponent = ShipMeshComp;

The problem is that the static mesh in my game/ships folder is exactly that, just a static mesh. I have a Blueprint version of this ship in the blueprint folder that I would like to spawn instead.

I read another post on here and tried changing the contructor struct to this:


struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinderOptional<UBlueprint>ShipMesh;
		FConstructorStatics() : ShipMesh(TEXT("Blueprint'/Game/Blueprints/Engineer.Engineer'")){}
	};

The problem is that just below that I have


static FConstructorStatics ConstructorStatics;
	ShipMeshComp = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ShipMesh"));
	ShipMeshComp->SetStaticMesh(ConstructorStatics.ShipMesh.Get());
	RootComponent = ShipMeshComp;

where I get the error that SetStaticMesh is for static meshes now and not for blueprints, which I’m setting above.

So my question is how can I spawn a blueprint class of my ship in C++?

Thanks!

This will load a blueprint from a folder location, however you could also change it in your game if you have a blueprint of the class which holds the blueprint variable.

Header


	UPROPERTY(Transient, Replicated, EditAnywhere, Category = "Blueprints")
		TSubclassOf<class AActor> SomeBP;

CPP


static ConstructorHelpers::FObjectFinder<UBlueprint> SomeBluePrint(TEXT("Blueprint'/Game/Blueprints/Some_BP.Some_BP'"));
	if (SomeBluePrint.Object != NULL)
	{
		SomeBP= (UClass*)SomeBluePrint.Object->GeneratedClass;
	}

Then to spawn the actor in game using that blueprint:

YourShipClassObj = World->SpawnActor<AYourShipClass>(SomeBP, StartingPosition, StartingRotation);