Can't spawn ACharacter c++

Hey :wave:
Could anyone explain how to spawn ACharacter actor in c++?

World->SpawnActor(AActor::StaticClass(), nullptr, Parameters) works
World->SpawnActor(APawn::StaticClass(), nullptr, Parameters) works
World->SpawnActor(ACharacter::StaticClass(), nullptr, Parameters) is null

I looked in doc, read forums (here are at least three questions and no one was solved) and I couldn’t find anything. Seems like no one spawns character in c++.

I have custom Character class let it be ASomeCustomCharacter. Creating BP from it and spawning directly on level in editor works fine. Spawning same BP from PlayerController BP also works fine, but not in c++.
I saw one assumption that workable way to set Character BP as property and pass it as TSubclassOf<ASomeCustomCharacter>
World->SpawnActor(BlueprintClassesProvider->SomeCustomCharacterClass, nullptr, Parameters)

but result the same.

Is your spawn method set to :

ESpawnActorCollisionHandlingMethod::AlwaysSpawn

Else you risk not spawning anything if collision collides.

This is how GameModeBase spawns the default Pawn / Character:

APawn* AGameModeBase::SpawnDefaultPawnAtTransform_Implementation(AController* NewPlayer, const FTransform& SpawnTransform)
{
	FActorSpawnParameters SpawnInfo;
	SpawnInfo.Instigator = GetInstigator();
	SpawnInfo.ObjectFlags |= RF_Transient;	// We never want to save default player pawns into a map
	UClass* PawnClass = GetDefaultPawnClassForController(NewPlayer);
	APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(PawnClass, SpawnTransform, SpawnInfo);
	if (!ResultPawn)
	{
		UE_LOG(LogGameMode, Warning, TEXT("SpawnDefaultPawnAtTransform: Couldn't spawn Pawn of type %s at %s"), *GetNameSafe(PawnClass), *SpawnTransform.ToHumanReadableString());
	}
	return ResultPawn;
}

You can indeed store the class of your BP derived class in a property , so that you can use the BP class in the spawn logic.

FTransform SpawnTransform; /// Set the correct location.
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(CustomClassProvider->GetCustomCharacterClass(), SpawnTransform, SpawnInfo);
1 Like

Oh man, you made my day. The problem was exactly with the collision.
Thanks a lot!