Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gepard on Tue 01/11/2016 15:52:56

Title: [SOLVED] How to best change button graphic when clicking on inventory item
Post by: Gepard on Tue 01/11/2016 15:52:56
Hello everyone! I have a simple question. I have an inventory window and three buttons on a GUI. I would like to change the graphic of the buttons when player clicks on an inventory item, so that the button looks the same as the inventory item itself (to have the same sprite). Can anyone suggest the best way to do this, so my script won't become a mess? Thank you!
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Crimson Wizard on Tue 01/11/2016 16:00:34
Do you need help determining when player clicked on item, or changing image?

Code (ags) Select

function on_mouse_click(MouseButton btn)
{
    <... probably some other code...>
    if (btn == eMouseLeftInv)
    {
        InventoryItem *item = Inventory.GetAtScreenXY(mouse.x, mouse.y);
        MyButton.Graphic = item.Graphic;
    }
    <... probably some other code...>
}


E: I noticed you said "click on item" not "select item", so I changed the code.
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Gepard on Tue 01/11/2016 16:03:42
Thanks a lot!
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Gepard on Tue 01/11/2016 19:42:52
Code (ags) Select
function MouseInventory (MouseButton btn) {
if (btn == eMouseLeftInv) {
    InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
    if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
    else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
    else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
    }
}


So far I have this, but nothing is happening. :undecided: Any idea why?
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Cassiebsg on Tue 01/11/2016 20:16:52
try adding this line for debug purpose
Code (ags) Select

    function MouseInventory (MouseButton btn) {
    if (btn == eMouseLeftInv) {
        InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
        if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
        else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
        else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
        else Display ("Oops... none of my if is true!"); // Line for debugging
        }
    }
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Gepard on Tue 01/11/2016 20:27:26
Thanks. I've already tried adding a few messages like that all over the code. Not one of them fires up.
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Gepard on Tue 01/11/2016 20:51:54
This is the whole code:

Code (ags) Select
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{   
  if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
  {}
  else if (button == eMouseLeft) {
    ProcessClick(mouse.x,mouse.y, mouse.Mode);
  }
  else if (button == eMouseLeftInv) {
    InventoryItem *item = InventoryItem.GetAtScreenXY (mouse.x, mouse.y);
    if (btnWord1.Graphic == 11) btnWord1.NormalGraphic = item.Graphic;
    else if (btnWord2.Graphic == 11) btnWord2.NormalGraphic = item.Graphic;
    else if (btnWord3.Graphic == 11) btnWord3.NormalGraphic = item.Graphic;
    }
  else // right-click, so cycle cursor
  {   
    mouse.SelectNextMode();
  }
}


Before I made a new function. This time I put it under mouse click script. Still, it doesn't work. Inventory window opens and mouse mode is set to interact. When I click on inventory item, the cursor just changes to the image of the item as usual but no change in button graphic.
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Crimson Wizard on Wed 02/11/2016 08:46:34
Go to General Settings and make sure that "Process inventory clicks in script" is True.
Title: Re: How to best change button graphic when clicking on inventory item
Post by: Gepard on Wed 02/11/2016 14:42:41
Thanks Crimson, had to change the "Override built-in inventory window click handling" in General settings. Youre the best.
Title: Re: [SOLVED] How to best change button graphic when clicking on inventory item
Post by: Crimson Wizard on Wed 02/11/2016 14:58:25
Oh, right, its "override" one, I forgot how it is called exactly :).