• 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!

Legacy GM ]Solved]Different Color letters in Text

Ladi_Pix3l

Member
Hey developers!!!

How can I achieve this without drawing. . . . .well this..
Code:
draw_draw_set_color(c_red);
draw_text(1,4,"T");
draw_draw_set_color(c_orange);
draw_text(2.2,4,"i");
draw_draw_set_color(c_yellow);
draw_text(3.4,4,"t");
draw_draw_set_color(c_lime);
draw_text(4.6,4,"l");
draw_draw_set_color(c_blue);
draw_text(5.8,4,"e");
draw_set_color(c_white);
I feel like this is not the best way to go about this
 

obscene

Member
That's pretty much it. You could create a script with a little effort that would make it easier to do it on the fly...

scr_draw_text_multicolor("t",c_red,"i",c_orange,etc...);

But behind the hood it would be doing the same thing you coded.
 
C

Crazy Star

Guest
I made an example for the fun of it =)

Create event:
Code:
{
    
    strarr = [];
    strarr = scr_add_color_substring(strarr, "t", c_black);
    strarr = scr_add_color_substring(strarr, "e", c_orange);
    strarr = scr_add_color_substring(strarr, "s", c_blue);
    strarr = scr_add_color_substring(strarr, "t", c_lime);
    
}
Draw event:
Code:
{

    scr_draw_color_string(10, 10, strarr);
    draw_text(10, 30, "test");

}
scr_add_color_substring()
Code:
///@argument0 array
///@argument1 substr
///@argument2 color
{
    var string_array, substr, color;
        string_array = argument0;
        substr = argument1;
        color = argument2;
        
    string_array[array_length_1d(string_array)] = [substr, color];

    return string_array;
        
}
scr_draw_color_string()
Code:
///@argument0 x
///@argument1 y
///@argument2 array
{
    var dx, dy;
        dx = argument0;
        dy = argument1;
    var len = array_length_1d(argument2);
    for( var i=0; i<len; ++i )
    {
        var sub = argument2[i];
        draw_set_color(sub[1]);
        draw_text(dx, dy, sub[0]);
        dx += string_width(sub[0]);
    }
    draw_set_color(c_white);
}

Happy coding!
 

samspade

Member
Hey developers!!!

How can I achieve this without drawing. . . . .well this..
Code:
draw_draw_set_color(c_red);
draw_text(1,4,"T");
draw_draw_set_color(c_orange);
draw_text(2.2,4,"i");
draw_draw_set_color(c_yellow);
draw_text(3.4,4,"t");
draw_draw_set_color(c_lime);
draw_text(4.6,4,"l");
draw_draw_set_color(c_blue);
draw_text(5.8,4,"e");
draw_set_color(c_white);
I feel like this is not the best way to go about this
obscene is right. A script would be easiest. The basic idea would be something like this:

Code:
///scr_draw_text_multicolor(x, y, string, color_array);

var draw_x = argument[0];
var draw_y = argument[1];
var str = argument[2];
var color_array = argument[3];
var array_length = array_length_1d(color_array);
var str_length = string_length(str);

for (var i = 1; i <= str_length; i += 1) {
    var char = string_char_at(str, i);
    var char_width = string_width(char);
    var col = color_array[i div array_length];
    draw_set_color(col);
    draw_text(draw_x, draw_y, char);
    draw_x += char_width;
}
I'm doing this off the top of my head so it might not work exactly, but the idea is you would pass in something like this:

scr_draw_text_multicolor(x, y, "Hello World", [c_white, c_red, c_blue]);

And it would draw it alternating the color at each letter (counting the space in this example).
 

Ladi_Pix3l

Member
I made an example for the fun of it =)

Create event:
Code:
{
   
    strarr = [];
    strarr = scr_add_color_substring(strarr, "t", c_black);
    strarr = scr_add_color_substring(strarr, "e", c_orange);
    strarr = scr_add_color_substring(strarr, "s", c_blue);
    strarr = scr_add_color_substring(strarr, "t", c_lime);
   
}
Draw event:
Code:
{

    scr_draw_color_string(10, 10, strarr);
    draw_text(10, 30, "test");

}
scr_add_color_substring()
Code:
///@argument0 array
///@argument1 substr
///@argument2 color
{
    var string_array, substr, color;
        string_array = argument0;
        substr = argument1;
        color = argument2;
       
    string_array[array_length_1d(string_array)] = [substr, color];

    return string_array;
       
}
scr_draw_color_string()
Code:
///@argument0 x
///@argument1 y
///@argument2 array
{
    var dx, dy;
        dx = argument0;
        dy = argument1;
    var len = array_length_1d(argument2);
    for( var i=0; i<len; ++i )
    {
        var sub = argument2[i];
        draw_set_color(sub[1]);
        draw_text(dx, dy, sub[0]);
        dx += string_width(sub[0]);
    }
    draw_set_color(c_white);
}

Happy coding!
This looks interesting. It's looks a little messy though... not in the way you put it together. that is fantastic. Just more of setting up the concept.


obscene is right. A script would be easiest. The basic idea would be something like this:

Code:
///scr_draw_text_multicolor(x, y, string, color_array);

