GameMaker: Studio

GameMaker: Studio

View Stats:
Help - Drawing Inventory with a FOR loop
Help! I have created an inventory from Shaun Spalding inventory tutorial. The inventory he does is drawn on one row. I want to make multiple rows for my inventory like a 5 by 5 inventory. I have been trying to find a way but could not find one. Soo if any one can help, plz do.

Here is the code:

obj_inventory Create Event
///Initilize Inventory
globalvar show_inv; //Display the inventory
show_inv = true;
globalvar max_items; //Total item slots
max_items = 25;

for (i = 0; i < max_items; i += 1)
{
global.inventory = -1; //Set inventory empty
button = instance_create(0,0,obj_inv_button); //Creates button for each slot
button.slot = i; //Set button.slot it i
}

globalvar mouse_item; // Create varible to craay item
mouse_item = -1;
instance_create(0,0,obj_mouse_item); //Create mouse item

obj_inventory Draw Event
///Draw Inventory
if(show_inv)
{
var x1 = view_xview[0] + 96; //x cordinate of first slot
var y1 = view_yview[0] + 72; //y cordinate of first slot

draw_sprite(spr_inventory,0,x1,y1); //Draws the inventory background

for(i = 0; i < max_items; i += 1)
{
draw_sprite(spr_inventory_slot,0,x1 + 24+(i * 24),y1 + 24); //Draw Slot
button.x = x1 + 24 + (i * 24); //Set x cordinate of button
button.y = y1 + 24; //Set y cordinate of button
}
}

Basicly there are three main object
obj_inventory: Controlles the inventory
obj_inv_button: Button thats hovers over each slot
obj_mouse_item: The object that carries item from one slot to the other

The main line where that the slots are being drawn on are here in obj_inventory's Draw Event
for(i = 0; i < max_items; i += 1)
{
draw_sprite(spr_inventory_slot,0,x1 + 24+(i * 24),y1 + 24); //Draw Slot
button.x = x1 + 24 + (i * 24); //Set x cordinate of button
button.y = y1 + 24; //Set y cordinate of button
}
This draws the 25 slots then makes a button hover over each slot.

So if anyone can figure how to make this draw a 5x5 inventory, plz tell me.
< >
Showing 1-5 of 5 comments
Diveyoc Dec 10, 2016 @ 12:26am 
I think there is a free download in the YoYo marketplace for this. It looked exactly like what Shaun did except with a 5x5 grid.
The Winter Bud Dec 10, 2016 @ 1:10am 
without the engine, it's hard to test on my end, but something like this might work
var xx,yy; for(i = 0; i < max_items; i += 1){ xx=i mod 5; yy=i div 5; draw_sprite(spr_inventory_slot,0,x1 + 24+(xx* 24),y1 + 24+(yy*24)); //Draw Slot button.x = x1 + 24 + (xx* 24); //Set x cordinate of button button.y = y1 + 24+(yy*24); //Set y cordinate of button }
Donut Lord 64 Dec 10, 2016 @ 2:34am 
Thanks. I will go test that now.
Donut Lord 64 Dec 10, 2016 @ 2:52am 
I just tested that but it didn't fully work. What it did was make 2 rows instead of one. I think changing xx and yy around will fix. I am just going to play around with the numbers and see what happens for now but if you now what went wrong tell me.
Donut Lord 64 Dec 10, 2016 @ 2:56am 
Doesn't matter. I found out what went wrong. On my project I only had max items set to 10. When I set it to 25 it work fine. Thanks for the help Dennis Tudor Jr.
< >
Showing 1-5 of 5 comments
Per page: 1530 50

Date Posted: Dec 9, 2016 @ 11:32pm
Posts: 5