Will (A)

I can't figure out how to report the ammount of time that gamepad buttons, trigger or thumbstuicks have been pressed/non-zero for.

Does anyone have any ideas



Re: XNA Game Studio Express Timing gamePad button, trigger and thumbsticks

hpjchobbes

My first thought would be to save the total elapsed time when a button is first pressed, then if you want to know how long it's been down, get the current total elapsed time and subtract the saved time from it.

Code Snippet

System.TimeSpan buttonStartTimePress, buttonTotalTimePress;
GamePadState previousState;

protected void Update(GameTime gameTime)
{
// Get the current state of the gamepad
GamePadState currentState = GamePad.GetState(PlayerIndex.One);

// Check if the state has changed since last time
if (currentState.PacketNumber != previousState.PacketNumber)
{
// Input has changed
// Lets see if the A button is pressed
if (currentState.Buttons.A == ButtonState.Pressed)
{
// Is it newley pressed, or still being pressed
if (previousState.Buttons.A != ButtonState.Pressed)
{
// Newley pressed, lets start recording the time
buttonStartTimePress = gameTime.TotalGameTime;
}
else
{
// Is still being held down
// Let's see how long it's been held down for
buttonTotalTimePress = gameTime.TotalGameTime - buttonStartTimePress;
}
}
else
{
// The button is not being pressed
// Check if the previous state was pressed (ie, released the button)
if (previousState.Buttons.A == ButtonState.Pressed)
{
// We've released the button
// Let's see how long it's been held down for
buttonTotalTimePress = gameTime.TotalGameTime - buttonStartTimePress;
}
}
}

}






Re: XNA Game Studio Express Timing gamePad button, trigger and thumbsticks

Will (A)

Excellent, cheers for that mate.

I've slightly adapted your timing code by splitting the task between the Update and UpdateInput methods. For some reason this seemed to work better.

I've aslo put a Console.LineWrite in there so peeps can see if its all working as its supposed to.

Code Snippet


System.TimeSpan buttonStartTimePress;
System.TimeSpan buttonTotalTimePress;

bool buttonPress;
bool previousButtonPress;

protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

UpdateInput();

if (buttonPress != previousButtonPress)
{
if (buttonPress)
// Start recording the time.
buttonStartTimePress = gameTime.TotalGameTime;
else
{
// Finsh recording the time.
buttonTotalTimePress = gameTime.TotalGameTime - buttonStartTimePress;

Console.WriteLine(buttonTotalTimePress);
}

previousButtonPress = buttonPress;
}

base.Update(gameTime);
}

GamePadState previousGamePadState;

void UpdateInput()
{
// Check the current state of Player One.
GamePadState currentState = GamePad.GetState(PlayerIndex.One);
// Process input only if connected and if the packet numbers differ.
if ((currentState.IsConnected) && (currentState.PacketNumber
!= previousGamePadState.PacketNumber))
{
if (currentState.Buttons.A == ButtonState.Pressed &&
previousGamePadState.Buttons.A == ButtonState.Released)
{
// Button A has just been pressed.
GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);

buttonPress = true;

}

else if (currentState.Buttons.A == ButtonState.Released &&
previousGamePadState.Buttons.A == ButtonState.Pressed)
{
// Button A has just been released.
GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);

buttonPress = false;
}

// When finished with differences, update PreviousGamePadState.
previousGamePadState = currentState;
}
}






Re: XNA Game Studio Express Timing gamePad button, trigger and thumbsticks

Zingam

In that context: what is the best method for checking for button/trigger combos




Re: XNA Game Studio Express Timing gamePad button, trigger and thumbsticks

hpjchobbes

Code looks good. I really think I need to break out my code a lot more, but I'm just starting. I was thinking of creating 'game states' and have my code branch out based on what state the game is in. Input would be different if the game was in Attract mode, Menu mode, Playing mode, Movie/Scene mode, etc.

Zingam wrote:
In that context: what is the best method for checking for button/trigger combos


I'm not sure this would be the best way, but I would start by keeping a queue of button presses and timing between pressings. You could then compare the queue against a list of valid combos to see if anything matches. Updating the queue would be determined by how your game mechanics work.

Code Snippet

// Struct holds the buttons that were pressed as well

// as the time since last buttons were pressed

public struct KeyPress

{

public GamePadButtons gpButtons;

public TimeSpan tsElapsedTime;

}

// Queue holds a list of Keypresses

Queue<KeyPress> KeyPressQueue = new Queue<KeyPress>();