Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using SFML2.0 and am trying to make a wrapper class for my controller/joystick. I read all the input data from my controller and send it off to my controllable object.

I want to have two types of buttons per button press, one that is continues(true false state ) and one that is an action and is set to false after the next frame update.

Here is an example of how I set my button A to true or false with the SFML api. Whereas data is my struct of buttons, and A holds my true/false state every update.

data.A = sf::Joystick::isButtonPressed(i,st::input::A);

But I've also added "data.actionA" which represents the one time action state.

Basically what I want is for actionA to be set false after the update its been set to true. I'm trying to keep track of the previous state. But I seem to fall into this loop where it toggles between true and false every update.

Anyone an idea?

share|improve this question
    
If there is one reason why SE is actually a bad idea, this question demonstrates it perfectly. I find myself falling into the trap of asking really trivial questions like this, I mean, people should solve these logical issues by themselves, if you gave it just a bit of thought you could've figured this out! These tasks need to be practiced, they will be encountered all the time. –  akled Jun 19 '12 at 19:44
1  
I was actually stuck for a long time. Asking these questions can help others out as well. Most questions here are about "logical issues". So don't see the harm. –  Sidar Jun 20 '12 at 16:06
1  
Sorry, I guess I encountered this problem too many times before and forgot that maybe others haven't. But I think you get my point, I just have this fear of becoming lazy... –  akled Jun 20 '12 at 16:20
1  
We tend to overlook things. That's not to say I didn't feel kinda stupid knowing how simple the solution was. Sometimes we just need to take a break! Also it would only be lazy if you asked without trying yourself. –  Sidar Jun 20 '12 at 16:24
add comment

2 Answers

up vote 3 down vote accepted

Here's some pseudo code for you to try

// This here is a global variable.
...
    bool wasButtonPressed = false;
...    

// This here is your function.
...
    bool isButtonPressed = sf::Joystick::isButtonPressed(i,st::input::A);

    // Check if the key was pressed since last frame, aka KeyPressed.
    if(isButtonPressed && !wasButtonPressed)
        someFunction();

    // Check if the key was held since last frame, aka KeyHeld.
    if(isButtonPressed && wasButtonPressed)
        someFunction();

    // Check if the key was released since last frame, aka KeyReleased.
    if(!isButtonPressed && wasButtonPressed)
        someFunction();

    wasButtonPressed = isButtonPressed;
...

We store the previous state, so we know how the key has changed since the last frame.

share|improve this answer
    
Yes I already solved it myself ( see edit ). I was already keeping track of previous states. I was overthinking it. –  Sidar Jun 20 '12 at 16:08
add comment

My solution:

data.actionA = data.A = sf::Joystick::isButtonPressed(i,st::input::A);
if(prev.A) data.actionA = false;

First I always set the actionA to the value of the button state. Then I check if the previous state of A is true. If so we negate the value.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.