#draw different strings from different haligns depending on a value

1 messages · Page 1 of 1 (latest)

night yew
#

alright so here's some code i have:

for(var i = 0; i < array_length(text_messages)-1; i++) {
    switch(text_messages[i].from) {
        case -1:
            draw_set_halign(fa_right);
            draw_text((view_wport[0]/2)+((string_width(text_messages[current_message].msg)*text_messages[i].from)/5),text_messages[current_message].y_val+(string_height(-1)*current_message),text_messages[current_message].msg);
            break;
        case 1:
            draw_set_halign(fa_left);
            draw_text((view_wport[0]/2)+((string_width(text_messages[current_message].msg)*text_messages[i].from)/5),text_messages[current_message].y_val+(string_height(-1)*current_message),text_messages[current_message].msg);
            break;
    }
}

now it looks kinda messy, but basically i'm trying to go through an array of structs, each of which holds a string, and a from value. i thought about using a for to check for the from value, but when i do this it draws each string from both haligns, at once. whats the better way to do this?

#

this is how it should look, basically

#
text_messages = [
    {msg : "hey thanks again for getting me this doll. super creepy lol", from : 1, y_val : 0},
    {msg : "Yeah, I really love garage sales. Def a nice find!", from : -1, y_val : 0},
    {msg : "I knew you'd like it too! Thought it would make a nice addition to your decor.", from : -1, y_val : 0},
    {msg : "it's perfect! thank u so so much dude", from : 1, y_val : 0},
    {msg : "No problem :)", from : -1, y_val : 0},
    {msg : "Aight it's late, gonna head to bed. Gnight max", from : -1, y_val : 0},
    {msg : "gn", from : 1, y_val : 0}
]

the array in question

#
(view_wport[0]/2)+(
    (string_width(text_messages[current_message].msg) * text_messages[i].from) / 5
), // x
text_messages[current_message].y_val + (string_height(-1) * current_message) // y

edited the positioning for readability

silent plume
#

works fine for me

#

only change I made was in the for loop, var current_message = i because that variable wasn't provided

night yew
#

wait i forgot a piece of code sorry hold on

#
if(keyboard_check_pressed(vk_enter)) current_message++;

current_message is a variable created at 0, and it increases by 1

#

its meant to be like a text message thing

silent plume
#

you want the messages to display up to the current_message?

night yew
#

yea

#

im going to work on changing it to have like text bubbles and animation etc, but rn im just wanting to get the basic framework down

silent plume
#

alright, I got it figured out

#
  1. your for loop is exiting early by 1
  2. you need to exit out of the for loop when i is greater than current_message
  3. (future optimization) the draw_text y value should be incremeted separately from just the current text's height. suppose you have a line break or text wrap
  4. you don't want to draw the text from the current_message, you want to draw the text from i. for example: you have text_messages[current_message].msg
#

hope that helps, let me know if you have any questions

#

or need the code to compare after trying it yourself

night yew
#

so at the end of the for loop, i added

#
if i > current_message break;
#

for point 2

#

and in the draw_texts i changed [current_message] to [i]

#

is that what you meant?

silent plume
night yew
#

alright

#

so this is the current code

#
or(var i = 0; i < array_length(text_messages)-1; i++) {
    if i > current_message break;
    switch(text_messages[i].from) {
        case -1:
            draw_set_halign(fa_right);
            draw_text((view_wport[0]/2)+((string_width(text_messages[current_message].msg)*text_messages[i].from)/5),text_messages[current_message].y_val+(string_height(-1)*current_message),text_messages[i].msg);
            break;
        case 1:
            draw_set_halign(fa_left);
            draw_text((view_wport[0]/2)+((string_width(text_messages[current_message].msg)*text_messages[i].from)/5),text_messages[current_message].y_val+(string_height(-1)*current_message),text_messages[i].msg);
            break;
    }
}
if(keyboard_check_pressed(vk_enter)) {
    if current_message < array_length(text_messages)-1 current_message++;
}
#

again ignore the messiness

#

lemme record this

silent plume
#

looks good to me, ship it!

night yew
silent plume
#

you're still referencing text_messages[current_message]

#

to make your life easier, make a variable that holds the current text message you're trying to draw

#
var _message = text_messages[i];
night yew
#

changed every mention to _message (which is text_messages[i].msg)

silent plume
#

surprised it didn't throw an error

#

can you show what you have?

night yew
#

the whole code?

#

for this part at least

silent plume
#

draw event code

