Advertisement
LukeLC

wait (GameMaker Studio)

Dec 18th, 2018
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @function   wait(index, time);
  2. /// @param      {int|string}    index
  3. /// @param      {real}          time
  4. /// @author     Lucas Chasteen <lucas.chasteen@xgasoft.com>
  5. /// @copyright  XGASOFT 2018, All Rights Reserved
  6.  
  7. /*
  8. Sets a timer and waits for it to complete. Returns 'true' or 'false' depending on timer state.
  9. Input time is a value in seconds.
  10.  
  11. Note that 'true' will only be returned for **one step**, after which the timer will restart
  12. automatically.
  13.  
  14. Timer 'index' can be either an integer or a string. All timers are global, so you should not
  15. use the same index twice unless you want to overwrite a previous timer. For this reason, a
  16. unique string is recommended for easy memorability.
  17.  
  18. Example usage:
  19.     if (wait("t_alarm", 300)) {
  20.         //Action
  21.     }
  22. */
  23.  
  24. //Set script to auto-init at launch
  25. gml_pragma("global", "wait();");
  26.  
  27. //Initialize data structure if no arguments are supplied
  28. if (argument_count == 0) {
  29.     global.ds_wait = -1; /* DO NOT DO THIS MANUALLY OR YOU WILL CREATE A MEMORY LEAK */
  30.     exit;
  31. }
  32.  
  33. //Ensure timer data structure exists
  34. if (!ds_exists(global.ds_wait, ds_type_map)) {
  35.     global.ds_wait = ds_map_create();
  36. }
  37.  
  38. //Ensure timer ID exists
  39. if (!ds_map_exists(global.ds_wait, argument[0])) {
  40.     global.ds_wait[? argument[0]] = 0;
  41. }
  42.  
  43. //Set timer
  44. if (global.ds_wait[? argument[0]] == 0) {
  45.     global.ds_wait[? argument[0]] = argument[1];
  46. } else {
  47.     //Decrement timer
  48.     global.ds_wait[? argument[0]] -= delta_time/1000000;
  49.                
  50.     //Reset timer and return true when complete
  51.     if (global.ds_wait[? argument[0]] <= 0) {
  52.         ds_map_delete(global.ds_wait, argument[0]);
  53.         return true;
  54.     }
  55. }
  56.  
  57. //Disable until time is complete
  58. return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement