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

GML need help with inventory

  • Thread starter Newbie Gamemaker
  • Start date
N

Newbie Gamemaker

Guest
hi everyone, i'm newbie. i'm doing the inventory in my first project. i got the trouble with the inventory that i create 9 slots in inventory and it is working but now i want to create more slots (adding 18 slots -same backpack that means when backpack open, it will shows more 18 slots) and the trouble is backpack open, i can not dragging item (move item form slots of inventory into backpack).i will adding the images that will show more detail. Here, the code is:
Code:
// Drag inventory
if mouse_check_button_pressed(mb_left) && !dragging {
    for(i=0;i<=8;i++) {
    if global.opentab = 0{ // backpack open:
 
       ix = view_wview/2-4*36+i*36;
       iy = view_hview-22 || iy =view_yview+300 || iy =view_yview+260;
       mx = mouse_x;
       my = mouse_y;
       if point_distance(ix,iy,mx,my) < 20 && oPlayer.inv[i] != -1 {
   
            oPlayer.clickedInv = true;
            dragging = true;
            dragID = i;
            onlyPress = true;
            pressX = mx;
            pressY = my;
        }
   
        }else
        {
        ix = view_wview/2-4*36+i*36;
        iy = view_hview-22;
        mx = mouse_x-view_xview;
        my = mouse_y-view_yview;
        if point_distance(ix,iy,mx,my) < 20 && oPlayer.inv[i] != -1 {
            oPlayer.clickedInv = true;
            dragging = true;
            dragID = i;
            onlyPress = true;
            pressX = mx;
            pressY = my;
        }
     
     
        }
    }
}
if mouse_check_button(mb_left) {
 
 
    mx = mouse_x-view_xview;
    my = mouse_y-view_yview;
    if point_distance(pressX,pressY,mx,my) > 4 {
        onlyPress = false;
 
 }
}
if mouse_check_button_released(mb_left) && dragging && !onlyPress {
    release_on_inv = false;
    for(i=0;i<=28;i++) {
    if global.opentab = 0{ //backpack open
     ix = view_wview/2-4*36+i*36;
     iy = view_hview-22 || view_yview+300 || view_yview+260;
     mx = mouse_x-view_xview;
     my = mouse_y-view_yview;
        if point_distance(ix,iy,mx,my) < 20 {
   
            release_on_inv = true;
            invID = i;
        }
        } else
        {
        ix = view_wview/2-4*36+i*36;
        iy = view_hview-22;
        mx = mouse_x-view_xview;
        my = mouse_y-view_yview;
        if point_distance(ix,iy,mx,my) < 20 {
            release_on_inv = true;
            invID = i;
        }
     
     
        }
    }
    if release_on_inv {
        if invID != dragID {
            if oPlayer.inv[invID] = oPlayer.inv[dragID] && stackable[oPlayer.inv[invID]] {
                while(oPlayer.invNum[invID] < 50 && oPlayer.invNum[dragID] > 0) {//10
                    oPlayer.invNum[invID] += 1;
                    oPlayer.invNum[dragID] -= 1;
                }
                if oPlayer.invNum[dragID] <= 0 {
                    oPlayer.inv[dragID] = -1;
                }
            }
            else {
                tmpInv = oPlayer.inv[invID];
                tmpInvNum = oPlayer.invNum[invID];
                tmpInvDurability = oPlayer.invDurability[invID];
                oPlayer.inv[invID] = oPlayer.inv[dragID];
                oPlayer.inv[dragID] = tmpInv;
                oPlayer.invNum[invID] = oPlayer.invNum[dragID];
                oPlayer.invNum[dragID] = tmpInvNum;
                oPlayer.invDurability[invID] = oPlayer.invDurability[dragID];
                oPlayer.invDurability[dragID] = tmpInvDurability;
            }
        }
    }
    else {
        repeat(oPlayer.invNum[dragID]) {
            drop_item(oPlayer.x+irandom_range(-6,6),oPlayer.y+irandom_range(-6,6),oPlayer.inv[dragID],oPlayer.invDurability[dragID]);
        }
        oPlayer.inv[dragID] = -1;
        oPlayer.invNum[dragID] = 0;
        oPlayer.invDurability[dragID] = -1;
    }
    dragging = false;
    dragID = -1;
}
 

Attachments

Phil Strahl

Member
I haven't looked at the code in detail, but *maybe* it has something to do that happens on line #3:
Code:
   for(i=0;i<=8;i++)
It looks like you're only checking the first 9 slots, so to check 18 i think it should read
Code:
 for (i = 0; i <= 17; i++)
 
N

Newbie Gamemaker

Guest
thank @Phil Strahl your answer. I already change it, yes. You'are right. When backpack open, there are 27 slots so that i change it:
Code:
for (i =0;  i <=26; i++)
but it's still not working. Anybody can help me? Thanks for reading.
 
T

trentallain

Guest
thank @Phil Strahl your answer. I already change it, yes. You'are right. When backpack open, there are 27 slots so that i change it:
Code:
for (i =0;  i <=26; i++)
but it's still not working. Anybody can help me? Thanks for reading.
Maybe try doing < instead of <= in that line?
 
N

Newbie Gamemaker

Guest
thank @trentallain for your help. I'm also change it but it's still not working so that i think this code:
Code:
for (i =0;  i <=26; i++)
no problem. Maybe problem is here:
Code:
 if global.opentab = 0{ // backpack open:
 
       ix = view_wview/2-4*36+i*36;
       iy = view_hview-22 || iy =view_yview+300 || iy =view_yview+260;
       mx = mouse_x;
       my = mouse_y;
       if point_distance(ix,iy,mx,my) < 20 && oPlayer.inv[i] != -1 {
  
            oPlayer.clickedInv = true;
            dragging = true;
            dragID = i;
            onlyPress = true;
            pressX = mx;
            pressY = my;
        }
  
        }
but i don't know how can i fix this? if someone know it, please help me. I am grateful to you for your help.
P/s: sorry all, i'm an idiot because i'm newbie here so that's reason i need you help.
 
T

trentallain

Guest
thank @trentallain for your help. I'm also change it but it's still not working so that i think this code:
Code:
for (i =0;  i <=26; i++)
no problem. Maybe problem is here:
Code:
 if global.opentab = 0{ // backpack open:
 
       ix = view_wview/2-4*36+i*36;
       iy = view_hview-22 || iy =view_yview+300 || iy =view_yview+260;
       mx = mouse_x;
       my = mouse_y;
       if point_distance(ix,iy,mx,my) < 20 && oPlayer.inv[i] != -1 {
 
            oPlayer.clickedInv = true;
            dragging = true;
            dragID = i;
            onlyPress = true;
            pressX = mx;
            pressY = my;
        }
 
        }
but i don't know how can i fix this? if someone know it, please help me. I am grateful to you for your help.
P/s: sorry all, i'm an idiot because i'm newbie here so that's reason i need you help.
Okay so there's a few things that need fixing in that.

First of all, use == instead of = in an if statement. = Sets the value, whereas == checks the value.

Second, put if statements that affect the whole for loop outside of it, otherwise this is inefficient.

Thirdly, I don't think you can set iy multiple values like that...

Fourthly make sure those variables like iy and ix are either temporary variables (by putting var in front of it) or they are set in the create event.

Next, try seperating your location calculations into temporary variables so that you can see what you are doing clearer.
 
N

Newbie Gamemaker

