GML multiline chat using ds_list and for loops [Solved]

Moosh

Member
Hello everyone,

I've been using a ds_list to gather chat from my Twitch channel to make it look a little fancy.

I've managed to access the chat to pull into gamemaker 2 and using the for loop function to separate them onto new lines per each person talking.

I've managed to get the lines to break should the message be long but then I'm running into problems now;
When the longer text is displayed it's drawing over the previous chat. I can't find a way to keep the previous chat and shift it down by however many lines the longer text is.

I've done a test file to create a ds_list and insert a few tester lines that would take up 1 line and a couple that would take up 2/3 lines

This code is placed in the 'draw GUI' event only and the object is placed at x:32, y:32 in the default room.

The if statement that labels the strings with the i counter and an "x" or "y" is to keep track of which strings are going to be broken into multiple lines

Code:
myText = ds_list_create();
ds_list_insert(myText, 0, "Hello");
ds_list_insert(myText, 0, "I greatly appreciate the time you've spent to help.");
ds_list_insert(myText, 0, "Here is a test line that will span 3 lines in the 'chat'. For now, this is how it looks.");
ds_list_insert(myText, 0, "Thank you for helping");

draw_set_font(fFont);
if (ds_exists (myText, ds_type_list)) {
    var chat_size = ds_list_size (myText);
    for (var i=0; i<chat_size; i++) {
        var message = myText [| i];
        font_size_ = font_get_size (fFont)+10;
        chatlines_ = floor (string_height_ext (message, font_size_, 450) / font_size_);
       
       
        if (chatlines_ > 1) {
            draw_text_ext(x,y+(i*font_size_),string(i)+"x - "+message,font_size_, 450);           
        } else {
            draw_text_ext(x,y+(i*font_size_),string(i)+"y - "+message,font_size_, 450);           
        }
    }
}
Expected output:
0y - Thank you for helping
1x - Here is a test line that will
span 3 lines in the 'chat'. For
now, this is how it looks.
4x - I greatly appreciate the time
you've spent to help.
6y - Hello

If anyone can help, I'd be very grateful. I've spent a few days trying to figure this out but getting nowhere.
 

FrostyCat

Member
Your draw_text_ext() calls are always assuming single lines. The fact that chatlines_ is not involved at all in the positioning of those calls should have been a major red flag. Just use string_height_ext() exclusively, and cumulate their heights instead of using fixed line heights.
Code:
var yy = y;
for (var i = 0; i < chat_size; i++) {
  var message = myText [| i],
      font_size_ = font_get_size(fFont)+10,
      line_height = string_height_ext(message, font_size_, 450);
  draw_text_ext(x, yy, message, font_size_, 450);
  yy += line_height;
}
 

TheouAegis

Member
If the size of your chat window will not change, then you could loop through the list of chat, break up a long lines, and insert them into the list before you handle drawing the chat.
 

Moosh

Member
Your draw_text_ext() calls are always assuming single lines. The fact that chatlines_ is not involved at all in the positioning of those calls should have been a major red flag. Just use string_height_ext() exclusively, and cumulate their heights instead of using fixed line heights.
Code:
var yy = y;
for (var i = 0; i < chat_size; i++) {
  var message = myText [| i],
      font_size_ = font_get_size(fFont)+10,
      line_height = string_height_ext(message, font_size_, 450);
  draw_text_ext(x, yy, message, font_size_, 450);
  yy += line_height;
}
Thank you so much. It had been driving me mad!
 
Top