Graphics Draw your text one character at a time

jazzzar

Member
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:
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.
 
O

OhGoodShepherd

Guest
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.)
 
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

Member
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.)
i'll update it for you tomorrow and fix everything you want, you may ask for changes you want :)
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!
glad you liked it, good luck man :)
 

jazzzar

Member
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.)
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 :)
 
B

Blakkid489

Guest
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 by a moderator:

Humayun

Member
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);
 
B

Blakkid489

Guest
what do you mean by adding the code to alarm[0]?
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
 
S

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?
 

Humayun

Member
either execute this code after calling scr_TextDisplay() script or make a new variable in create event "len = 0;" where 0 is length.
 
N

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.

Code:
str=argument0;//the string to drawxpos=argument1;//the starting x position of the text
xpos=argument1;//the starting x position of the text
....
The
Code:
xpos=argument1;//the starting x position of the text
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:
D

Davemane42

Guest
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:
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:
 
Top