Placement 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) (Inside of PlaceAIPawn()) 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::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn, float Radius, float MinScale, float MaxScale)
{
TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, MinScale, MaxScale);
for (FSpawnPosition SpawnPosition : SpawnPositions)
{
PlaceActor(ToSpawn, SpawnPosition);
}
return;
}
void ATile::PlaceAIPawns(TSubclassOf<APawn> ToSpawn, int MinSpawn, int MaxSpawn, float Radius)
{
TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, 1, 1);
for (FSpawnPosition SpawnPosition : SpawnPositions)
{
PlaceAIPawn(ToSpawn, SpawnPosition);
}
return;
}
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); // Problem is here
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"));
}