[SOLVED] Custom inventory issue - on_mouse_click / if (button == eMouseLeft)

Started by WHAM, Mon 08/08/2011 18:00:15

Previous topic - Next topic

WHAM

I have a custom inventory. I wanted to create custom on_mouse_click interactions with it, instead of using the system which comes with AGS.

If I set, in general settings, the "Override built-in inventory window click handling" to false, the GUI detects clicks on the inventory item correctly and I can select an item as active inventory item with the interact mouse cursor.

I wanted to alter this functionality in some ways, and set the above mentioned setting to true, which causes the game to call "on_mouse_click" even inside inventory windows as usual.

To test the issue I'm about to describe, I've simplified the code as below.

Code: ags

function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
  Display("click");
  if (button == eMouseLeft) {
    Display("left");
  } else if (button == eMouseRight) {
    Display("right");
  } else {
    Display("fail");
  }
  ProcessClick(mouse.x, mouse.y, eModeInteract);
}


The Display commands I've set up reveal that on_mouse_click command is correctly called (game displays ("click"), but then it gets weird: the check for which mouse button was clicked fails to detect which button was clicked. The same check works outside of the inventory GUI, but inside the inventory GUI it seems that the if -lines are never run at all and if I click on an inventory item, the script above displays "fail" on screen, denoting that the script did not detect left-click or right-click, but instead went for the "else" option.

What causes this behavior inside an inventory GUI?
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Khris

If you left-click on an inventory item, on_mouse_click is called with button being eMouseLeftInv.
There's also eMouseRightInv (and eMouseMiddleInv, I think).

The item can be read using inventory[game.inv_activated].

Clicking on the empty area or on the GUI itself will call the respective GUI/GUIControl's OnClick function, if clickable and linked.

Here's my standard way of handling inv clicks:

Code: ags
  InventoryItem*ia = inventory[game.inv_activated];

  ...
  else if (button == eMouseLeftInv) {
    if (mouse.Mode == eModeUseinv) ia.RunInteraction(eModeUseinv);  // use active item on clicked item
    else player.ActiveInventory = ia;  // select clicked item
  }
  else if (button == eModeRightInv) {
    if (mouse.Mode == eModeUseinv) player.ActiveInventory = null;  // de-select active item
    else {
      String d = ia.GetTextProperty("desc");  // custom property contains item's description
      if (d != "process") player.Say(d);
      else ia.RunInteraction(eModeLookat);ia.RunInteraction(eModeLookat);  
    }
  }


This method of looking at inv items prevents you from having to create and link a function for each item when all they do is display a description.
In special cases, put "process" as descriptive text and AGS will call the function.

WHAM

Oh, damn! I hadn't realized those existed (never did read the whole manual).
Thanks a million, Khris! You saved my night!
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

SMF spam blocked by CleanTalk