Selecting an Inventory Item from a Custom GUI

Started by Tenacious Stu, Sat 20/11/2010 02:33:28

Previous topic - Next topic

Tenacious Stu

Hi there,

I have tried searching for an answer to this as I thought others will have had the same problem, but whatever I found, be it in the manual, forum or wiki I always came way confused and so I hope people don't mind in my asking it here.

I have created a GUI that appears on screen all the time during gameplay. I am using a two click interface where left click is 'interact with' and right click is 'look at'. When the player right clicks it does indeed look at the object, but left click won't select the object, even though I've read a few times that 'interact with' is reserved for 'select' with regards to inventory. I have a feeling that somehow the 'Handle inventory clicks in script' setting needs to be changed, but I don't know where to go from there, or should I have left click changed to 'talk to' so that 'interact with' isn't being reserved and I can select the inventory item another way. I'm just not sure what I should do?

Thank you so much in advance for any help as I'm pretty stuck with this one...  :-\

Cheers

monkey0506

#1
I've never personally used the built-in handling of inventory, but if you have "Handle inventory clicks in script" set to True then you could modify your on_mouse_click function (in the GlobalScript.asc) to something like:

Code: ags
function on_mouse_click(MouseButton button)
{
  CursorMode mode = eModeInteract;
  if ((button == eMouseRight) || (button == eMouseRightInv)) mode = eModeLookat;
  else if (player.ActiveInventory != null) mode = eModeUseinv;
  if ((button == eMouseLeftInv) || (button == eMouseRightInv))
  {
    InventoryItem *iat = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
    if (iat.IsInteractionAvailable(mode)) iat.RunInteraction(mode);
    else if ((button == eMouseLeftInv) && (player.ActiveInventory == null)) player.ActiveInventory = iat;
    else if ((button == eMouseRightInv) && (player.ActiveInventory != null)) player.ActiveInventory = null;
  }
  else if (IsInteractionAvailable(mouse.x, mouse.y, mode)) ProcessClick(mouse.x, mouse.y, mode);
  else if ((button == eMouseLeft) && (player.ActiveInventory == null))
  {
    ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if ((button == eMouseRight) && (player.ActiveInventory != null))
  {
    player.ActiveInventory = null;
  }
}

Tenacious Stu

Thanks Monkey,

I changed "Handle inventory clicks in script" to TRUE and my game is working fine still, but When I left click the inventory item, it still doesn't select it? Is there another piece of script I need for this?

Thanks

Khris

Since this is one of the most frequently asked questions of all time, here's my answer from another thread a few days ago:

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=42202.msg559481#msg559481

In short you determine the inventory item under the mouse and set it as player.ActiveInventory.

monkey0506

Sorry, I forgot to include the ActiveInventory bits. I've modified the code above.

Tenacious Stu

Thanks again Monkey, however now its coming up with errors when I try to run it saying Undefined Symbol 'eModeUseInv'

is there something else I need to add to get this working?

@Khris,

I've bee trying to use the help found in the link you gave, but I'm either getting errors or it just isn't working, I must be putting lines of code in the wrong places or something.

Upon trying to do this, I have realised that scripting is not my strong suit... like, at all!  :-\

monkey0506

Sorry, that was a typo on my part. It should be eModeUseinv not eModeUseInv (AGS is case-sensitive).

If you've changed the name of that cursor mode then it should also be changed appropriately.

Tenacious Stu

HURRAY! It works now,

Ta very much Monkey, your a star n'half  ;D

monkey0506

One thing to note is that this doesn't actually support unhandled interactions at all..simply because I don't like the built-in unhandled_event. I myself usually write a custom function to take CursorMode and LocationType parameters (as well as an InventoryItem* to get around the eLocationNothing issue) and then call where I need it.

However, glad it's working for you! := Let me know if you need help getting it working with otherwise unhandled interactions (whether using the built-in unhandled_event or otherwise).

SMF spam blocked by CleanTalk