Hi there, I am trying to do a Connect 4 game. I want the 2 players to have the same input keys to play. I Achieved to create the players and spawn them in the world.
I can play as Player1 without any issues but when I use the “SwapPlayerControllers” method and play as Player2, I got a strange problem. When I move the token left or right, the input seems to be triggered twice.
I think I made it wrong. What is the best way to change the active player in my case ?
Here is some code of my GameMode class :
void APuissance4GameMode::InitFirstPlayer()
{
UWorld *World = GetWorld();
Player1 = Cast<AP4Pawn>(UGameplayStatics::GetPlayerPawn(World, 0));
if (IsValid(Player1))
{
//TPP1 is a TargetPoint placed in the editor
Player1->SetActorTransform(TPP1->GetActorTransform());
Player1->EnableInversedControls();
}
}
void APuissance4GameMode::SpawnSecondPlayer()
{
UWorld *World = GetWorld();
APlayerController *PC=UGameplayStatics::CreatePlayer(World, 1, true);
Player2 = Cast<AP4Pawn>(UGameplayStatics::GetPlayerPawn(World, 1));
if (IsValid(Player2))
{
//TPP2 is a TargetPoint placed in the editor
Player2->SetActorTransform(TPP2->GetActorTransform());
}
}
void APuissance4GameMode::SwitchPlayer()
{
APlayerController *OldPC = Cast<APlayerController>(CurrentPlayer->GetController());
if (CurrentPlayer == Player1)
{
CurrentPlayer = Player2;
}
else if (CurrentPlayer == Player2)
{
CurrentPlayer = Player1;
}
APlayerController *Controller = Cast<APlayerController>CurrentPlayer->GetController());
SwapPlayerControllers(OldPC, Controller);
}