How would I make text draw reverse?

BMatjasic

Member
Hello guys,

Recently I've been working on a chat system in which I came to a small problem. I've created a script named draw_lines in which I've added the following code:

Code:
var xx = argument0;
var yy = argument1;
var yyy = yy
for(var i=0; i < ds_list_size(lines); i++) {

    var map = ds_list_find_value(lines, i);
    var pname = ds_map_find_value(map, "pname");
    var pcolor = ds_map_find_value(map, "pcolor");
    var text = ds_map_find_value(map, "text");
    var tcolor = ds_map_find_value(map, "tcolor");
        draw_set_halign(fa_left);
        draw_set_color(pcolor);
        draw_set_font(fnt_name);
        var str = string(pname) + " : ";
        draw_text(xx, yy - 24*i,str);
        draw_set_color(tcolor);
        draw_set_font(fnt_text);
        draw_text(xx + string_width(str)*1 , yy -  24*i,  text);

}
And this is the script in which I add line:

Code:
var pname = argument0;
var pcolor = argument1;
var text = argument2;
var tcolor = argument3;

var map = ds_map_create();

ds_map_add(map, "pname", pname);
ds_map_add(map, "pcolor", pcolor);
ds_map_add(map, "text", text);
ds_map_add(map, "tcolor", tcolor);

ds_list_add(lines, map);
So the text currently appears like this:



How would I be able to make the first message appear at the bottom and the following messages up?

Thanks in advance!

PS: Great to be back on community again!
 
M

Mishtiff

Guest
Hi friend,

I am not entirely certain, but here is my thought:

-change your for loop to the following-

for(var i = ds_list_size(lines); i > -1; i--) {

(Edited so you could only see my changes)

Also: I believe in your wins/losses area in the picture: Looses should be Losses* :)
 
Last edited by a moderator:

BLang

Member
Yup, you just have to invert your loop. However, I have to point out that the solution Mishtiff posted is going to throw an error.

What it should look like is:
for (var i=ds_list_size(lines)-1; i>-1; i--)

This is because ds_list_size returns the total amount of items in the list, but their actual indexes start from 0, which means that if there's, say, 50 items in the list, the index of the last item will actually be 49.
 

BMatjasic

Member
Thank you for your help friends, but I must inform you that I've already tried that method and retried it now and it still doesn't work. :/ It seems completly logical to me, but somehow it still prints the last message at the top instead on the bottom. @Mishtiff thank you for noticing the grammar mistake :p
 
R

rui.rosario

Guest
The mistake is that although you inverted the loop you still use i to set the y displacement, so even if you start at say index 20, it will still calculate to yy - 24*20 instead of yy - 24*0

I would change the code to
Code:
var xx = argument0;
var yy = argument1;
var yyy = yy
var size = ds_list_size(lines) - 1;
for(var i = size; i >= 0 ; i--) {

    var map = ds_list_find_value(lines, i);
    var pname = ds_map_find_value(map, "pname");
    var pcolor = ds_map_find_value(map, "pcolor");
    var text = ds_map_find_value(map, "text");
    var tcolor = ds_map_find_value(map, "tcolor");
        draw_set_halign(fa_left);
        draw_set_color(pcolor);
        draw_set_font(fnt_name);
        var str = string(pname) + " : ";
        draw_text(xx, yy - 24*i,str);
        draw_set_color(tcolor);
        draw_set_font(fnt_text);
        draw_text(xx + string_width(str)*1 , yy -  24 * (size - i),  text);

}
As you can see, in the last line I adapt the y displacement to be in the inverse order. With this line of thought I believe you won't even need the reversed loop
 
Last edited by a moderator:
M

Mishtiff

Guest
You are welcome BMatjasic! rui.rosario has your answer as well! I did not see your post until now that it was not working, but it would make sense that it prints in the same fashion, considering how your draw_text at the end is set up. I hope he solved your issue! :)
 
M

Mishtiff

Guest
let me look over the code again and see what i can find

EDIT: Can you repost the code at its current point please
 
R

rui.rosario

Guest
By some strange reason I get text drawn over the second. Why is that so?

http://prntscr.com/bkzzos

I am running out of ideas :/
Does it have newline characters? (Or in this case what GM acknowledges as a new line, the # character)

If so you may need to adapt the code to the vertical size of the string

EDIT: Something like:
Code:
var xx = argument0;
var yy = argument1;
var yyy = yy
var size = ds_list_size(lines) - 1;
for(var i = size; i >= 0 ; i--) {

    var map = ds_list_find_value(lines, i);
    var pname = ds_map_find_value(map, "pname");
    var pcolor = ds_map_find_value(map, "pcolor");
    var text = ds_map_find_value(map, "text");
    var tcolor = ds_map_find_value(map, "tcolor");

       // Use the yy variable to store the next position to draw in
       yy -= string_height(str) + 8; // The 8 here is for a little displacement

        draw_set_halign(fa_left);
        draw_set_color(pcolor);
        draw_set_font(fnt_name);
        var str = string(pname) + " : ";

        draw_text(xx, yy, str);

        draw_set_color(tcolor);
        draw_set_font(fnt_text);
        // Btw, why *1?
        draw_text(xx + string_width(str)*1 , yy,  text);
}
 

BMatjasic

Member
Guys I've fixed it! draw_clear_alpha solver the problem, so text doesn't remain in the same position. Works like a charm. Thanks for your time and help, I'll mark this post as solved.
 
Top