1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Graphics Draw your text one character at a time

Discussion in 'Tutorials' started by jazzzar, Jul 11, 2016.

  1. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    GM Version: Studio
    Target Platform: Windows
    Download: see code below

    Summary:
    Draw text one character at a time

    Tutorial:
    Hello, this is a script i made to draw your text one character by one,it gives a nice effect for story telling and dialogues, it may not be the best way to do it, but it works, which matters the most :
    here is the script :
    scr_TextDisplay() :
    Code:
    str=argument0;//the string to drawxpos=argument1;//the starting x position of the text
    xpos=argument1;//the starting x position of the text
    ypos=argument2;// the starting y position of the text
    cur_width=0;//this is used to see the current width of the "already drawn" characters
    len=string_length(str);//the length of the string used to seperate the string into single characters(using an array)
    chars[len-1]=0;//the array that holds every single character in the string
    line=0;//the line variable determines how much to jump down from the current postion based on the height of the chars
    var i,j;//you know what these are :P
            for (i=len-1;i>=0;i--)//main loop that seperates the characters and puts them into an array
                chars[i]=string_char_at(str,i+1);
    draw_text(xpos,ypos,chars[0]);//draw the first character,PS:you can remove this and put it in the next for loop but u need to put chars_drawn to 0 in the create event
    cur_width+=string_width(chars[0]);//add the first character's width to the total width
    for (j=1;j< chars_drawn;j++)
        {
            if (chars[j]=='#')
            {
                line+=string_height(chars[j-1]);
                cur_width=0;
            }
            else
            {
            draw_text(xpos+cur_width,ypos+line,chars[j]);
            cur_width+=string_width(chars[j]);
            }
        }//the loop that actually draws the characters
    
    now in the object that you want it to draw the text put this into create event :
    Code:
    chars_drawn=1;//as i said put this to 0 if you want to draw the first character in the for loop
    alarm[0]=10//any value here this is the delay between each character drawn;
    
    in alarm[0] put this :
    Code:
    chars_drawn++;
    if chars_drawn!=len
        alarm[0]=10//the same as above
    Now in the draw event :
    Code:
    scr_TextDisplay("your text goes here",starting x pos,starting y position);
    Hope this helps you, and saves you some time :)
    EDIT : now you can use # to jump to a new line :)
    REDA.
     
    Last edited: Aug 5, 2016
  2. Blakkid489

    Blakkid489 Member

    Joined:
    Jun 30, 2016
    Posts:
    257
    Very nice but how do you go to the next line? "#" doesnt seem to work
     
    jazzzar likes this.
  3. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    It doesn't work because the code draws one character at a time, thus it only goes to the next line for that single character. The quickest way to modify this code to add support for new lines is to put the following in the drawing loop:

    Code:
    if (chars[j] == "#")
    {
        ypos += string_height("#");
        curr_width = 0;
        j++;
    }
    
    ... This would go right before the 'draw_text' line. Not tested, but it should work.
     
    Roger Robertson likes this.
  4. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    that's because it draws a single character at a time, as @stainedofmind stated, his code should work, try it, if it doesn't i'll update the thing for you
     
    Last edited: Jul 28, 2016
    Roger Robertson likes this.
  5. I was not able to get the new line to function correctly. (Almost certainly due to my newbishness.)
    I added it after the draw_text but it did not work at all - then I realized there was a second draw_text - I added it there and it creates a new line but does not go back to the start of the line ... I experimented with changing xpos and a few other things but was unable to get the end result to work as I thought it should (# creating a new line that starts directly below the previous line.)
     
    jazzzar likes this.
  6. Dogwithswords

    Dogwithswords Member

    Joined:
    Aug 4, 2016
    Posts:
    45
    I actually needed this script! Thank you!

    I always wanted to implement a typing dialogue for training levels and between characters.

    I may even add a cool little "blip" sound effect for each character entered. Time to experiment a bit with this to see what I can produce!
     
    jazzzar likes this.
  7. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    i'll update it for you tomorrow and fix everything you want, you may ask for changes you want :)
    glad you liked it, good luck man :)
     
    Dogwithswords likes this.
  8. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    so i decided to update it now for you, just look at the original post, copy and paste just the main script and you're all done :)
     
    stainedofmind likes this.
  9. Blakkid489

    Blakkid489 Member

    Joined:
    Jun 30, 2016
    Posts:
    257
    Lovely. . . .just . . .lovely :D Thanks for the update
     
    jazzzar likes this.
  10. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    you're very welcome :)
     
  11. Thank you, this will come in handy! :cool:
     
    jazzzar likes this.
  12. jazzzar

    jazzzar Member

    Joined:
    Jun 29, 2016
    Posts:
    514
    You're welcome ;)
     
  13. Blakkid489

    Blakkid489 Member

    Joined:
    Jun 30, 2016
    Posts:
    257
    One more question i promise.

    How does one reset the drawing of the text to have a different text appear?

    I have a legitimate code working around this text system but figure out how to reset the drawing of the text so I can have a new text appear. For the most part I just have the text object destroyed but for a particular case I wanna make a new text appear with out destroying the object.

    EDIT: Nvm lol I just had to reset the alarm[0] back to 5
     
    Last edited: Sep 30, 2016
  14. stoves

    stoves Guest

    what do you mean by adding the code to alarm[0]?
     
  15. Humayun

    Humayun Member

    Joined:
    Jun 20, 2016
    Posts:
    89
    Good work! You put a lot effort in it.

    The code I used to make dialog boxes is:
    Code:
    //CreateEvent
    str = "hello word";
    str_main = "";
    
    alarm[0] = 10;
    //Alarm0 Event
    str_main+=string_char_at(str,string_length(str_main)+1);
    alarm[0] = 10;
    //Draw
    draw_text(0,0,str_main);
    
     
  16. Blakkid489

    Blakkid489 Member

    Joined:
    Jun 30, 2016
    Posts:
    257
    Well just have reset the objects alarm that has char_drawn++ and set it back to 5 and put chars_drawn back to 0 to make a text.

    str[0] = "Hello hero"
    str[1] = "Your first mission is to collect all the orbs in the city"

    Or something to that extent
     
  17. stoves

    stoves Guest

    every time i start up my game i get an error message, most likely due to my noobishness at gml.
    here is what it says:


    ___________________________________________
    ############################################################################################
    FATAL ERROR in
    action number 1
    of Alarm Event for alarm 0
    for object obj_hermes:

    Variable obj_hermes.len(100006, -2147483648) not set before reading it.
    at gml_Object_obj_hermes_ObjAlarm0_1 (line 2) - if chars_drawn!=len
    ############################################################################################
    any help?
     
  18. Humayun

    Humayun Member

    Joined:
    Jun 20, 2016
    Posts:
    89
    either execute this code after calling scr_TextDisplay() script or make a new variable in create event "len = 0;" where 0 is length.
     
  19. Noe2302

    Noe2302 Guest

    Really nice! Pretty useful for my Project! So is there a way to draw different strings after each other like they are one text? so if i want for example
    "Hey [name]. How's it going?"
    and the name is stored in a variable, how do let your script draw this?
    Code:
    display_text("Hey " + name + ". How's it going?", x, y)
    doesn't work. Am i just doing it wrong or isn't it possible with your script?



    And just a detail but you messed up copying your code.

    The
    went a second time in the comment of the first line for some reason. It should be:
    Code:
    str=argument0;//the string to draw
    xpos=argument1;//the starting x position of the text
    ....
    Just a little detail.So anyway, keep up your good work ^^
     
    Last edited by a moderator: Nov 23, 2016
  20. Davemane42

    Davemane42 Member

    Joined:
    Jun 21, 2016
    Posts:
    8
    Really thank you, i modified your code to allow for color change and effect to be added via tag in the text and its working great ;)
    Ex:
    Code:
    "This text is white$2 and this is blue $1and then green$0@0 HELL YEAY!@#$3@1Orange$ and $4Purple@ "
    
    MOD EDIT: Image removed as it contains offensive language.
     
    Last edited by a moderator: Nov 27, 2016
  21. NoobsWeStand

    NoobsWeStand Member

    Joined:
    Jun 20, 2016
    Posts:
    144
    Is there an easy way to make this work with: draw_set_halign(fa_center / fa_right)?
    Currently it looks like this, and is not centered:
    [​IMG]
     
  22. samyak

    samyak Member

    Joined:
    Jul 19, 2016
    Posts:
    93
    How to increase the width between characters?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice