GMS 2 problem with keyboard_check, keyboard_check_pressed and keyboard_check_released

Discussion in 'Programming' started by kafecadm, May 14, 2019.

  1. kafecadm

    kafecadm Member

    Joined:
    May 13, 2019
    Posts:
    4
    Hello:

    Firstly I thank everyone for any support you guys could provide, I've been working with GMS 2 for a while now and It has been awesome so far. but right now I am a bit confused, I have the following code:

    Code:
    
    if keyboard_check(ord("X"))
    {
        state = STATE.CHARGING;
    }
    else if keyboard_check_released(ord("X"));
    {
        state = STATE.RELEASE;
    }
    else if keyboard_check_pressed(ord("X"));
    {
        state = STATE.FIRE;
    }
    
    The problem is that if I hold the button it goes into the CHARGING state as it supposed to, but if i hit and quickly release it keeps going into the CHARGING and RELEASE states, and it doesn't attack.

    I have compared my code with others in the forum, blogs, tutorials, etc and I know my code is coherent but for some reason it's not working.

    I thank you guys again.

    Regards
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,931
    I'm a little surprised that code works. It doesn't seem like STATE.FIRE would ever be triggered. That said, there isn't anything wrong with your code, so it is probably with the states themselves. What do those look like?

    If you want to see what code is triggering there, I would use some show_debug_messages: Using show_debug_message.
     
  3. kafecadm

    kafecadm Member

    Joined:
    May 13, 2019
    Posts:
    4
    Oh boy... Well now I am more confued =S lol

    according to GMS2 help

    keyboard_check - With this function you can check to see if a key is held down or not.

    keyboard_check_pressed - With this function you can check to see if a key has been pressed or not

    keyboard_check_released - With this function you can check to see if a key has been released or not.

    so following that logic I would comprehend that

    Code:
    if keyboard_check  // this would be true for every step the button has been held
    
    else if keyboard_released // this would be true only for the step where the button has been released
    
    else if keyboard_pressed // this would be true only for the step where the button has been pressed
    
    Am I wrong in my logic?..

    Regards
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,931
    Yes, that logic is wrong. If else chains will stop at the first one that is true. So:

    Code:
    
    if (a) {
        //stuff
    } else if (b) {
        //stuff
    } else if (c) {
        //stuff
    }
    
    
    For b or c to trigger a must be false. If a is true, then b and c cannot trigger even if they are also true. So when you press a key, it is also held down. Unless GM handles key presses differently, this means that any time c is true, a is also true and therefore, you will never get to c.

    You should reverse the order make it c, b, a (pressed, released, held). However, even that might not fix the problem depending upon where the problem is.
     
    Japster likes this.
  5. kafecadm

    kafecadm Member

    Joined:
    May 13, 2019
    Posts:
    4
    Ok my bad:

    I ran several tests and I found out what you meant, when you hit the key both keyboard_check() and keyboard_pressed() are true only when in the next steps only check would return true... imma try to work on that logic and I'll keep you guys posted.

    Tnx again for helping me man.

    Regards
     
  6. kafecadm

    kafecadm Member

    Joined:
    May 13, 2019
    Posts:
    4
    Hello and thank you again

    I fixed it, not the most elegant solution but works for me... I just needed a bit of your input =P tnx again for the help

    this is the final code

    Code:
    
    /* CODE FOR CHARGED BEAM
        if keyboard_check("X")
        {
           //  using the charging var i validate if the player has been holding the key for a while
            charging++;
    
           // if so then I enter the charging state
            if charging >= 10  
            {
                state = CHARGING;   
            }
        }
         // if the player releases the key while in the CHARGING state then i reinitialize the charging varialbe and enter the desired state
        else if keyboard_check_release("X") and state == CHARGING
        {
            charging = 0;
            state = CHARGE;
        }
        else
        {
            charging = 0;
        }
       
        if key_attack  // attack has been pressed
        {
             state = ATTACK
        }
    
    So the real solution here is the delay due to the fact that keyboard_check_pressed and keyboard_check always return true for the first time you have pressed a key.

    Thank you for helping me understand this.

    Regards
     
    Japster and samspade like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice