TAGS :Viewed: 12 - Published at: a few seconds ago

[ InputeState method parameters ]

Looking over some of the inputs for the GSMkit for XNA, I've noticed that some of the booleans have two parameters, while others only have one. Why? Is there a difference between the two?

Here are two examples, followed by a link to the entire code.

        /// <summary>
    /// Checks for a "pause the game" input action.
    /// The controllingPlayer parameter specifies which player to read
    /// input for. If this is null, it will accept input from any player.
    /// </summary>
    public bool IsPauseGame(PlayerIndex? controllingPlayer)
    {
        PlayerIndex playerIndex;

        return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
    }


    /// <summary>
    /// Checks for a "menu cancel" input action.
    /// The controllingPlayer parameter specifies which player to read input for.
    /// If this is null, it will accept input from any player. When the action
    /// is detected, the output playerIndex reports which player pressed it.
    /// </summary>
    public bool IsMenuCancel(PlayerIndex? controllingPlayer,
                             out PlayerIndex playerIndex)
    {
        return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
    }

Full InputState Code

Answer 1


The first function tells you that a player pressed the pause button but doesn't tell you which one. The second function tells you that a player canceled the menu and tells also which one did it. That's the out PlayerIndex playerIndex parameter of this function.

Basically, the developer chose to pass or not the information they received from the input detection function.

As to why, I guess it is important to know which player closes the menu. In PES for example, every player set his settings then press the cancel menu button. The menu actually closes only when both players have pressed the cancel button.

Guess, the information about who asked for a pause is not relevant and thus is not passed out.

Answer 2


Controlling player is used if you want to specify a particular person to look for for input. If not specified it looks at all controllers. Player Index specifies who pressed the input that was provided. This is why it is an out parameter.