var draw_x = argument[0];
var draw_y = argument[1];
var str = argument[2];
var color_array = argument[3];
var array_length = array_length_1d(color_array);
var str_length = string_length(str);

for (var i = 1; i <= str_length; i += 1) {
    var char = string_char_at(str, i);
    var char_width = string_width(char);
    var col = color_array[i div array_length];
    draw_set_color(col);
    draw_text(draw_x, draw_y, char);
    draw_x += char_width;
}
I'm doing this off the top of my head so it might not work exactly, but the idea is you would pass in something like this:

scr_draw_text_multicolor(x, y, "Hello World", [c_white, c_red, c_blue]);

And it would draw it alternating the color at each letter (counting the space in this example).
Hmmm does this have a limit to how many colors I can use?
 

samspade

Member
This looks interesting. It's looks a little messy though... not in the way you put it together. that is fantastic. Just more of setting up the concept.



Hmmm does this have a limit to how many colors I can use?
No. However, I did make a few changes to the script as there were a couple errors when I actually tested it.

Code:
///scr_draw_text_multicolor(x, y, string, color_array);

var draw_x = argument[0];
var draw_y = argument[1];
var str = argument[2];
var color_array = argument[3];
var array_length = array_length_1d(color_array);
var str_length = string_length(str);

for (var i = 1; i <= str_length; i += 1) {
    var char = string_char_at(str, i);
    var char_width = string_width(char);
    var col = color_array[(i - 1) mod array_length];
    draw_set_color(col);
    draw_text(draw_x, draw_y, char);
    draw_x += char_width;
}

draw_set_color(c_white);
Additionally, I am having some trouble with the spacing for reasons I can't figure out. Certain letters don't seem to be spaced properly (such as I). Someone who has more experience with character manipulation will have to answer though.
 
C

Crazy Star

Guest
Maybe it's better to use string_width() on the entire substring for increased precision. Just an idea.
 

Gamebot

Member
I would just go with your original way. Easier to change if you need to later. The only other way I would suggest is using a sprite with different sprite indexes, one for each letter. You can pre-color these or make them white then color them in the create event using a for loop. You could even get a bit risky for each letter by using:

randomize();
make_color_rgb( random(255), random(255), 255)

You could use random_range for more control ect...
 

samspade

Member
Maybe it's better to use string_width() on the entire substring for increased precision. Just an idea.
How would you do that since the string never consists of more than one letter at a time as you need to draw each letter individually to have a different color? Even if it did work, what is the underlying problem? I have run it in the debugger and ever char is given a width and the draw_x is increased by that width each time. So the code is doing exactly what you think it should, and it works for most of the letters.
 

Gamebot

Member
This is a tested switch event with STRING_WIDTH;

Create:
Code:
offx = 0;       
title = "title";
Draw:
Code:
/This assumes you are using "TITLE" or similar.

draw_set_font(fnt_codecom);
draw_set_halign(fa_left);



for(i = 1; i <= string_length(title); i++)
{
 var letter = string_char_at(title, i)
 var width = string_width(letter)

 // Optional. Draw shadow text beneath any other color text;
 draw_set_color(c_black);
 draw_text(x + offx - 1, y + 1, letter);

  switch(i)
 {
  case 1:
  draw_set_color(c_red);
  break;
 
  case 2:
  draw_set_color(c_blue);
  break;
 
  case 3:
  draw_set_color(c_green);
  break;
 
  case 4:
  draw_set_color(c_orange);
  break;
 
  case 5:
  draw_set_color(c_purple);
  break;
 }
  draw_text(x + offx, y, letter);

  //Offset text with 20 pixels in between each letter. Extra "space" if needed.
  offx += width;
  offx += 20;
}
 
//Reset x position after loop
offx = x;
Change the last offx to see the changes in width between letters. (The number 20)
 

Ladi_Pix3l

Member
This is a tested switch event with STRING_WIDTH;

Create:
Code:
offx = 0;      
title = "title";
Draw:
Code:
/This assumes you are using "TITLE" or similar.

draw_set_font(fnt_codecom);
draw_set_halign(fa_left);



for(i = 1; i <= string_length(title); i++)
{
 var letter = string_char_at(title, i)
 var width = string_width(letter)

 // Optional. Draw shadow text beneath any other color text;
 draw_set_color(c_black);
 draw_text(x + offx - 1, y + 1, letter);

  switch(i)
 {
  case 1:
  draw_set_color(c_red);
  break;
 
  case 2:
  draw_set_color(c_blue);
  break;
 
  case 3:
  draw_set_color(c_green);
  break;
 
  case 4:
  draw_set_color(c_orange);
  break;
 
  case 5:
  draw_set_color(c_purple);
  break;
 }
  draw_text(x + offx, y, letter);

  //Offset text with 20 pixels in between each letter. Extra "space" if needed.
  offx += width;
  offx += 20;
}
 
//Reset x position after loop
offx = x;
Change the last offx to see the changes in width between letters. (The number 20)
This actually worked nicely. Thanks guys =). I'm gonna tweak this a bit so I have slightly more control to change the order or colors and whether or not I wanna have a pattern implemented.

Thanks again!!!
 
Top