As I am now heading heavily into MonoGame development [Some of you may have seen my Instagram/Twitter feeds] I came across this handy piece of code and thought, heck why not just make this knowledge available to everyone, while adding some additional data to it… also for complete beginners I wanted to make it clear to them where this code goes too… [FTR, as of writing this post, I consider myself a MonoGame Beginner]
So I decided to start with something really useful…
Focussing on: if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) Exit();
but more specifically Exit()
I also added Keyboard interaction, something useful to add to this is when the X is clicked on the window, but as that is only useful when not in full screen mode, this entry will just be on two methods… I or another user may add the interaction for when the window is force closed or the X is clicked at a later time [Or when I remember and implement that interaction myself]
Game1.cs:
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
// Let us check the gamepad input first
// note that this works even when the gamepad is connected after the game starts.
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
UnloadContent(); // A good place to call this - this is one of the other methods in Game1.cs
Exit(); // And your window vanishes
};
// Now let us use the keyboard method - Hello Escape Key
// Capture the keyboard state and then respond to input
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Escape)) // Can even be the letter X upper and lower are the same
{
UnloadContent(); // A good place to call this - this is one of the other methods in Game1.cs
Exit(); // And your window vanishes
}
base.Update(gameTime);
}
I have a question, how do you handle more than four pads? do you drop out of XBOX pad controls? just my assumption…
Another handy piece is being able to see your mouse cursor:
Game1.cs:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Look here
IsMouseVisible = true; // This can be removed when publishing or when you add your own cursor to your game
}
Note that all code here is here to make things work and not exactly always performance oriented, that comes later, though if performance is included, then you got lucky and should thank whoever saved your day either way.
Anyhow, I will post things which I find useful, here, it can be a sort of repository, kind of surprised this is not already a thing here really…
When I complete my web studies I will make a searchable database site and … port these over to it…
Anyway, I hope my entries become useful for others.
More to follow…