C++ Can you spawn from a blueprint without it being assigned?

My game doesn’t need a character, it doesn’t make sense for the game type. So I need to spawn stuff from the PlayerController.

The pattern I have requires that a Blueprint be assinged to a class in the Editor, but in the examples PlayerController doesn’t get a blueprint, only character and it’s messy to assign to the chracter and then make the PlayerController grab it from the character.

Can you just get a blueprint by name? like in UE3 how you could assign things in the defaultproperties.

I think I need the UClass* of the blueprint I want to spawn. How would I get that from C++ if it is an asset?

Can you provide more detail on what you are trying to do? I’m sure it’s possible, but it’s hard to give the right info since I’m not clear what it is you want to do.

You can Blueprint a PlayerController and the class of a Blueprint should be the Blueprint’s name with _C added to the end.

So, instead of gettting the Character, I’d like to just straight spawn from a blueprint.

From the Editor copying the reference to clipboard yields: Blueprint’/Game/MyLaserAttractor.MyLaserAttractor’

I’d like to use that string to spawn from in the PlayerController so I don’t have to mess with the character. I set the mesh in the Character to None because I’m not using it as a pawn.

if(MyCharacter != NULL && MyCharacter->LaserAttractorBlueprint != NULL)
{   //Spawninfo
	FActorSpawnParameters SpawnInfo;

	SpawnInfo.Owner = this;

	LaserAttractor = GetWorld()->SpawnActor(MyCharacter->LaserAttractorBlueprint, MyCharacter->GetActorLocation(), MyCharacter->GetActorRotation(),SpawnInfo );
	UE_LOG(LogCatSniper, Log, TEXT("LaserAttractor spawned"));

Dear Michael,

There’s actually an easy way to do this :slight_smile:

Using a Blue Printed Player Controller Class

I use a blueprinted player controller class in my game

You set it up in the gameinfo, no character involvement required

gameinfo .cpp constructor

AVictoryGameGameInfo::AVictoryGameGameInfo(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)

//backup in case retrieving the BP fails
PlayerControllerClass = AVictoryGamePlayerController::StaticClass();
	static ConstructorHelpers::FObjectFinder VictoryPCOb(TEXT("Blueprint'/Game/VictoryEditor/VictoryPlayerControllerBP.VictoryPlayerControllerBP'"));
	if (VictoryPCOb.Object != NULL)
		PlayerControllerClass = (UClass*)VictoryPCOb.Object->GeneratedClass;

Now when you start the game the PC is auto-spawned from your BP

I heavily rely on this setup in my game, since I set a lot of properties on my PC blueprint, and it works flawlessly (thanks to Epic’s awesome programmers and blueprint system!)


Removing the Character entirely

You can easily avoid spawning any character (no matter even if a character BP is assigned to the defaults) by editing your game info

in your game info,


virtual void RestartPlayer(class AController* NewPlayer) OVERRIDE;

in the .cpp

void YourGameinfo::RestartPlayer(class AController* NewPlayer)
    //do not spawn player character

**To spawn Blueprints**

Create the blueprint class property in your main class that you are operating from


    (UClass*) WarriorBP;

**.cpp of your main active class**

    //Warrior BP, you only need to do this once
    	static ConstructorHelpers::FObjectFinder WarriorOb(TEXT("Blueprint'/Game/Blueprints/WarriorBP.WarriorBP'"));
    	if (WarriorOb.Object != NULL)
    		WarriorBP = (UClass*)WarriorOb.Object->GeneratedClass;


**spawning the  BP:**

        if(!WarriorBP) return;

    	FActorSpawnParameters SpawnInfo;
    	//SpawnInfo.bNoCollisionFail = true;
    	SpawnInfo.Owner = this;  //whatever class we're in, I'm assuming an AActor class
    	//spawn creature
    	AWarriorBaseClass* NewCreature = 
    		GetWorld()->SpawnActor(WarriorBP, TheLoc, TheRot,SpawnInfo );

**By base class I mean the class the BP you are using was created from** 

This is the method I use anyways, so I know this works :)



Thanks Rama, this was what I was looking for how to do.

Rama always on point.