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?”