How to spawn multiple Characters from inside a Gamemode

Hi,

I am trying to get players(Characters) to spawn in the map using the enhanced input system.


void ASportGamemode::StartPlay()
{
	Super::StartPlay();

	// Set Default Pawn Class
	DefaultPawnClass = nullptr;

	TArray<AActor*> FoundActors{};
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), APlayerStart::StaticClass(), FoundActors);

	for (size_t i{0}; i < FoundActors.Num(); ++i)
	{
		// Get Transform of playerStart
		auto transform = FoundActors[i]->GetActorTransform();

		// Spawn a character at the playerStart
		auto* newController = UGameplayStatics::CreatePlayer(GetWorld(), i);
		auto* newCharacter = GetWorld()->SpawnActor<ABaseCharacter>(PlayerCharacterBP, transform);

// Enabling this causes non of my characters to receive input.
		/*if (newController != nullptr && newCharacter != nullptr)
		{

			newController->Possess(newCharacter);
		}*/
	}
}

The issues are as follows:

  • When I start ‘i’ from 0 it complains that there already is a controller 0.

By changing i to i + 1 I can get one controller to move a character, but the other one gets stuck

auto* newController = UGameplayStatics::CreatePlayer(GetWorld(), i + 1);

Does anyone know what’s causing this?

// ABaseCharacter
void ABaseCharacter::BeginPlay()
{
	Super::BeginPlay();

	if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(CharacterMappingContext, 0);
		}
	}
}

// Called to bind functionality to input
void ABaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
	{
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ABaseCharacter::Move);
	}
}