Peculiar Controller Bug (?)

So, I’ve ran into a strange issue indeed. This is my player / AI spawning 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 PC 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 PS4 controllers) all is well.

Anyone got any idea why this might be happening?

Hey mate,

what does your logs tell you?

what exactly is meant by: “it all breaks”

What does this line mean:


FString PCountString = GetWorld()->URL.GetOption(TEXT("PlayerCount="), TEXT("1"));

why do you set PlayerCount to 1 if there are more? (In case this is what this function does, maybe 1 represent a specific option with the playercount you set somewhere?!)

cheers

There is no peculiar output in the log.

It all breaks = none of the controllers work anymore.

That string gets the number of players from the global world URL (i.e. I pass in a PlayerCount variable in my LoadLevel). It just defaults to 1.

is there some NULL when you set up some breakpoints during your init?

During the SpawnCharacters? I haven’t tested, but I don’t see how there could be, I’m checking for nulls along the way. I’ll test it out when I get home…

Well sure you do that,

but what i mean exactly is, are there any values which seems strange if you debug your code with breakpoints.

You said there is no particular log output… I guess you mean, that all your UELogs you set up, are telling exactly what you expected?

I just tested it, everything looks in order. As for the logs, I meant that both my outputs and the engine outputs are nominal. There are no weird errors, nothing.

Threads that go bump in the night.

Can we get a helping hand here? This is the last hurdle thats keeping us from showing this. Any help is highly appreciated.