[SOLVED] How to best change button graphic when clicking on inventory item

Started by Gepard, Tue 01/11/2016 15:52:56

Previous topic - Next topic

Gepard

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!
Drink up me 'arties! Yo ho!

Crimson Wizard

Do you need help determining when player clicked on item, or changing image?

Code: ags

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.

Gepard

Drink up me 'arties! Yo ho!

Gepard

Code: ags
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?
Drink up me 'arties! Yo ho!

Cassiebsg

try adding this line for debug purpose
Code: ags

    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
        }
    }
There are those who believe that life here began out there...

Gepard

Thanks. I've already tried adding a few messages like that all over the code. Not one of them fires up.
Drink up me 'arties! Yo ho!

Gepard

This is the whole code:

Code: ags
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.
Drink up me 'arties! Yo ho!

Crimson Wizard

Go to General Settings and make sure that "Process inventory clicks in script" is True.

Gepard

Thanks Crimson, had to change the "Override built-in inventory window click handling" in General settings. Youre the best.
Drink up me 'arties! Yo ho!

Crimson Wizard

Oh, right, its "override" one, I forgot how it is called exactly :).

SMF spam blocked by CleanTalk