Beginner Question: Same Key to draw/holster weapon

Discussion in 'Programming' started by Graphixx_03, Jul 23, 2019.

  1. Graphixx_03

    Graphixx_03 Member

    Joined:
    Jul 23, 2019
    Posts:
    1
    Hi there, new to the forums, but thought it would be a good place to find the answer to this:

    I’m sure it’s simple, but I want the “shift” key to draw weapon when it’s holstered, but also use the same key to holster it when it’s drawn..

    The code I expected to work ended up just looping and quickly drawing and Holstering again. Ive got all the sprites to change as needed but trying to figure out how to get this to work properly.

    Thanks in advance!
     
  2. Annoyed Grunt

    Annoyed Grunt Member

    Joined:
    Jun 20, 2016
    Posts:
    136
    Make sure to check for presses of the key and not for the key being held. Depending on what method you are using, this translates to using the Key Pressed Event instead of the Key Down Event or keyboard_check_pressed() instead of keyboard_check().
    The difference is simple: checking for the key press won't allow looping since pressing a key is an action that lasts only a single step and the user must raise and lower the key again for it to happen a second time. On the other hand, checking for the key being held down is an action that lasts throughout multiple steps.
     
  3. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    182
    You may need another var also?

    Create:
    Code:
    // Are we allowed to draw our weapon OR put it back. We don't want continuous looping if something is true to false
    can_use = false;
    draw = false;
    
    Step:
    Code:
    if (vk_shift) {can_use = true}
    if (can_use) {draw = !draw}
    
    if (draw) {change sprite index/speed ect}
    else {other sprite index/speed ect}
    
    Animation end:
    Code:
    can_use = false;
    
     
  4. van0014

    van0014 Member

    Joined:
    Apr 13, 2017
    Posts:
    10
    To handle any 'debounce' looping, pressing the key should set an alarm or variable that counts to 0. Then set the button so it won't work during the cooldown, so it can't even loop on a quick double press
     
    Last edited: Jul 24, 2019

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