I am working through one of the Udemy courses and am getting results inconsistent with what is being presented. I have copied the code directly from the instructor in an effort to resolve this problem, so presumably this all should be working. I am trying to place pawns in the world at runtime. This is working perfectly fine with regular actors (the PlaceActor() function works) but for some reason when I try the same function with a Pawn, I get a nullptr. Specifically, even when ToSpawn is NOT a nullptr and is populated with expected values, GetWorld()->SpawnActor< APawn>(ToSpawn) returns a nullptr. I can’t tell if this is due to updates to the engine or if some unexpected value is being set erroneously.
void ATile::PlaceActor(TSubclassOf<AActor> ToSpawn, const FSpawnPosition SpawnPosition)
{
AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
Spawned->SetActorRelativeLocation(SpawnPosition.Location);
Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
Spawned->SetActorRotation(FRotator(0, SpawnPosition.Rotation, 0));
Spawned->SetActorScale3D(FVector(SpawnPosition.Scale));
}
void ATile::PlaceAIPawn(TSubclassOf<APawn> ToSpawn, const FSpawnPosition SpawnPosition)
{
APawn* Spawned = GetWorld()->SpawnActor<APawn>(ToSpawn);
Spawned->SetActorRelativeLocation(SpawnPosition.Location);
Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
Spawned->SetActorRotation(FRotator(0, SpawnPosition.Rotation, 0));
Spawned->SpawnDefaultController();
Spawned->Tags.Add(FName("Enemy"));
}