GMS 2 Need DS_Grid Help

A

Al the Coder

Guest
I am new to programming with GMS in its entirety, and I need a bit of help.
I have a Ds_Grid set up fully with visuals for my inventory system, but the issue is that I have no idea how to grab the array values from the ds_grid relative to the mouse position on the inventory GUI. The inventory is currently situated at the bottom left side of my room when running. So i guess any help or tutorials you can come up with to show me how this can be done would be appreciated.

Code just in case anyone needs to look:

Draw Event:

Code:
draw_self();
draw_set_color(my_color);

draw_text(bbox_left + 85, bbox_top + text_border, "Name");

item_left_start = bbox_left + 120;
item_top_start = bbox_top + 48;

for (i = 0; i < inventory_end_at; i += 1)
{
    for (j = 0; j < inventory_width; j += 1)
    {
        inventory_on_screen = i;
        if (j == 0)
        {
            draw_text(item_left_start - 43, item_top_start + (i * 32), ds_grid_get(my_items, 0, i + scrolled_amount));
        }
        if (j == 3)
        {
            draw_sprite(ds_grid_get(my_items, j, i + scrolled_amount), 0, bbox_left + 45, item_top_start + (i * 32) + 10);   
        }
    }
}

draw_rectangle(bbox_left + text_border, item_top_start + ((item_selected - scrolled_amount) * 32) -7, bbox_right - text_border, item_top_start + ((item_selected - scrolled_amount) * 32) + 25, true);

draw_sprite(s_border_desc_window, 0, bbox_left + sprite_get_xoffset(s_border_desc_window), room_height - sprite_get_yoffset(s_border_desc_window) - 8);

if (is_empty)
{
    draw_text_ext(bbox_left + 28, room_height - sprite_get_yoffset(s_border) - 210, empty_message, 32, sprite_get_width(s_border) - text_border);
    my_color = c_red;
}
else
{
    draw_text_ext(bbox_left + 28, room_height - sprite_get_yoffset(s_border) - 210, ds_grid_get(my_items, 2, item_selected), 32, sprite_get_width(s_border) - text_border);
    my_color = c_black;
}

if (ds_grid_height(my_items) > floor((sprite_height - (text_border * 3)) / 32))
{
    draw_sprite(s_scrollbar, 0, bbox_right - 16, 25 + bbox_top + item_selected * (sprite_height - text_border) / ds_grid_height(my_items));   
}

draw_rectangle(bbox_left, bbox_top + 40, bbox_right, bbox_bottom, true);
if (mouse_x >= bbox_left && mouse_y >= bbox_top + 40 && mouse_x <= bbox_right && mouse_y <= bbox_bottom)
{
    show_debug_message("In bounds")

}
At the bottom here I have it checking to see if the mouse x and mouse y are pointing inside the inventory gui and give me output and a rectangle visually show me my bounds. My goal here is to be able to click an object within this window and drag the "sprite" or "Object" out to be able to use it elsewhere in room.

If there is a better or easier way to accomplish this let me know. Also feel free to correct my code of any bad coding (if any). Thanks
 

Rob

Member
Hey man,

Inside your second for loop (the one that's drawing the inventory) this is where I would check for the mouse (because we want to check for the mouse for every part of your inventory).

All you need to do is check for device_mouse_x_to_gui and device_mouse_y_to_gui and check each area of the entry to see if the mouse is there.

This area will need to be the area that you're drawing each specific part of your inventory, hence why it goes in the second for loop.

Part of my inventory tutorial covers this: (skip to 19:00)



Your code could be a little easier for you if you define things like this (and also stop you having to check for the mouse twice)

Code:
var startDrawX = bbox_left + 120;
var startDrawY = bbox_top + 48;
var currentSlot = 0; //Keeps track of which entry in the grid we're at
var mx = device_mouse_x_to_gui(0);
var my = device_mouse_y_to_gui(0);

//Assuming you're drawing 2 columns
for (var xx = 0; xx < 2; xx ++){
   for (var yy = 0; yy < inventory_width; yy ++){
       var invDrawX = (startDrawX + (xx * 32);
       var invDrawY = (startDrawY + (yy * 32);

      draw_text(invDrawX, invDrawY, ds_grid_get(my_items, 0, currentSlot + scrolled_amount);
  
      //draw your sprite - I keep getting confused on what "j" does

      //Check for mouse - assuming sprites are centered
      if (abs(invDrawX - mx) < 16) && (abs(invDrawY - my) < 16){
          //Mouse is over this particular Slot
          //Now you can say something like selectedSlot = currentSlot;
      }

      currentSlot ++;

   }
}
 
Last edited:
A

Al the Coder

Guest
I appreciate your help and I liked your tutorial video as well thank you. Currently its not a Draw GUI event, its just a normal draw event. So I'm not sure how thats going to react with device_mouse_x/y_to_gui. I am drawing two columns with my for loops. "i" loops through all current items in inventory, and "j" is looping through the grid width to auto align all sprites, names and other grid data. This also allows me to add / remove columns to my gui. Should I maybe think about changing my event to Draw Gui event?
 

Rob

Member
I appreciate your help and I liked your tutorial video as well thank you. Currently its not a Draw GUI event, its just a normal draw event. So I'm not sure how thats going to react with device_mouse_x/y_to_gui. I am drawing two columns with my for loops. "i" loops through all current items in inventory, and "j" is looping through the grid width to auto align all sprites, names and other grid data. This also allows me to add / remove columns to my gui. Should I maybe think about changing my event to Draw Gui event?
If you're not using the draw_gui event then mouse_x and mouse_y should work instead of the other functions I suggested.

If you want something to appear on the screen all the time, regardless of where the player has moved to, that's a good time to use draw_gui.

Things like healthbars, inventory, scores, buttons - anything that you want to keep in the same position on the screen.
 
Last edited:
A

Al the Coder

Guest
Hey I added the relevant code and I was able to get it to work perfectly. Thanks for you help Rob you are a champ!
 
Top