night yew
#
if !surface_exists(message_surface) message_surface = surface_create(1280,720) else {
    surface_set_target(message_surface);
    
    draw_set_font(fnt_text_message);
    for(var i = 0; i < array_length(text_messages)-1; i++) {
        if i > current_message break;
        var _message = text_messages[i].msg;
        switch(text_messages[i].from) {
            case -1:
                draw_set_halign(fa_right);
                draw_text((view_wport[0]/2)+((string_width(_message)*text_messages[i].from)/5),text_messages[i].y_val+(string_height(-1)*current_message),_message);
                break;
            case 1:
                draw_set_halign(fa_left);
                draw_text((view_wport[0]/2)+((string_width(_message)*text_messages[i].from)/5),text_messages[i].y_val+(string_height(-1)*current_message),_message);
                break;
        }
    }
    if(keyboard_check_pressed(vk_enter)) {
        if current_message < array_length(text_messages)-1 current_message++;
    }
    
    //window_set_caption((string_height(text_messages[current_message > 0 ? current_message-1 : 0].msg)))
    surface_reset_target();
}

draw_surface(message_surface,0,0);
silent plume
#

I'm just looking at the loop

#

var _message = text_messages[i].msg; is supposed to be var _message = text_messages[i]; because we want to know the current struct we're using, we have other variables we need to reference

#

then any other instances of text_messages[i] can be _message

#

string_width(_message) we want to get the width of the msg string: string_width(_message.msg)

night yew
#
var _message = text_messages[i];
switch(_message.from) {
    case -1:
        draw_set_halign(fa_right);
        draw_text((view_wport[0]/2)+((string_width(_message.msg)*_message.from)/5),_message.y_val+(string_height(-1)*current_message),_message.msg);
        break;
    case 1:
        draw_set_halign(fa_left);
        draw_text((view_wport[0]/2)+((string_width(_message.msg)*_message.from)/5),_message.y_val+(string_height(-1)*current_message),_message.msg);
        break;
}
}
#

does make it neater

#

ok real quick

#

gonna try something

#

current_message is always less than i

#

so is if i > current_message break; obsolete then?

#

no wait

silent plume
night yew
#

so i is ALWAYS greater than current_message until the end

#

for some reason

#

hm

silent plume
#

did you fix #1?

#
  1. your for loop is exiting early by 1
night yew
#

so

#
for(var i = 0; i < array_length(text_messages)-1; i++) {
#

do i get rid of the -1?

silent plume
#

yup

night yew
#

alright

silent plume
#

the loop will continue as long as your expression is true

night yew
#

now i is always greater than current_message

#

so the loop should break bc of that check

#

but it doesnt

silent plume
#

how are you updating the title?

night yew
#

window_set_caption("current_message = "+string(current_message)+"; i = "+string(i));

silent plume
#

and where?

night yew
#

im lazy and prefer to set caption rather than draw text

#

right at the start of the loop

silent plume
#

so that explains why i is greater than current_message

#

you're updating your title before checking if you need to break out of the loop

#

update your title just after the break

night yew
#

ok that did that

#

cool

#

so the issue left now (?)

#

is that it's drawing every text up to i

silent plume
#

alright, let's see what your loop looks like now

night yew
#
for(var i = 0; i < array_length(text_messages); i++) {
        
    if i > current_message break;
    window_set_caption("current_message = "+string(current_message)+"; i = "+string(i));
    var _message = text_messages[i];
    switch(_message.from) {
        case -1:
            draw_set_halign(fa_right);
            draw_text((view_wport[0]/2)+((string_width(_message.msg)*_message.from)/5),_message.y_val+(string_height(-1)*current_message),_message.msg);
            break;
        case 1:
            draw_set_halign(fa_left);
            draw_text((view_wport[0]/2)+((string_width(_message.msg)*_message.from)/5),_message.y_val+(string_height(-1)*current_message),_message.msg);
            break;
    }
}
silent plume
#

your y position needs to be incremented by i, not the current_message

night yew
#

holy shit

#

that did it

#

thank you so much

silent plume
#

you're welcome!

#

do you want to go for the extra credit?

night yew
silent plume
#

#3

#
  1. (future optimization) the draw_text y value should be incremeted separately from just the current text's height. suppose you have a line break or text wrap
night yew
#

string_height_ext?

silent plume
#

string_height would work for now

#

you need to have a variable store the current y position you're drawing at

#

and then increment that by the string's height and padding

night yew
#

actually im not even sure why i have y_val

silent plume
#

you added that to your structs, they're all 0 anyways, that could probably be removed