[Solved] Close inventory on item select (Single click cursor)

Started by Stapper, Mon 23/05/2016 05:05:48

Previous topic - Next topic

Stapper

I've been using the single click cursor script from hedgefield (link) which works very nicely.
I would like to add the feature of closing the inventory automatically upon selecting an item. I know I have to use "gInventory.Visible = false;" but I have no idea where to place it. I've tried certain lines but none of them worked. Is it possible to do this within this script?

Here is the script for reference:
Code: ags
//MOUSE CLICKS
function on_mouse_click(MouseButton button) {
  //Walk/Talk/Interact:
  if ((button == eMouseLeft && SC_SwapLeftRight == false) || (button == eMouseRight && SC_SwapLeftRight == true)) {
    if ((GetLocationType(mouse.x, mouse.y) == eLocationHotspot) || (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {
      if (mouse.Mode == eModeUseinv) ProcessClick(mouse.x,mouse.y, mouse.Mode);
      else ProcessClick(mouse.x, mouse.y, eModeInteract);
    }
    else if (GetLocationType(mouse.x, mouse.y) == eLocationCharacter) {
      if (mouse.Mode == eModeUseinv) ProcessClick(mouse.x, mouse.y, mouse.Mode);
      else ProcessClick(mouse.x, mouse.y, eModeTalkto);
    }
    else ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  //LookAt:
  else if ((button == eMouseRight && SC_SwapLeftRight == false) || (button == eMouseLeft && SC_SwapLeftRight == true)) {
    if (mouse.Mode == eModeUseinv) {
      player.ActiveInventory = null;
      mouse.Mode = eModePointer;
    }
    else if ((GetLocationType(mouse.x, mouse.y) == eLocationHotspot) || (GetLocationType(mouse.x, mouse.y) == eLocationCharacter) || (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {
      ProcessClick(mouse.x, mouse.y, eModeLookat);
    }
  }
  //Inventory select/combine:
  if (SC_CustomInvClicks == true) {
    if ((button == eMouseLeftInv && SC_SwapLeftRight == false) || (button == eMouseRightInv && SC_SwapLeftRight == true)) {
      if (inventory[game.inv_activated].IsInteractionAvailable(eModeUseinv) == 1 && mouse.Mode == eModeUseinv) {
        inventory[game.inv_activated].RunInteraction(eModeUseinv);
      }
      else if (inventory[game.inv_activated].IsInteractionAvailable(eModeInteract) == 1) inventory[game.inv_activated].RunInteraction(eModeInteract);
      else player.ActiveInventory = inventory[game.inv_activated];
    }
  }
}


//INVENTORY CLICKS
function on_event(EventType event, int data) {
  GUI *theGUI = gInventory1.OwningGUI;
  if (SC_CustomInvClicks == true) {
    //deselect current inventory item:
    if (event == eEventGUIMouseDown && player.ActiveInventory != null && data == theGUI.ID) {
      if ((mouse.IsButtonDown(eMouseRight) && SC_SwapLeftRight == false) || (mouse.IsButtonDown(eMouseLeft) && SC_SwapLeftRight == true)) {
        GUIControl *theControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
        if (theControl==gInventory1) {
          player.ActiveInventory = null;
        }
      }
    }
    //look at inventory item:
    else if (event == eEventGUIMouseDown && player.ActiveInventory == null && data == theGUI.ID) {
      if ((mouse.IsButtonDown(eMouseRight) && SC_SwapLeftRight == false) || (mouse.IsButtonDown(eMouseLeft) && SC_SwapLeftRight == true)) {
        GUIControl *theControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
        if (theControl==gInventory1) {
          InventoryItem *i = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
          if (i != null) i.RunInteraction(eModeLookat);
        }
      }
    }
  }
}

Matti

I can't test this right now, but line 32 is where the new active inventory item is set. Try this:

Code: ags

else 
{
  player.ActiveInventory = inventory[game.inv_activated];
  gInventory.Visible = false;
}

Stapper

Thanks for your reply! I've tested it out but unfortunately it doesn't change anything...

Matti

What's the visibility setting of your inventory GUI?

Stapper

It was set to "Pause game when shown", I've just tested it with "Normal, initially off" as well but without change.

Matti

It seems the inventory is called gInventory1, not gInventory. Could that be the problem?

Stapper

The inventory window is called gInventory1 which is placed on the GUI called gInventory.

It closes just fine when I click the button for it but somehow won't close when selecting an item ???

Mandle

Is it maybe because the script to open the inventory window when the mouse hovers over it immediately runs again, thus reopening the GUI before the next game loop even happens?

If this is the case then you just have to correct/recode the GUI's popup function to not happen if the player's cursor has not moved below the popup zone after changing to the item graphic. (A simple bool to keep track of this should be cool)

This means the GUI should vanish when the player selects an item, and will only reappear if the cursor is moved down the screen and then back up to the top again.

Stapper

No, I don't have a fancy hover animation or anything like that, didn't look into how that works yet. I just have the inventory opening with the press of a button that shows on the screen:

Code: ags
function btnOpenInventory_OnClick(GUIControl *control, MouseButton button)
{
  show_inventory_window();
}

And then a button on the inventory GUI that closes it again:
Code: ags
function bCloseInventory_OnClick(GUIControl *control, MouseButton button)
{
	gInventory.Visible = false;
  mouse.UseDefaultGraphic();
  if (player.ActiveInventory==null)
    mouse.Mode = eModePointer;
}


When I click on the item, the cursor changes to reflect this properly, but the inventory GUI remains open.

Khris

The first order of debugging: add a Display command:
Code: ags
else 
{
  player.ActiveInventory = inventory[game.inv_activated];
  Display("Hiding inventory now.");
  gInventory.Visible = false;
}

What happens when you try this?

dayowlron

this may be a dumb question but your call to btnOpenInventory_OnClick calls a function called show_inventory_window.
Have you verified that "show_inventory_window" displays gInventory?
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Stapper

Alright it works now, thanks everyone for all your insights!
Khris' comment made me realize that the section didn't even trigger and I saw I didn't properly place a step in the setup of the script. Truly a beginner mistake :(

Step 3 was clearly written as: "Use the bool SC_SwapLeftRight and the function CustomInv(true or false) in your gamestart function to modify the controls." but I didn't write it properly there.
Just adding this fixed it:
Code: ags
function game_start() {   
  CustomInv(true);


Once again a huge thanks all! ;-D

SMF spam blocked by CleanTalk