Advertisement
Kupoapo

draw_text_scrolling (2.3)

Jan 12th, 2021
2,954
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. function draw_text_scrolling(text_x, text_y, text_str, text_spd, text_slp, text_snd){
  26.  
  27.     #region SOURCE CODE
  28.  
  29.     //Checks if the index counter has been defined.
  30.     if !variable_instance_exists(self.id, "_text_char"){
  31.    
  32.         //Defines the counter variables.
  33.         _text_char      = 0;
  34.         _text_prev      = 0;
  35.         _text_sleep     = 0;
  36.    
  37.     }
  38.  
  39.     //Gets the current character index.
  40.     var text_ind = floor(_text_char);
  41.     var text_chr = string_char_at(text_str, text_ind);
  42.  
  43.     //Checks if the text index isn't the final char.
  44.     if (text_ind != string_length(text_str)){
  45.  
  46.         //Checks if the text is not sleeping.
  47.         if (_text_sleep == 0){
  48.  
  49.             //Checks if the following character is a sleep identifier.
  50.             if !((text_chr == ".") || (text_chr == ",") || (text_chr == "?") || (text_chr == "!")){
  51.    
  52.                 //Increments the character counter.
  53.                 _text_char += text_spd;
  54.        
  55.                 //Maxes out the character counter at the string length.
  56.                 _text_char = min(_text_char, string_length(text_str));
  57.        
  58.                 //Checks if the character has been fully incremented.
  59.                 if (text_ind > _text_prev) && (text_snd != undefined) && audio_exists(text_snd){
  60.            
  61.                     //Plays the text sound.
  62.                     audio_stop_sound(text_snd);
  63.                     audio_play_sound(text_snd, 0, false);
  64.            
  65.                 }
  66.        
  67.                 //Sets the previous value to the char index.
  68.                 _text_prev = text_ind;
  69.    
  70.             }else{
  71.        
  72.                 //Starts the sleeping process.
  73.                 _text_sleep = text_slp;
  74.        
  75.             }
  76.    
  77.         }else{
  78.    
  79.             //Checks if the final frame of waiting.
  80.             if (_text_sleep == 1){
  81.        
  82.                 //Increments the character past the sleep identifier.
  83.                 _text_char = text_ind + 1;
  84.        
  85.             }
  86.    
  87.             //Decrements the text sleep.
  88.             _text_sleep--;
  89.    
  90.         }
  91.    
  92.     }
  93.  
  94.     //Draws the scrolling text.
  95.     draw_text(text_x, text_y, string_copy(text_str, 1, text_ind));
  96.  
  97.     #endregion
  98.    
  99. }
  100. //########################################################
  101. ///
  102. ///     Created by Kupoapo
  103. ///
  104. //########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement