Kupoapo

draw_text_scrolling (2.2)

Mar 7th, 2020 (edited)
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#########################################################
  2.  
  3. ///     Description:    Draws text scrolling from the first to last character in a string.
  4. ///     Usage:          draw_text_scrolling(0, 0, "Hello World", 0.5, 20, undefined)
  5.  
  6. ///     @param text x
  7. ///     The x position of the text being drawn.
  8.  
  9. ///     @param text y
  10. ///     The x position of the text being drawn.
  11.  
  12. ///     @param text string
  13. ///     The text string to be scrolled across the screen.
  14.  
  15. ///     @param text speed
  16. ///     The speed of the text scrolling. (Range: [0.0] to [1.0])
  17.  
  18. ///     @param text sleep
  19. ///     The time in frames to sleep on pausing characters. (Ex. [.] [,] [!] [?])
  20.  
  21. ///     @param text sound
  22. ///     The sound to play after each character scroll. (Ex. [undefined] [sound_talk])
  23.  
  24. //########################################################
  25.  
  26. #region SOURCE CODE
  27.  
  28. //Gets the variables from parameters.
  29. var text_x      = argument0;
  30. var text_y      = argument1;
  31. var text_str    = argument2;
  32. var text_spd    = argument3;
  33. var text_slp    = argument4;
  34. var text_snd    = argument5;
  35.  
  36. //Checks if the index counter has been defined.
  37. if !variable_instance_exists(id, "_text_char"){
  38.    
  39.     //Defines the counter variables.
  40.     _text_char      = 0;
  41.     _text_prev      = 0;
  42.     _text_sleep     = 0;
  43.    
  44. }
  45.  
  46. //Gets the current character index.
  47. var text_ind = floor(_text_char);
  48. var text_chr = string_char_at(text_str, text_ind);
  49.  
  50. //Checks if the text index isn't the final char.
  51. if (text_ind != string_length(text_str)){
  52.  
  53.     //Checks if the text is not sleeping.
  54.     if (_text_sleep == 0){
  55.  
  56.         //Checks if the following character is a sleep identifier.
  57.         if !((text_chr == ".") || (text_chr == ",") || (text_chr == "?") || (text_chr == "!")){
  58.    
  59.             //Increments the character counter.
  60.             _text_char += text_spd;
  61.        
  62.             //Maxes out the character counter at the string length.
  63.             _text_char = min(_text_char, string_length(text_str));
  64.        
  65.             //Checks if the character has been fully incremented.
  66.             if (text_ind > _text_prev) && (text_snd != undefined) && audio_exists(text_snd){
  67.            
  68.                 //Plays the text sound.
  69.                 audio_stop_sound(text_snd);
  70.                 audio_play_sound(text_snd, 0, false);
  71.            
  72.             }
  73.        
  74.             //Sets the previous value to the char index.
  75.             _text_prev = text_ind;
  76.    
  77.         }else{
  78.        
  79.             //Starts the sleeping process.
  80.             _text_sleep = text_slp;
  81.        
  82.         }
  83.    
  84.     }else{
  85.    
  86.         //Checks if the final frame of waiting.
  87.         if (_text_sleep == 1){
  88.        
  89.             //Increments the character past the sleep identifier.
  90.             _text_char = text_ind + 1;
  91.        
  92.         }
  93.    
  94.         //Decrements the text sleep.
  95.         _text_sleep--;
  96.    
  97.     }
  98.    
  99. }
  100.  
  101. //Draws the scrolling text.
  102. draw_text(text_x, text_y, string_copy(text_str, 1, text_ind));
  103.  
  104. #endregion
  105.  
  106. //########################################################
  107. ///
  108. ///     Copyright Kupoapo Ltd
  109. ///
  110. //########################################################
Add Comment
Please, Sign In to add comment