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

SOLVED "for" statement problem... (trying to make a dialogbox)

FoxyOfJungle

Kazan Games
Hi.
I have a ds_list, it contains dialogs for my dialogbox.

How I make:

CREATE:
GML:
messagebox_list = ds_list_create();
messagebox_show = true;
messagebox_n = 0;

ds_list_add(messagebox_list,"The quick brown fox jumps over the lazy dog");
ds_list_add(messagebox_list,"This is a long history...");
ds_list_add(messagebox_list,"...Was sometime ago...");
ds_list_add(messagebox_list,"[...]");

DRAW GUI:
GML:
if (messagebox_show)
{
    var _mxx = gui_w()/2-sprite_get_width(spr_hud_messagebox)/2;
    var _myy = gui_h()/2-sprite_get_height(spr_hud_messagebox)/2;
    draw_sprite_ext(spr_hud_messagebox,0,_mxx,_myy,1,1,0,c_white,1);
    draw_text_ext(_mxx+10,_myy+10,ds_list_find_value(messagebox_list,messagebox_n),-1,280);
  

    var _pxx = gui_w()/2-ds_list_size(messagebox_list)*8;
    var _pyy = _myy+sprite_get_height(spr_hud_messagebox)-20;
  
    for (i=0; i<ds_list_size(messagebox_list); i+=1)
    {
        draw_sprite(spr_hud_messagebox_n,0,_pxx+i*16, _pyy);
    }
  
    for (i=messagebox_n; i<ds_list_size(messagebox_list); i+=1)
    {
        draw_sprite(spr_hud_messagebox_n,1,_pxx+i*16, _pyy);
    }
  
  
  
    if keyboard_check_pressed(ord("X"))
    {
        messagebox_n += 1;
    }
  
    if (messagebox_n >= ds_list_size(messagebox_list))
    {
        ds_list_clear(messagebox_list);
        messagebox_n = 0;
        messagebox_show = false;
    }
}

The real problem is here:
GML:
for (i=0; i<ds_list_size(messagebox_list); i+=1)
{
    draw_sprite(spr_hud_messagebox_n,0,_pxx+i*16, _pyy);
}

for (i=messagebox_n; i<ds_list_size(messagebox_list); i+=1)
{
    draw_sprite(spr_hud_messagebox_n,1,_pxx+i*16, _pyy);
}
What I need? I want to make the red icon stay in the position it is in the message when I press X, see:

36453.png
(The image has been edited so that the stitch is the way I want it)


Thanks.
 
Last edited:

chamaeleon

Member
What I need? I want to make the red icon stay in the position it is in the message when I press X, see:
Draw what you need to draw that is not a red icon when i is not equal to messagebox_n, draw what you need to draw when you want to draw a red icon when i is equal to messagebox_n? Not that you have explained at all what the criteria is that drives the generation of whatever you are vaguely pointing to in the picture.

Side-note and not immediately relevant, I don't see you prefixing i with var to make it a local variable. It can mess with your head if you call scripts in a loop and you use the same variable in the script. Upon return from the script the loop will continue on with whatever the loop variable was set to in the script, which could result in either skipping steps, infinite loops, or other undesirable behavior.
 
You don't need a loop for the the red icon, you simply need a variable which holds which position in the list you are at, then you use the
Code:
draw_sprite(spr_hud_messagebox_n,1,_pxx+i*16, _pyy);
And replace i with whatever that variable is.
 

FoxyOfJungle

Kazan Games
Draw what you need to draw that is not a red icon when i is not equal to messagebox_n, draw what you need to draw when you want to draw a red icon when i is equal to messagebox_n? Not that you have explained at all what the criteria is that drives the generation of whatever you are vaguely pointing to in the picture.

Side-note and not immediately relevant, I don't see you prefixing i with var to make it a local variable. It can mess with your head if you call scripts in a loop and you use the same variable in the script. Upon return from the script the loop will continue on with whatever the loop variable was set to in the script, which could result in either skipping steps, infinite loops, or other undesirable behavior.
Thanks for reply. I didn't really notice the missing var variable, I'll put it, thanks. (I used the default GMS 2 code blocks for fast coding)



You don't need a loop for the the red icon, you simply need a variable which holds which position in the list you are at, then you use the
Code:
draw_sprite(spr_hud_messagebox_n,1,_pxx+i*16, _pyy);
And replace i with whatever that variable is.
How could I not have thought of that before? just solved my problem, many thanks!
edit:
148548.gif
 
Top