Advertisement
RykinPoe

GMS Input Object

Sep 27th, 2021 (edited)
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create Event
  2. // Enforce singleton pattern
  3. if (!variable_global_exists("Input") or !instance_exists(global.Input)){
  4.     global.Input = id;
  5. } else {
  6.     if (id != global.Input){
  7.         instance_destroy();
  8.         exit;
  9.     }
  10. }
  11.  
  12. // Configuration
  13. deadzone = .2;
  14. menu_mode = false;
  15. menu_delay_amount = 10;
  16.  
  17. // Button/Key definitions
  18. activateKey = vk_space;
  19. activateButton = gp_face1; // Xbox:A Playstation:Cross
  20. attackKey = vk_shift;
  21. attackButton = gp_face3; // Xbox:X Playstation:Square
  22. itemKey = vk_control;
  23. itemButton = gp_face4; // Xbox:Y Playstation:Triangle
  24. pauseKey = vk_escape;
  25. pauseButton = gp_start; // Xbox:Start Playstation:Options
  26.  
  27. // unused buttons
  28. //unused = gp_face2; // Xbox:B Playstation:Circle
  29. //unused = gp_shoulderl; // Xbox:Left Bumper Playstation:L1
  30. //unused = gp_shoulderlb; // Xbox:Left Trigger Playstation:L2
  31. //unused = gp_shoulderr; // Xbox:Right Bumper Playstation:R1
  32. //unused = gp_shoulderrb; // Xbox:Right Trigger Playstation:R2
  33. //unused = gp_select; // Xbox:View Playstation:Touchpad Down
  34. //unused = gp_stickl; // Xbox:Left Stick Click Playstation:L3
  35. //unused = gp_stickr; // Xbox:Right Stick Click Playstation:R3
  36.  
  37. // Initialization, no need to edit these, add more as needed
  38. gamepad_id = undefined;
  39. horizontal = 0; // used for left stick, dpad, arrow keys, and wasd
  40. vertical = 0; // used for left stick, dpad, arrow keys, and wasd
  41. right_horizontal = 0; // used for right stick
  42. right_vertical = 0; // used for right stick
  43. activate = 0;
  44. attack = 0;
  45. item = 0;
  46. pause = 0;
  47.  
  48. // Menu initialization, no need to edit
  49. menu_delay_vertical = menu_delay_amount;
  50. menu_delay_horizontal = menu_delay_amount;
  51. menu_last_vertical = undefined;
  52. menu_last_horizontal = undefined;
  53.  
  54. // Async System Event
  55. switch (async_load[? "event_type"]){
  56.     case "gamepad discovered":
  57.         if (gamepad_id == undefined){ // only change the id if it isn't already set
  58.             gamepad_id = async_load[? "pad_index"];
  59.             gamepad_set_axis_deadzone(gamepad_id, deadzone);
  60.         }
  61.     break;
  62.    
  63.     case "gamepad lost":
  64.         gamepad_id = undefined;
  65.     break;
  66. }
  67.  
  68. // Begin Step Event
  69. // set everything to 0
  70. activate = 0;
  71. attack = 0;
  72. item = 0;
  73. pause = 0;
  74. horizontal = 0;
  75. vertical = 0;
  76. right_horizontal = 0;
  77. right_vertical = 0;
  78.  
  79. // check gamepad
  80. if (gamepad_id != undefined){
  81.     // check left stick
  82.     horizontal = gamepad_axis_value(gamepad_id, gp_axislh);
  83.     vertical = gamepad_axis_value(gamepad_id, gp_axislv);
  84.    
  85.     // check right stick
  86.     right_horizontal = gamepad_axis_value(gamepad_id, gp_axisrh);
  87.     right_vertical = gamepad_axis_value(gamepad_id, gp_axisrv);
  88.    
  89.     // read dpad if there is no left stick input
  90.     if (horizontal == 0 and vertical == 0){
  91.         horizontal = gamepad_button_check(gamepad_id, gp_padr) - gamepad_button_check(gamepad_id, gp_padl);
  92.         vertical = gamepad_button_check(gamepad_id, gp_padd) - gamepad_button_check(gamepad_id, gp_padu);
  93.     }
  94.    
  95.     // check buttons
  96.     activate = gamepad_button_check_pressed(gamepad_id, activateButton);
  97.     attack = gamepad_button_check_pressed(gamepad_id, attackButton);
  98.     item = gamepad_button_check_pressed(gamepad_id, itemButton);
  99.     pause = gamepad_button_check_pressed(gamepad_id, pauseButton);
  100. }
  101.  
  102. // check keyboard if no gamepad input
  103. if (horizontal == 0 and vertical == 0 and right_horizontal == 0 and right_vertical == 0 and activate == 0 and attack == 0 and item == 0 and pause == 0){
  104.     // check arrow keys and WASD
  105.     var keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
  106.     var keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
  107.     var keyUp = keyboard_check(vk_up) or keyboard_check(ord("W"));
  108.     var keyDown = keyboard_check(vk_down) or keyboard_check(ord("S"));
  109.    
  110.     horizontal = keyRight - keyLeft;
  111.     vertical = keyDown - keyUp;
  112.    
  113.     // check other keys
  114.     activate = keyboard_check_pressed(activateKey);
  115.     attack = keyboard_check_pressed(attackKey);
  116.     item = keyboard_check_pressed(itemKey);
  117.     pause = keyboard_check_pressed(pauseKey);
  118. }
  119.  
  120. if (menu_mode){
  121.     // adjust output to work better with a menu
  122.     // vertical
  123.     var _v_input = round(vertical); // round the input in case it is analog
  124.     if (_v_input == menu_last_vertical){
  125.         menu_delay_vertical--;
  126.         vertical = 0;
  127.         if (menu_delay_vertical <= 0){
  128.             menu_last_vertical = undefined;
  129.         }
  130.     } else {
  131.         vertical = _v_input;
  132.         menu_last_vertical = _v_input;
  133.         menu_delay_vertical = menu_delay_amount;
  134.     }
  135.    
  136.     // horizontal
  137.     var _h_input = round(horizontal); // round the input in case it is analog
  138.     if (_h_input == menu_last_horizontal){
  139.         menu_delay_horizontal--;
  140.         horizontal = 0;
  141.         if (menu_delay_horizontal <= 0){
  142.             menu_last_horizontal = undefined;
  143.         }
  144.     } else {
  145.         horizontal = _h_input;
  146.         menu_last_horizontal = _h_input;
  147.         menu_delay_horizontal = menu_delay_amount;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement