void UGameInstance::DebugRemovePlayer(int32 ControllerId) { #if !UE_BUILD_SHIPPING ULocalPlayer* const ExistingPlayer = FindLocalPlayerFromControllerId(ControllerId); if (ExistingPlayer != NULL) { RemoveLocalPlayer(ExistingPlayer); } #endif }
void UAmethystGameInstance::HandleControllerConnectionChange(bool bIsConnection, int32 Unused, int32 GameUserIndex) { if (!bIsConnection) { // Controller was disconnected // Find the local player associated with this user index ULocalPlayer * LocalPlayer = FindLocalPlayerFromControllerId(GameUserIndex); if (LocalPlayer == NULL) { return; // We don't care about players we aren't tracking } // Invalidate this local player's controller id. LocalPlayer->SetControllerId(-1); } }
ULocalPlayer* UGameInstance::CreateLocalPlayer(int32 ControllerId, FString& OutError, bool bSpawnActor) { checkf(GetEngine()->LocalPlayerClass != NULL); ULocalPlayer* NewPlayer = NULL; int32 InsertIndex = INDEX_NONE; const int32 MaxSplitscreenPlayers = (GetGameViewportClient() != NULL) ? GetGameViewportClient()->MaxSplitscreenPlayers : 1; if (FindLocalPlayerFromControllerId( ControllerId ) != NULL) { OutError = FString::Printf(TEXT("A local player already exists for controller ID %d,"), ControllerId); } else if (LocalPlayers.Num() < MaxSplitscreenPlayers) { // If the controller ID is not specified then find the first available if (ControllerId < 0) { for (ControllerId = 0; ControllerId < MaxSplitscreenPlayers; ++ControllerId) { if (FindLocalPlayerFromControllerId( ControllerId ) == NULL) { break; } } check(ControllerId < MaxSplitscreenPlayers); } else if (ControllerId >= MaxSplitscreenPlayers) { UE_LOG(LogPlayerManagement, Warning, TEXT("Controller ID (%d) is unlikely to map to any physical device, so this player will not receive input"), ControllerId); } NewPlayer = NewObject<ULocalPlayer>(GetEngine(), GetEngine()->LocalPlayerClass); InsertIndex = AddLocalPlayer(NewPlayer, ControllerId); if (bSpawnActor && InsertIndex != INDEX_NONE && GetWorld() != NULL) { if (GetWorld()->GetNetMode() != NM_Client) { // server; spawn a new PlayerController immediately if (!NewPlayer->SpawnPlayActor("", OutError, GetWorld())) { RemoveLocalPlayer(NewPlayer); NewPlayer = NULL; } } else { // client; ask the server to let the new player join NewPlayer->SendSplitJoin(); } } } else { OutError = FString::Printf(TEXT( "Maximum number of players (%d) already created. Unable to create more."), MaxSplitscreenPlayers); } if (OutError != TEXT("")) { UE_LOG(LogPlayerManagement, Log, TEXT("UPlayer* creation failed with error: %s"), *OutError); } return NewPlayer; }
void UAmethystGameInstance::HandleControllerPairingChanged(int GameUserIndex, const FUniqueNetId& PreviousUser, const FUniqueNetId& NewUser) { if (CurrentState == AmethystGameInstanceState::WelcomeScreen) { // Don't care about pairing changes at welcome screen return; } #if Amethyst_CONSOLE_UI && PLATFORM_XBOXONE if (IgnorePairingChangeForControllerId != -1 && GameUserIndex == IgnorePairingChangeForControllerId) { // We were told to ignores IgnorePairingChangeForControllerId = -1; // Reset now so there there is no chance this remains in a bad state return; } if (PreviousUser.IsValid() && !NewUser.IsValid()) { // Treat this as a disconnect or signout, which is handled somewhere else return; } if (!PreviousUser.IsValid() && NewUser.IsValid()) { // Treat this as a signin ULocalPlayer * ControlledLocalPlayer = FindLocalPlayerFromControllerId(GameUserIndex); if (ControlledLocalPlayer != NULL && !ControlledLocalPlayer->GetCachedUniqueNetId().IsValid()) { // If a player that previously selected "continue without saving" signs into this controller, move them back to welcome screen HandleSignInChangeMessaging(); } return; } // Find the local player currently being controlled by this controller ULocalPlayer * ControlledLocalPlayer = FindLocalPlayerFromControllerId(GameUserIndex); // See if the newly assigned profile is in our local player list ULocalPlayer * NewLocalPlayer = FindLocalPlayerFromUniqueNetId(NewUser); // If the local player being controlled is not the target of the pairing change, then give them a chance // to continue controlling the old player with this controller if (ControlledLocalPlayer != nullptr && ControlledLocalPlayer != NewLocalPlayer) { UAmethystGameViewportClient * AmethystViewport = Cast<UAmethystGameViewportClient>(GetGameViewportClient()); if (AmethystViewport != nullptr) { AmethystViewport->ShowDialog( nullptr, EAmethystDialogType::Generic, NSLOCTEXT("ProfileMessages", "PairingChanged", "Your controller has been paired to another profile, would you like to switch to this new profile now? Selecting YES will sign out of the previous profile."), NSLOCTEXT("DialogButtons", "YES", "A - YES"), NSLOCTEXT("DialogButtons", "NO", "B - NO"), FOnClicked::CreateUObject(this, &UAmethystGameInstance::OnPairingUseNewProfile), FOnClicked::CreateUObject(this, &UAmethystGameInstance::OnPairingUsePreviousProfile) ); } } #endif }