Determining If an Xbox 360 Controller Is Disconnected (Xbox 360, Windows)

This topic shows you how to detect if a controller is disconnected by checking the IsConnected property of a retrieved GamePadState object. If the value is true, the controller is still connected, and the game continues. If the value is false, the controller is disconnected, and the game will pause until the user reconnects the controller.

Detecting if a Controller Is Disconnected

To detect if a controller is disconnected

  1. At program start, present an interface for users to choose which controllers are connected, and then store this information.

  2. Go to the game loop.

  3. In the game loop, get the state of each Xbox 360 Controller by using GetState.

  4. Retrieve the IsConnected property to verify that each controller from the earlier list of connected controllers is still connected.

    If the current value of IsConnected is false for a previously connected controller, that controller now is disconnected.

  5. Enter a pause loop, and wait for the user to reconnect the controller.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace InputDisconnect
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        // Game states.
        enum GameState
        {
            InputSetup = 0,
            Game,
            Disconnected
        }

        // Current state of the game.
        GameState gameState = GameState.InputSetup;

        // Controllers that are in use.
        bool[] activeControllers = new bool[4];

        // Disconnected controller detected.
        bool disconnectDetected = false;

        // Background color.  Use this to indicate a disconnect.
        Color backColor = Color.CornflowerBlue;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allow the game to exit.
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
                ButtonState.Pressed)
                this.Exit();

            UpdateInput();

            switch (gameState)
            {
                case GameState.InputSetup:
                    // Allow players to join the game, 
                    // and determine active controllers.
                    // In this example, there is only one player.
                    activeControllers[0] = true;

                    // When ready, proceed to the game.
                    gameState = GameState.Game;
                    break;

                case GameState.Game:
                    // If disconnected, go to the disconnect loop.
                    if (disconnectDetected)
                    {
                        backColor = Color.Black;
                        gameState = GameState.Disconnected;
                    }
                    break;

                case GameState.Disconnected:
                    // If reconnected, continue to the game.
                    if (!disconnectDetected)
                    {
                        backColor = Color.CornflowerBlue;
                        gameState = GameState.Game;
                    }
                    // Otherwise, pause the game and display a message.
                    break;
            }

            base.Update(gameTime);
        }

        void UpdateInput()
        {
            disconnectDetected = false;
            PlayerIndex index = PlayerIndex.One;
            for (int i = 0; i < 4; i++, index++)
            {
                if (activeControllers[i] && 
                    !GamePad.GetState(index).IsConnected)
                {
                    disconnectDetected = true;
                }
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(backColor);
            base.Draw(gameTime);
        }
    }
}

See Also

Detecting Xbox 360 Controller Button Presses (Xbox 360, Windows)