Legacy GM help with draw event for inventory with for loops

Hello. Using GMS Pro vers. 1.4.1757.

I posted not long ago about help setting up arrays to define my inventory. Thanks to community responses, I think I have that under control; now what I'm having trouble with is drawing the inventory correctly. Currently, when I pick up an item, it draws it in every slot of the inventory rather than just one.

Here is the create event for the inventory:
Code:
globalvar show_inv; //whether to display inventory
show_inv = false;

maxslots = 20;

//setting item ID and item count
for (i = 0; i < maxslots; i ++) //slot count
    {  
    global.inventory[i,0] = -1 // id of items in all slots are -1, no item
    global.inventory[i,2] = 0 //count of items in all slots set to 0
    coordx[i] = 0;
    coordy[i] = 0;
    }
   
space = 32;
rows = 4;
columns = 5;

//dimensions of black display box
x1 = view_xview[0]+80;
x2 = x1+240;
y1 = view_yview[0]+32;
y2 = y1 + 160;
   
global.inventory[6,0] = 2; //set inventory 6,0 to item id 2 (coffee)
global.inventory[6,2] = 5; // set quantity of item to 5
The last two lines are for testing purposes, in order to see if it will display the correct item (2) and quantity (5) in the correct box (6).

Now the draw event code:
Code:
for (xx = 0; xx < columns; xx++)
    for (yy = 0; yy < rows; yy ++)
            {
            draw_sprite(spr_box_16x16, 0, x1+space+(xx*space), y1+space+(yy*space)) //draws empty boxes
            for (i = 0; i < maxslots; i ++)
            coordx[i] = x1+space+(xx*space); 
            coordy[i] = y1+space+(yy*space);
            if global.inventory[i,0] > -1
                {
                draw_sprite(spr_items, global.inventory[i,0], coordx[i],coordy[i]);
                draw_set_colour(c_white);
                draw_text_transformed(coordx[i]+12,coordy[i]+13,string(global.inventory[i,2]), .5, .5, 0);
                }
            }
    }
The problem I'm seeing is that the coffee sprite and quantity are displayed in EVERY box, rather than just box 6. So the draw_sprite and draw_text functions are running for every value of i, but shouldn't only one of those values of i (6) be greater than -1?

Any help appreciated.
 

NicoDT

Member
Hi!
For what I can see, you are not opening brakets after the "xx" for, and after the "i" for (but you did open them after "yy" for).
Try with that and let me know if it works
 

NicoDT

Member
Try with this

Code:
for (xx = 0; xx < columns; xx++) {
    for (yy = 0; yy < rows; yy ++) {
            draw_sprite(spr_box_16x16, 0, x1+space+(xx*space), y1+space+(yy*space)) //draws empty boxes
            for (i = 0; i < maxslots; i ++) {
                if (global.inventory[i,0] > -1) {
                    coordx[i] = x1+space+(xx*space);
                    coordy[i] = y1+space+(yy*space);
                    draw_sprite(spr_items, global.inventory[i,0], coordx[i],coordy[i]);
                    draw_set_colour(c_white);
                    draw_text_transformed(coordx[i]+12,coordy[i]+13,string(global.inventory[i,2]), .5, .5, 0);
                }
            }
    }
}
 
S

Shihaisha

Guest
The problem here is not missing brackets (they are required, of course), but unnecessary for loops. Each slot should be drawn only once, but the first two for loops are drawing them multiple times.

Here is a more simple and working code for draw event:
Code:
xx = 1;
yy = 1;

for (i = 0; i < maxslots; i ++)
{
draw_sprite(spr_box_16x16, 0, x1+space+(xx*space), y1+space+(yy*space)) //draws empty boxes
coordx[i] = x1+space+(xx*space);
coordy[i] = y1+space+(yy*space);
if global.inventory[i,0] > -1
{
  draw_sprite(spr_items, global.inventory[i,0], coordx[i],coordy[i]);
  draw_set_colour(c_white);
  draw_text_transformed(coordx[i]+12,coordy[i]+13,string(global.inventory[i,2]), .5, .5, 0);
}
if(xx < columns){xx +=1;}else{xx=1;yy+=1;}
}
 
This works! I changed xx and yy to zero because otherwise the
" x1+space+(xx*space), y1+space+(yy*space)" was placing the first row and column one space too far. This necessitated dropping the number of columns and rows by 1 each (rows = 3, columns = 4), which is not ideal since 4 rows and 5 columns are actually being drawn. I'll have to work on that some more.

Either way, it's a big step forward. Thank you Shihaisha for pointing out the extra for loops. Your alternate solution to drawing the right number of rows and columns was quite clever.
 
Top