Guest
thank @trentallain for your answer.
Yes, i tried it by you. But the thirdly step as you said, i'm confusing about it because i don't know how to set iy (that's mean for the height of the backpack) and some trouble such as:
Code:
if point_distance(ix,iy,mx,my) < 20
i don't know how to fix because i think this can be problem of this.
 

Simon Gust

Member
I'm just going to drop my thoughts here.
The tutorial you Chose to make an inventory from isn't really the Right one I feel. You should've gone with a tutorial that is a real inventory not just a hotbar with extra functionallity.

The (imo) best way to make an inventory (with hotbar) is to have a giant loop that loops through every inventory Slot (including hotbar Slots) and the whole functionallity including the drawing of the Slots and items is in there.

example Code (in a draw GUI Event preverably)
Code:
slot_size = 36;

inventory_size = 32;
inventory_width = 8;

inventory_x = view_wview / 2 - inventory_width / 2 * slot_size;
inventory_y = view_yview + 260;

// inventory open / not open
var len = inventory_width; // just one row as hotbar
if (inventory_open) {
    len = inventory_size; // multiple rows as inventory and hotbar
}

// loop through every slot
for (var i = 0; i < len; i++)
{
    // position of a slot
    var xx = inventory_x + (i mod inventory_width) * slot_size;
    var yy = inventory_y + (i div inventory_width) * slot_size;
   
    // draw slot
    draw_sprite(spr_inventory_slot, 0, xx, yy)
   
    // check if there is an item in the inventory slot (assuming that the slots are filled with instance ids)
    var slot = global.inventory[i];
    if (slot != noone)
    {
        // draw item
        draw_sprite(spr_item, slot.index, xx+4, yy+4);
       
        // item functionallity here
        // check if mouse hovering over slot
        var hover = point_in_rectangle(mx, my, xx, yy, xx+32, yy+32);
        if (hover)
        {
            // click to pick up item etc...
           
           
        }
    }
    else
    {
        // if you have an item in hand you can put it down here
        var hover = point_in_rectangle(mx, my, xx, yy, xx+32, yy+32);
        if (hover)
        {
            // click to put down item etc...
           
           
        }
    }
}
the real magic happens where you define xx and yy.
using the functions mod and div lets you wrap around the positions so you get a sort-of grid inventory,
instead of a singular long row.
 
N

Newbie Gamemaker

Guest
I'm just going to drop my thoughts here.
The tutorial you Chose to make an inventory from isn't really the Right one I feel. You should've gone with a tutorial that is a real inventory not just a hotbar with extra functionallity.

The (imo) best way to make an inventory (with hotbar) is to have a giant loop that loops through every inventory Slot (including hotbar Slots) and the whole functionallity including the drawing of the Slots and items is in there.

example Code (in a draw GUI Event preverably)
Code:
slot_size = 36;

inventory_size = 32;
inventory_width = 8;

inventory_x = view_wview / 2 - inventory_width / 2 * slot_size;
inventory_y = view_yview + 260;

// inventory open / not open
var len = inventory_width; // just one row as hotbar
if (inventory_open) {
    len = inventory_size; // multiple rows as inventory and hotbar
}

// loop through every slot
for (var i = 0; i < len; i++)
{
    // position of a slot
    var xx = inventory_x + (i mod inventory_width) * slot_size;
    var yy = inventory_y + (i div inventory_width) * slot_size;
  
    // draw slot
    draw_sprite(spr_inventory_slot, 0, xx, yy)
  
    // check if there is an item in the inventory slot (assuming that the slots are filled with instance ids)
    var slot = global.inventory[i];
    if (slot != noone)
    {
        // draw item
        draw_sprite(spr_item, slot.index, xx+4, yy+4);
      
        // item functionallity here
        // check if mouse hovering over slot
        var hover = point_in_rectangle(mx, my, xx, yy, xx+32, yy+32);
        if (hover)
        {
            // click to pick up item etc...
          
          
        }
    }
    else
    {
        // if you have an item in hand you can put it down here
        var hover = point_in_rectangle(mx, my, xx, yy, xx+32, yy+32);
        if (hover)
        {
            // click to put down item etc...
          
          
        }
    }
}
the real magic happens where you define xx and yy.
using the functions mod and div lets you wrap around the positions so you get a sort-of grid inventory,
instead of a singular long row.
ok, Thank you so much. i will try this
 
Top