I'm trying to port my game from XNA to SharpDX, but I can't find the solution for my problem:
namespace TomShane.Neoforce.Central
{
public class InputState
{
#region Fields
public const int MaxInputs = 4;
public readonly KeyboardState[] CurrentKeyboardStates;
// public readonly GamePadState[] CurrentGamePadStates;
public readonly KeyboardState[] LastKeyboardStates;
// public readonly GamePadState[] LastGamePadStates;
public readonly KeyboardManager Keyboard;
// public readonly bool[] GamePadWasConnected;
// public TouchCollection TouchState;
//public readonly List<GestureSample> Gestures = new List<GestureSample>();
#endregion
#region Initialization
/// <summary>
/// Constructs a new input state.
/// </summary>
public InputState()
{
CurrentKeyboardStates = new KeyboardState[MaxInputs];
// CurrentGamePadStates = new GamePadState[MaxInputs];
LastKeyboardStates = new KeyboardState[MaxInputs];
// LastGamePadStates = new GamePadState[MaxInputs];
//GamePadWasConnected = new bool[MaxInputs];
}
#endregion
#region Public Methods
/// <summary>
/// Reads the latest state of the keyboard and gamepad.
/// </summary>
public void Update()
{
for (int i = 0; i < MaxInputs; i++)
{
LastKeyboardStates[i] = CurrentKeyboardStates[i];
// LastGamePadStates[i] = CurrentGamePadStates[i];
CurrentKeyboardStates[i] = Keyboard.GetState();
// CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
// Keep track of whether a gamepad has ever been
// connected, so we can detect if it is unplugged.
//if (CurrentGamePadStates[i].IsConnected)
//{
// GamePadWasConnected[i] = true;
//}
}
// TouchState = TouchPanel.GetState();
//Gestures.Clear();
//while (TouchPanel.IsGestureAvailable)
//{
// Gestures.Add(TouchPanel.ReadGesture());
//}
}
/// <summary>
/// Helper for checking if a key was newly pressed during this update. The
/// controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a keypress
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;
int i = (int)playerIndex;
// int i = playerIndex;
return (CurrentKeyboardStates[i].IsKeyDown(key) &&
LastKeyboardStates[i].IsKeyPressed(key));
//IsKeyUp(key));
}
else
{
// Accept input from any player.
return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex)); //||
// IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
// IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
// IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
}
}
/// <summary>
/// Helper for checking if a button was newly pressed during this update.
/// The controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a button press
/// is detected, the output playerIndex reports which player pressed it.
/// </summary>
/// <summary>
/// Checks for a "menu select" 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 IsMenuSelect(PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
IsNewKeyPress(Keys.Enter, 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);
}
/// <summary>
/// Checks for a "menu up" 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 IsMenuUp(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex);
}
/// <summary>
/// Checks for a "menu down" 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 IsMenuDown(PlayerIndex? controllingPlayer)
{
PlayerIndex playerIndex;
return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex);// ||
// IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
// IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
}
/// <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);
}
#endregion
}
}
I'm getting this error. I don't know how fix it.
Error 2 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 95 21 Central Error 3 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 156 21 Central Error 4 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 172 21 Central Error 5 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 186 20 Central Error 6 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 201 21 Central Error 7 The type 'SharpDX.PlayerIndex' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 216 21 Central Error 8 Cannot convert type 'SharpDX.PlayerIndex' to 'int' C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 103 23 Central Error 9 The out parameter 'playerIndex' must be assigned to before control leaves the current method C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 159 13 Central Error 10 The out parameter 'playerIndex' must be assigned to before control leaves the current method C:\Users\Pedro\Dropbox\GAMES\NeoforceControls-SharpDX-master\Central\ScreenManager\InputState.cs 175 13 Central
PlayerIndex?
means that it can be null or contain a value. It's a synonym to Nullable<PlayerIndex>
.
You can either change it to PlayerIndex
or, when using it, use it as playerIndex.Value
so you refer to the value it may be containing, you can also check if this is the case before using it by checking the HasValue
property of the type.
More info on nullable types here.
If that doesn't help, try to be more specific on the line of code throwing the error, instead of copying the whole thing.