Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: rongel on Mon 17/06/2019 08:52:24

Title: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Mon 17/06/2019 08:52:24
Hi!

I'm working on a small alchemy puzzle. I'm using a separate GUI and a inventory window that has all the ingredients that the player has collected (it's assigned to a dummy character). I want the inventory items act like buttons, and have the button pressed graphic.

My problem is that my custom button down graphic always activates too late, it happens when I release the mouse button. I want it to act like a GUI button, so that when I click it (and keep the mouse button pressed), the graphic changes instantly, but nothing else happens yet. Then when I release the button, the action registers, and the button graphic changes back.

I hope you got my point! This sounds like trivial thing, but it's much more enjoyable to click things that act like buttons.

Any ideas?
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: Cassiebsg on Mon 17/06/2019 12:33:42
Somehow I have a feeling that it would be easier to just use a normal GUI with buttons... Then just check what items the player has collected and fill in the buttons graphics and turn them visible.
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Mon 17/06/2019 12:47:08
Quote from: Cassiebsg on Mon 17/06/2019 12:33:42
Somehow I have a feeling that it would be easier to just use a normal GUI with buttons... Then just check what items the player has collected and fill in the buttons graphics and turn them visible.

Thanks for the reply!

Yes, I have a store location that has buttons for items on sale. But I couldn't figure out how to fill the list of items in a proper way. Some items appear on sale later on and depending on the actions of the player, so their order isn't fixed. With inventory items I can "easily" make flexible order, so that the items are nicely placed. But I miss the smooth button interactions...

I could use GUI buttons too, but I'd need some help how to keep the stuff in order, so that there won't be ugly empty spaces. 
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: Cassiebsg on Mon 17/06/2019 12:51:06
You can just fill the buttons as arrays, I believe.
Has player item X? yes, fill button array 1, has player have item Y? no... has player have item Y? yes, fill button array 2... later on, has player item Y? yes, fill array (the next empty one) 3...

EDIT: I'm though not sure if you lose items, so if you do, you would need a function to fill in the empty slot for the lost item... like lost item X, that was on button 1... so you now need to move the item in button 2 to btn 1,3 to 2, and so on...
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Mon 17/06/2019 13:34:57
Quote from: Cassiebsg on Mon 17/06/2019 12:51:06

EDIT: I'm though not sure if you lose items, so if you do, you would need a function to fill in the empty slot for the lost item... like lost item X, that was on button 1... so you now need to move the item in button 2 to btn 1,3 to 2, and so on...

Yes, you lose ingredients after using them, so the order changes a lot. Scripting the item/button order seems a bit daunting, but it might be that I have to do that. I'll probably ask for more help if I do it!  I was hoping that there would be a simple trick to mimic the button pressed graphic with inventory items though...
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: Cassiebsg on Mon 17/06/2019 14:58:06
Maybe there is, I'm not exactly an experience coder...
Maybe someone else with more experience will give you a better solution and maybe even code.  ;)
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Tue 18/06/2019 14:53:08
Quote from: Cassiebsg on Mon 17/06/2019 14:58:06
Maybe there is, I'm not exactly an experience coder...
Maybe someone else with more experience will give you a better solution and maybe even code.  ;)

Thanks anyway!

I had some success testing the button pressed graphics. It was almost too easy, there must be some horrible bug that I haven't encountered yet. My code is :

Code (ags) Select

// In repeatedly_execute
if(InventoryItem.GetAtScreenXY(mouse.x, mouse.y) == iAquaFortis && mouse.IsButtonDown(eMouseLeft)) iAquaFortis.Graphic =5248;
else if(iAquaFortis.Graphic != 5247) iAquaFortis.Graphic =5247;


The problem is that I might have up to 20 items, so I would have to script each item separately.  I can do that ofcourse, but I wonder if there is a simpler way to do this. Any thoughts? Or comments on the script?
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: Cassiebsg on Tue 18/06/2019 15:31:27
As as non-experienced coder, I would start by coding 3 or 4 items, then see exactly what changes in your code and then use pointers and arrays to make it a simple function that changes accordingly.

You might want to use costume properties.
For instance create one called "button pressed" and another "button normal". Fill in the sprite number for each item...
So for iAquaFortis the button pressed would be 5248 and the normal 5247.

When you then code that bit, you can check the items property and change the graphics accordingly... and you have reduced those 2 lines of code to work for all your items.  ;)
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Tue 18/06/2019 15:56:37
Quote from: Cassiebsg on Tue 18/06/2019 15:31:27
You might want to use costume properties.
For instance create one called "button pressed" and another "button normal". Fill in the sprite number for each item...
So for iAquaFortis the button pressed would be 5248 and the normal 5247.

That might be a good idea, thanks! Will test it tomorrow... 
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: Cassiebsg on Tue 18/06/2019 16:04:13
Oh, and you might want one to check if the item is usable in that puzzle or not... or just check for an empty field == not usable...
Title: Re: Make inventory item to act like a GUI button (button pressed graphic)
Post by: rongel on Wed 19/06/2019 12:39:34
Getting closer but not quite there yet...

Ok, so this was my first attempt, it's simple and works fine as long as the mouse stays on the inventory item. But when "i" is null (mouse is not touching any items), the item still uses the pushed down graphic:

Code (ags) Select

// BUTTON PRESSED GRAPHICS
InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);

if (i != null) {
  if(mouse.IsButtonDown(eMouseLeft)) {
    if(i.Graphic == i.GetProperty("Button normal")) i.Graphic = i.GetProperty("Button pressed");
  }
  else if(i.Graphic == i.GetProperty("Button pressed")) i.Graphic = i.GetProperty("Button normal");
}


So I made another attempt and tried to store the ID number of the item. So when the mouse isn't touching the items, the item with the store ID number resets. It seems to work a bit better:

Code (ags) Select

// BUTTON PRESSED GRAPHICS
int store_id =0;

InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);

if (i != null) {
  if(mouse.IsButtonDown(eMouseLeft)) {
    if(i.Graphic == i.GetProperty("Button normal")) {
      i.Graphic = i.GetProperty("Button pressed");
      store_id = i.ID;
    }
  }
  else if(i.Graphic == i.GetProperty("Button pressed")) i.Graphic = i.GetProperty("Button normal");
}
if(i == null && store_id !=0) inventory[store_id].Graphic = inventory[store_id].GetProperty("Button normal");


The only problem seems to be  that if you keep the mouse button down, and place the cursor on another item, so that there is constantly an item selected, the system gets messed up. So there should be a script if the selected item changes. Or maybe some more elegant solution...