RedKnight91

Key buffers and Key timers

Aug 13th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. //KEY BUFFER
  2. //------------------------------------------
  3. /*
  4. Holds the value of a key press/release for a set time then decrease it back to 0 unless key is pressed again
  5. I recommend using this only for key/mouse press/release, not for holding as that's usually a per-frame kind of check
  6.  
  7. NOTE: If you're using GML, key will check as bool TRUE as long as its value is > 0.5, otherwise you'll need to check its value
  8. GML: if (key) { /*code*/ }
  9. Other: if (key > 0) { /* code */ }
  10. */
  11. //------------------------------------------
  12.  
  13. //Buffer duration set to 10 frames
  14. buffer_time = 10;
  15.  
  16. //Reduce buffer timer
  17. key = max(--key, 0);
  18.  
  19. //Get new key press/release
  20. key = max(key_check * buffer_time, key);
  21.  
  22. //------------------------------------------
  23. //Short version
  24. //------------------------------------------
  25. buffer_time = 10;
  26. key = max(key_check* buffer_time, --key);
  27.  
  28.  
  29. //------------------------------------------
  30. //How to check if the key was JUST pressed
  31. //------------------------------------------
  32. if (key == buffer_time) { /*code*/ }
  33.  
  34.  
  35. //KEY TIMER
  36. //------------------------------------------
  37. /*
  38. Holds the number of frames a key was held for, resets to 0 when released
  39. I recommend using this for key/mouse hold, as it's usually a per-frame kind of check
  40. */
  41. //------------------------------------------
  42.  
  43. //Get current key press input
  44. var _key_input= key_check;
  45.  
  46. //Sum to previous key value OR reset to zero
  47. key = (key + _key_input) * _key_input;
Advertisement
Add Comment
Please, Sign In to add comment