XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Any PlayerIndex for GameState Management tutorial

Last post 07/11/2009 11:58 by UberGeekGames. 3 replies.
  • 06/11/2009 22:03

    Any PlayerIndex for GameState Management tutorial

    Hello, how would I go about getting the gamestate management tutorial to register which controller the gamer is using, and use that one for the rest of the session (as per the guidelines)
  • 06/11/2009 22:13 In reply to

    Re: Any PlayerIndex for GameState Management tutorial

    Use NetworkStateManagement, which does this for you. :)

    Alternatively, create a new GameScreen that is displayed before the main menu. On this screen, do this:
    public static class Control 
        public static PlayerIndex ControllingPlayer = PlayerIndex.One; 
     
     
     
    for(PlayerIndex idx = PlayerIndex.One; idx <= PlayerIndex.Four; idx++) 
        GamePadState gamePad = GamePad.GetState(idx); 
        if(idx.IsButtonDown(Buttons.A)) 
        { 
            Control.ControllingPlayer = idx; 
            this.ExitScreen(); 
        } 

    and then use Control.ControllingPlayer throughout the entire game.
    "No programmer can pick up a TV remote without thinking what it would take to add a stun gun. [...] Their motto is 'if it ain't broke, it doesn't have enough features yet'" - Scott Adams, The Dilbert Principle

    The signature that was too big for the 512 char limit
  • 07/11/2009 0:57 In reply to

    Re: Any PlayerIndex for GameState Management tutorial

    Thanks for that, I must be missing something though. I have no experience with the Game State Management system and am trying to help a friend.

    Anyway the issue is that once the screen changes, I lose control of the controller, and it defaults back to playerindex one, and I have no idea why. Here is my code on the fist screen:

     
    bool HandlePlayerInput(InputState input, PlayerIndex playerIndex)  
            {  
                // Look up inputs for the specified player profile.  
                KeyboardState keyboardState = input.CurrentKeyboardStates[(int)playerIndex];  
                for (PlayerIndex idx = PlayerIndex.One; idx <= PlayerIndex.Four; idx++)  
                {  
                    GamePadState gamePad = GamePad.GetState(idx);  
                    if (GamePad.GetState(idx).Buttons.A == ButtonState.Pressed)  
                    {  
                        ControllingPlayer = idx;  
                        LoadPGScreen(playerIndex);  
                    }  
                }    
     
     
     
                // The game pauses either if the user presses the pause button, or if  
                // they unplug the active gamepad. This requires us to keep track of  
                // whether a gamepad was ever plugged in, because we don't want to pause  
                // on PC if they are playing with a keyboard and have no gamepad at all!  
                bool gamePadDisconnected = !GamePad.GetState(Control.ControllingPlayer).IsConnected &&  
                                           input.GamePadWasConnected[(int)playerIndex];  
     
                if (gamePadDisconnected)  
                {  
                    MessageBoxScreen gamePadDisconectedMessageBox =  
                            new MessageBoxScreen(Resources.GamepadDisconected, false);  
                    ScreenManager.AddScreen(gamePadDisconectedMessageBox, playerIndex);  
                    return false;  
     
                }  
                else 
                {  
                    if (GamePad.GetState(Control.ControllingPlayer).IsConnected)  
                    {  
                        // load PGScreen  
                        if (GamePad.GetState(Control.ControllingPlayer).IsButtonDown(Buttons.A))  
                        {  
                            LoadPGScreen(playerIndex);  
                        }  
                    } 

    and the second:

    bool HandlePlayerInput(InputState input, PlayerIndex playerIndex)  
            {  
                // Look up inputs for the specified player profile.  
                KeyboardState keyboardState = input.CurrentKeyboardStates[(int)playerIndex];  
                GamePadState gamePadState = GamePad.GetState(Control.ControllingPlayer);  
     
                // The game pauses either if the user presses the pause button, or if  
                // they unplug the active gamepad. This requires us to keep track of  
                // whether a gamepad was ever plugged in, because we don't want to pause  
                // on PC if they are playing with a keyboard and have no gamepad at all!  
                bool gamePadDisconnected = !GamePad.GetState(Control.ControllingPlayer).IsConnected &&  
                                           input.GamePadWasConnected[(int)playerIndex];  
     
     
                if (gamePadDisconnected)  
                {  
                    MessageBoxScreen gamePadDisconectedMessageBox =  
                            new MessageBoxScreen(Resources.GamepadDisconected, false);  
                    ScreenManager.AddScreen(gamePadDisconectedMessageBox, playerIndex);  
                    return false;  
     
                }  
                else 
                {  
                    if (gamePadState.IsConnected)  
                    {  
                        if (GamePad.GetState(Control.ControllingPlayer).IsButtonDown(Buttons.A))  
                        {  
                            loadTitleScreen(playerIndex);  
                        }  
                    } 


  • 07/11/2009 11:58 In reply to

    Re: Any PlayerIndex for GameState Management tutorial

    Well the first problem I see is that you're using the built in input handling method, which I've found is much more confusing than writing your own. I would take it out and put as much of the repeating code such as the disconnection check in reuseable methods in the Control class, so you don't have to copy and paste it for each screen. (rule #1 of OOP: if you're using copy and paste, you're doing something wrong ;) )

    The only problem I see is that you're duplicating code in the first code block - take out the "else" statement after the GamePadDisconnected check. Put the playerindex control loop in the Update method too, since IIRC HandlePlayerInput is only called when a controller has input. This could lead to the loss of control that you're experiencing.
    "No programmer can pick up a TV remote without thinking what it would take to add a stun gun. [...] Their motto is 'if it ain't broke, it doesn't have enough features yet'" - Scott Adams, The Dilbert Principle

    The signature that was too big for the 512 char limit
Page 1 of 1 (4 items) Previous Next