[Bug] 3+ Controllers Don't Work

Heya,

As detailed here: Peculiar Controller Bug (?) - C++ - Epic Developer Community Forums

Using more than 2 controllers in my game (controller spawning code posted there) breaks even the previous two. I’ve tried pretty much everything and nothing works, any sort of assistance would be greatly appreciated.

Best regards,
Damir H.

EDIT () from the linked forum post:

"So, I’ve ran into a strange issue indeed. This is my player / AI spawning code:

Code:

void AMyGameMode::SpawnCharacters()
{
	FString PCountString = GetWorld()->URL.GetOption(TEXT("PlayerCount="), TEXT("1"));

	UE_LOG(LogTemp, Log, TEXT("Option String: %s %s"), *GetWorld()->URL.ToString(), *PCountString);
	NumberOfPlayers = FCString::Atoi(*PCountString);

	FActorSpawnParameters Params;
	Params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

	CharOne= GetWorld()->SpawnActor<ACharOneCharacter>(CharOneClass, CurrentCheckPoint->GetActorLocation() + FVector(0, 0, 88.f), FRotator::ZeroRotator, Params);
	CharTwo = GetWorld()->SpawnActor<ACharTwoCharacter>(CharTwoClass, CurrentCheckPoint->GetActorLocation() + FVector(0, 0, 88.f), FRotator::ZeroRotator, Params);
	CharThree= GetWorld()->SpawnActor<ACharThreeCharacter>(CharThreeClass, CurrentCheckPoint->GetActorLocation() + FVector(0, 0, 88.f), FRotator::ZeroRotator, Params);
	CharFour= GetWorld()->SpawnActor<ACharFourCharacter>(CharFourClass, CurrentCheckPoint->GetActorLocation() + FVector(0, 0, 88.f), FRotator::ZeroRotator, Params);



	UE_LOG(LogTemp, Log, TEXT("There are %d Player(s)."), NumberOfPlayers);

	for (int i = 0; i < NumberOfPlayers; i++)
	{
		//SpawnPlayer();
		UE_LOG(LogTemp, Log, TEXT("Spawning a player..."));

		APlayerController* PController = UGameplayStatics::GetPlayerController(this, i);
		if (PController == nullptr)
		{
			PController  = UGameplayStatics::CreatePlayer(this, i);
		}
		
		AMyPlayerControllerBase* HPC = Cast<AMyPlayerControllerBase>(PController);
		if (HPC != nullptr)
		{
			UE_LOG(LogTemp, Log, TEXT("Spawned player %s"), *HPC->GetName());
			HPC->AssignedControllerID = i;
			HPC->PossessFirstAvailableCharacter();
			
			UE_LOG(LogTemp, Log, TEXT("Assigning controller ID %d"), HPC->AssignedControllerID);
		}
		else
		{
			if (PController == nullptr)
			{
				UE_LOG(LogTemp, Error, TEXT("Player didn't spawn."));
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Wrong class."));
			}
			
		}
	}

	SetAIIfNotController(CharOne);
	SetAIIfNotController(CharTwo);
	SetAIIfNotController(CharThree);
	SetAIIfNotController(CharFour);

	
}

void AMyGameMode::SetAIIfNotController(AMyBaseChar* Char) 
{
	AMyPlayerControllerBase* HPC = Cast<AMyPlayerControllerBase>(Char->GetController());
	if (HPC != nullptr)
	{
		UE_LOG(LogTemp, Log, TEXT("Character %s is player controlled."), *Char->GetName());
		return;
	}

	Char->SpawnDefaultController();
	AAllyAIControllerBase* AIController = Cast<AAllyAIControllerBase>(Char->GetController());

	if (AIController != nullptr && AIController->DefaultBehaviorTree != nullptr)
	{
		AIController->RunBehaviorTree(AIController->DefaultBehaviorTree);
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("The AI Controller of %s is null!"), *Char->GetName());//a
	}
}

"(I’ve renamed some classes and variables since the project isn’t public yet).

"Long story short, one controller works, two controllers work, but the moment 3 controllers get plugged in, it all breaks. Note that I always start a 4 player game, so there are no AI. As long as it’s <3 controllers plugged in (I use DS4Windows and controllers) all is well.

“Anyone got any idea why this might be happening?”

Hey -

Do your controllers no work in a new project if you spawn characters through the level editor? Let me know if setting up the Level Blueprint to match the screenshot below allows you to control your characters in both a new project as well as your current project using the default GameMode and your custom GameMode. This should spawn 4 players when the game starts and each one is given a different controller ID. The -1 references the next unused controller, you could also wire the Index pin into the Controller ID pin for the same effect.

Cheers

Hey there ,

We’ve managed to solve the issue just in time for a big deadline, thanks for the BP suggestion. The fix was basically, instead of the default pawn being the SpectatorPawn (and the controllers PossessFirstAvailableCharacter() making it jump into the right pawn), the default pawn is a custom made NoCharacter pawn, which delays by 0.5s and then calls PossessFirstAvailableCharacter().

I am not sure whether the pawn class change or delay is responsible for the fix. Why 2 controllers worked but not 3 is anyone’s guess, order of execution is an arcane magic as far as I’m concerned.