• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Centering typewriter text?

hdarren

Member
Hi!

I am doing a intro to my game. The text shows like typical RPG with one letter showing after another. I want the complete lines to align to the center of the screen but the problem is that when I use draw_set_halign(fa_center) the words spread out from the center which looks bad. I want it so the letters spread out from the left hand side, so the words themselves do not move at all.

I am struggling to find the correct formula to work out the correct offset from the left-hand side of the screen. Can anybody help?

Thank you.
 

jazzzar

Member
Hi, did you try my tutorial here?
The thing you only need to do to get them starting at the left hand side, use fa_left and start drawing from there, i mean x a little more than 0, hope this helps :)
 

GMWolf

aka fel666
If i understand correctly, you want the final text to be centered, but you dont want the letters to change positions once they have been placed?

One easy solution to that is to use a monospace font, with one special character left bleck (like, the ~ character for example).
then before drawing you text, replace all the text minus what should be drawn and the spaces with the '~' character.

for example:
Code:
var text = "hello, this text is centered!"; //original text
var count = 8; //number of chars to draw. animate this value

vqr display_text = ""; //text to display
for(var i = 1; i <= string_length(text); i++) {
    var char = string_char_at(text, i);
   if (i <= count) {
        display_text += char;
    } else {
        if (char = " ") {
            display_text += " ";
        } else {
            display_text += "~";
        }
    }
}

draw_set_halign(fa_center);
draw_text(500, 500, display_text);
just make sure the ~ character is not part of the font!

hope this helps!
 
Last edited:

hdarren

Member
It's actually basic maths when you work it out. You set the text align to the left. You get the width of the string using string_width(). Now you get the view width and take the string width off that. Then you halve that result.
 

GMWolf

aka fel666
It's actually basic maths when you work it out. You set the text align to the left. You get the width of the string using string_width(). Now you get the view width and take the string width off that. Then you halve that result.
That's true. But it gets a little more complex of you want multiple lines and wrapping. Though I guess you can keep track of individual lines yourself.
 

GMWolf

aka fel666
Your "a little more complex" is just a matter of "adding _ext at the back of string_width".

See: string_width_ext()
But that will not center on a line by line basis, will it?
It will center the block of text, but have the all the lines start at the same x coordinate, or am I missing something?

Maybe I'm misunderstanding what the original question was...
 

Yambam

Member
But that will not center on a line by line basis, will it?
It will center the block of text, but have the all the lines start at the same x coordinate, or am I missing something?
Good point. :) Here's a handy script from GMLscripts.com (string_wordwrap). Wait that script doesn't take the width in pixels...
Code:
/// string_wordwrap(string,length,break,split)
//
//  Returns a string with break characters inserted
//  between words at a given character interval.
//
//      string      text to word wrap, string
//      length      maximum string length before a line break, real
//      break       line break characters to insert into text, string
//      split       split words that are longer than the maximum, bool
//
/// GMLscripts.com/license
{
    var str,num,brk,cut,out,i,j;
    str = argument0;
    num = argument1;
    brk = argument2;
    cut = argument3;
    out = "";
    while (string_length(str)) {
        while (string_pos(brk,str) <= num + 1) && (string_pos(brk,str) > 0) {
            out += string_copy(str,1,string_pos(brk,str)+string_length(brk));
            str = string_delete(str,1,string_pos(brk,str)+string_length(brk));
        }
        i = string_length(str) + 1;
        if (i > num + 1) {
            for (i = num + 1; i > 0; i -= 1) {
                if (string_char_at(str,i) == " ") break;
            }
        }
        if (i == 0) {
            if (cut) {
                j = num;
                i = j;
            } else {
                i = string_pos(" ",str);
                if (i == 0) {
                    j = string_length(str);
                    i = j;
                } else {
                    j = i;
                    i -= 1;
                }
            }
        } else {
            j = i;
            i -= 1;
        }
        out += string_copy(str,1,i) + brk;
        str = string_delete(str,1,j);
    }
    out = string_copy(out,1,string_length(out) - string_length(brk));
    return out;
}
 

GMWolf

aka fel666
@Yambam
But you still have the problem when changing the number of characters being displayed. As you animate the text, the characters will move to the left.
This will allow you to get the width of the whole text, not of individual lines right?
You would have to split the text into lines, and write each line centered on by one by getting the width of each line...
 
Top