checking if mouse is hovering the inventory

Started by Blano, Tue 03/09/2024 22:09:31

Previous topic - Next topic

Blano

Please, as i wrote on title i need this check, because my script doesnot seem to respond to any iteraction on inventory object..
Im using a black project and a custom pop up inventory
. thank you

Dam

Khris

To use built-in inv handling you need to switch the mouse.Mode to eModeInteract when the player opens the inventory. Now you should be able to select an inv item by clicking it.
Also make sure you set the invWindow's ItemWidth and ItemHeight to large enough values, otherwise just the top left corner of the item will react to a click.

To manually process clicks on inventory items you need to

1) go to General Settings -> Inventory and set "Override built-in inventory click handling" to "True"
2) add your own eMouseLeftInv and eMouseRightInv blocks to the global on_mouse_click function

(Also note that the hover check you're asking for in the title is not needed to solve this; you should always ask about the original problem, instead of about your attempt to solve it)

Blano

Ok i got the point, ill try what you suggest! Thank you!

Blano

#3
nothing at all, i looked all the forum but nothing seems to respond... i share the code

Code: ags


// called when a mouse button is clicked
function on_mouse_click(MouseButton button)
{
  if (IsGamePaused())
  {
    // game is paused, so do nothing (i.e. don't process mouse clicks)
  }
  else if (button == eMouseLeft)
  {
     if (mouse.Mode==eMouseLeftInv) {
  
  inventory[game.inv_activated].RunInteraction(mouse.Mode);
  
  
  
  
  }
    
    
    
    // left-click, so try using the current mouse cursor mode at this position
    Room.ProcessClick(mouse.x, mouse.y, mouse.Mode);
  }
  else if (button == eMouseRight)
  {
    // right-click, so cycle the mouse cursor mode
    mouse.SelectNextMode();
  }
}






edit:

the problem is it seems do not recognize the inv gui

Snarky

#4
First of all, there is no code in your sample to handle clicking in the inventory GUI. Do you have "Override built-in inventory window click handling" set in the project settings?

If you do need to add code to make things happen when you click, there is another point: For historical reasons, on_mouse_click reports different values when it is clicked over inventory items, so instead of checking if (button == eMouseLeft) you have you use if (button == eMouseLeftInv).

Edit: Basically, you haven't done what Khris suggested earlier in the thread:

Quote from: Khris on Tue 03/09/2024 23:15:552) add your own eMouseLeftInv and eMouseRightInv blocks to the global on_mouse_click function

Blano

i marked as true "Override built-in inventory window click handling" and i'm trying the run the code you suggested but
"if (button==eMouseLeftInv)" does nothing in my 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) {


Room.ProcessClick(mouse.x, mouse.y, mouse.Mode);
if (button==eMouseLeftInv){
     // inventory[game.inv_activated].RunInteraction(mouse.Mode);

  cXeno.Say("qualcosa triggera");
  
  }}




it's not my intent to not follow your suggestions.. just trying to interpretate in code

Khris

You have added the block inside the eMouseLeft block but eMouseLeft and eMouseLeftInv are two different values so button cannot be equal to both of them.

Code: ags
function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT or LEFTINV or RIGHTINV
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft) {
    Room.ProcessClick(mouse.x, mouse.y, mouse.Mode);
  }
  else if (button==eMouseLeftInv) {
    // inventory[game.inv_activated].RunInteraction(mouse.Mode);
    cXeno.Say("qualcosa triggera");
  }
}

Note how the proper indentation greatly helps with reading the code.

Snarky

#7
Khris responded first, but it might still be useful to explain indentation. (That means when lines don't start out at the left edge, but are moved in by some number of spaces.)

Indentation is used in programming to help show the structure of the program. Basically, the rule is that whenever you have a bunch of lines (often called a "block") that are grouped together (or even if it's just a single line) under some kind of "header," you move them one tab to the right, and when the group of lines is over (when you get to the next "}"), you move back left. A "header" is anything that is followed by {} brackets, like a function header, an if condition, a for loop, etc. So by indenting the lines that belong under that header, it shows that those are "inside" the header (they're the "body" under the header). It's a lot like when you make bulleted lists, and have some bullets indented by one level: it means that those bullets belong together under the bullet above.

So, your code should be formatted like this:

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) {
    Room.ProcessClick(mouse.x, mouse.y, mouse.Mode);
    if (button==eMouseLeftInv) {
       // inventory[game.inv_activated].RunInteraction(mouse.Mode);
      cXeno.Say("qualcosa triggera");
    }
  }
}

The first line starts out at the left edge because it isn't "under" or "inside" anything else. Then everything that is inside that function (the function body), between the {} brackets, is indented one tab (which is set to two spaces by default in AGS). Line 4 is inside an if-statement, so it's indented another level, but as soon as we leave that block, we go back to the normal indentation level for the function. Next, we have an else if-statement on line 6, which acts as a header for the block that follows (lines 7-11), which are therefore indented another level again. Inside of this block there's another if-statement with its own body, which is indented yet another level.

As Khris points out, what becomes clear when the code is formatted like this is that line 8,  "if (button==eMouseLeftInv)", is inside the condition (part of the body under the condition) on line 6, "else if (button == eMouseLeft)". So it will only reach this line if button == eMouseLeft, and then you're checking if button is a different value, eMouseLeftInv, which will necessarily be false.

The other part is that you need a line inside the "if (button==eMouseLeftInv)" condition to actually do what you want, namely to set the selected inventory item to be the active item:

Code: ags
    player.ActiveInventory = inventory[game.inv_activated];

Blano

ok all is goinng as i wish but how to drop back objects with the same button?

Khris

If button is eMouseLeftInv or eMouseRightInv, you can do

Code: ags
  InventoryItem* clickedItem = inventory[game.inv_activated];
https://adventuregamestudio.github.io/ags-manual/GlobalArrays.html
https://adventuregamestudio.github.io/ags-manual/Gamevariables.html

To allow using inv item A on inv item B, check if player.ActiveInventory is != null and what the mouse.Mode is.

To set the clicked item as active:
Code: ags
  player.ActiveInventory = clickedItem;
https://adventuregamestudio.github.io/ags-manual/Character.html#characteractiveinventory

And so on. At this point you're just asking us to write all the code for you, but we still don't know what interface exactly you're trying to build. You should check out existing templates and their code, look up the used commands and try to understand how it all fits together.

Blano

I apologize if I was insistent! Even though I've known Ags for a long time, I've never really used it, I've just played around with it in my free time, but now I think it's time to study its potential carefully.

Thank you again

Khris

A good point to start is the TwoClickHandler script from the BASS template.

SMF spam blocked by CleanTalk