Spawning A Character at Run-Time in C++

Hi All,

I want to spawn characters at run-time in C++.I use UE4.7. I find information about spawning actors at run-time in C++.But i can’t information about spawning characters.How can i do that? Please help,
Yours sincerely

A character is an actor so I’m pretty sure you should be able to spawn a character the same way. Did you have problems when trying it?

This is how I would spawn an actor or derivative of actor.

auto MyCharacter = World->SpawnActor(ATypeOfActor::StaticClass());

templated version :

auto MyCharacter = World->SpawnActor< ATypeOfActor >(ATypeOfActor::StaticClass());

You don’t have to use the templated version if you don’t want to. It’s just doing the cast for you. The non-templated version returns AActor *.

Just a note the Templated Version does not need the UClass parameter. Its just there if you want to Spawn a Child but get a ParentClass as return.

Thanks for your reply Alex.When i try to apply your snippet,i can see the character in the world outliner at play but i can not see it at the scene.

Ah good to know. Thanks!

I believe that version of the function spawns it at the origin. Have you tried ejecting while in PIE then selecting it in the outliner and focusing on it? It might be hiding.

Here’s a signature for SpawnActor that takes in a location :

AActor* SpawnActor( UClass* InClass, FVector const* Location=NULL, FRotator const* Rotation=NULL, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() );

Hİ Alex,

I tried to spawn object but the object that is spawned, is blueprint.
Thnks for your help

It’s possible to spawn a blueprint from C++. To do that you need to pass it a member of TSubclassof of then expose it to the editor as a UPROPERTY. Select the blueprint you wish to spawn in the editor then in code pass in the TSubclassof object as the parameter and it will spawn the blueprint version you selectred.

Here’s the documentation

And here’s a usage example (there may be a better way to do this) :

auto NewBullet = World->SpawnActor< ALuneBullet >(BulletType);

AStraighBullet is the subclass and BulletType is a TSubClassOfObject holding a reference to the blueprint asset. You could simply put in AActor instead if you don’t have a more specific code class. Here’s how it’s declared.

UPROPERTY(Category = LuneWeapon, EditDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = “true”))
TSubclassOf< ALuneBullet > BulletType;