• 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 [SOLVED] Making a button highlight with room view.

Dr_Nomz

Member
So I want to make a button appear according to the room view (so it fits nicely) and have a little highlight over it when I mouse over.

Problem is, I've tried just about every combination I can think of, and so far nothing's worked.
Room view code (works fine):
Code:
  var x1,x2,y1,y2;
  x1 = view_xview[0];
  x2 = x1 + view_wview[0];
  y1 = view_yview[0];
Rectangle code:
Code:
if (abs(mouse_x - x) < 50) && (abs(mouse_y - y) < 50) && showInv == true{
  draw_set_color(c_white);
  draw_rectangle(x-47,y-47,x+46,y+46,0)
So that all goes into the draw event, but nothing I've done seems to make the highlight function correctly. See, instead of appearing over the sprite nicely, it seems to draw it self and function in a completely different area, and then a click event afterwords does nothing.

What I want is for the highlight to draw over the sprite where it's located on the screen.
 
Can you show us the code for all of the events, not just excerpts like you are at the moment? I'm only asking as you have missing code, but seem to be telling us that some things are working, but we have no way of knowing what it is you are doing if you don't show all the code.
 

Dr_Nomz

Member
Okay, that code is from a tutorial by Shaun Spalding for making an inventory system, and part of that involves the use of buttons, so here's the code for it:
Code:
globalvar showInv; //Show Inventory
showInv = true;
globalvar maxItems; //Total Item Slots
maxItems = 5;

for (i = 0; i < maxItems; i += 1){
  global.inventory[i] = -1;
  button[i] = instance_create(0,0,obj_Inventory_Button)
  button[i].slot = i;
}

globalvar mouse_item;
mouse_item = -1;
instance_create(0,0,obj_Inventory_Mouse);
obj_Inventory STEP - just a jump to position obj_Character action.
Code:
if (showInv){
  var x1,x2,y1,y2;
  x1 = view_xview[0];
  x2 = x1 + view_wview[0];
  y1 = view_yview[0];
  y2 = y1 + 104;
 
  draw_set_color(c_black);
  draw_set_alpha(0.8);
  draw_rectangle(x1,y1,x2,y2,0);
  draw_set_alpha(1);
 
  for (i = 0; i < maxItems; i += 1){
    var ix = x1+54+(i * 104);
    var iy = y2-51;
    
    draw_sprite(spr_Item_Slot_2,0,ix,iy)
    button[i].x = ix;
    button[i].y = iy;
  }
}

And the button it creates:
Code:
var item = global.inventory[slot];
var click = mouse_check_button_pressed(mb_left);
var RC = mouse_check_button_pressed(mb_right);

if (abs(mouse_x - x) < 50) && (abs(mouse_y - y) < 50) && showInv == true{
  draw_set_color(c_white);
  draw_rectangle(x-47,y-47,x+46,y+46,0)
//Move the item in the inventory.
  if (click){
    if (item != -1){
      scr_Item_Drop_Slot(slot);
    }
    if (mouse_item != -1){
      scr_Item_Pickup_Slot(mouse_item,slot);
    }
    mouse_item = item;
  }
}

  if (item != -1) && showInv == true{
  draw_sprite(spr_Item_Sheet,item,x,y);
}
 
Last edited:

Dr_Nomz

Member
All that code works just fine on it's own, and the inventory functions perfectly, I just can't seem to use the same code to make button on my own, similar to the one used here.
 

CloseRange

Member
the question is a bit confusing. You want a button that follows the view?
Code:
var x1 = view_xview + 16;
var y1 = view_yview + 16;
var x2 = x1 + 128;
var y2 = y1 + 32;
draw_set_color(c_white);
draw_rectangle(x1, y1, x2, y2, true);
you want it to be clickable?
Code:
if(point_in_rectangle(mouse_x, mouse_y, x1, y1, x2, y2)) {
    // if you are hovering over the button
    if(mouse_check_button_pressed(mb_left)) {
        // button clicked
    }
}
it's just a matter of adding view_xview and view_yview

Is it something else you are asking?
 

Dr_Nomz

Member
FINALLY I got it. Thank you!

Code:
var x1 = view_xview + 16;
var y1 = view_yview + 16;
var x2 = x1 + 128;
var y2 = y1 + 32;

draw_sprite(spr_Blue_TEXT,0,x1,y1);

if(point_in_rectangle(mouse_x,mouse_y,x1,y1,x2,y2)){
  // if you are hovering over the button
  draw_set_color(c_white);
  draw_rectangle(x1,y1,x2,y2,0);
  if(mouse_check_button_pressed(mb_left)){
    instance_destroy();
  }
}
Seems to work great, gonna mark as solved.
 
Top