Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BaJankens on Mon 24/03/2014 20:45:53

Title: How do I use inventory objects?
Post by: BaJankens on Mon 24/03/2014 20:45:53
I must be staring at the screen too long, because for the life of me I can't figure out how to use an inventory object. I have a magic potion and an inventory bar at the bottom of the screen where you can see all your inventory. I want it to be possible to right click on your inventory items and use them that way, but I'm having trouble figuring it out. If anyone knows a simple way of doing this, please help.
Title: Re: How do I use inventory objects?
Post by: Mandle on Tue 25/03/2014 00:33:05
Quote from: BaJankens on Mon 24/03/2014 20:45:53
I must be staring at the screen too long, because for the life of me I can't figure out how to use an inventory object. I have a magic potion and an inventory bar at the bottom of the screen where you can see all your inventory. I want it to be possible to right click on your inventory items and use them that way, but I'm having trouble figuring it out. If anyone knows a simple way of doing this, please help.

From the manual (a very valuable resource!):

Quote
CursorGraphic property (inventory)

int InventoryItem.CursorGraphic

Gets/sets the sprite slot number of the inventory item's mouse cursor. This is the sprite used as the mouse cursor when this inventory item is selected.

NOTE: This property is only used if the "Use selected inventory graphic for cursor" setting in General Settings is turned on.

Example:

Display("The key's cursor graphic is %d", iKey.CursorGraphic);

will display inventory item iKey's cursor graphic.

Compatibility: Supported by AGS 3.1.2 and later versions.

See Also: InventoryItem.Graphic

Manual URL: http://www.adventuregamestudio.co.uk/manual/
Title: Re: How do I use inventory objects?
Post by: Khris on Tue 25/03/2014 04:28:05
That's a common problem, because by default, interacting with an inventory item makes AGS select it as active item.
What you need to do is in General settings, inventory, activate custom click handling for inv items, then add this to on_mouse_click:

Code (ags) Select
  // add after else if (button == eMouseRight) {} block
  else if (button == eMouseLeftInv) {
    inventory[game.inv_activated].RunInteraction(mouse.Mode);
  }
  else if (button == eMouseRightInv) {
    if (player.ActiveInventory != null) player.ActiveInventory = null; // active item? drop it
    else inventory[game.inv_activated].RunInteraction(eModeInteract); // interact with item
  }

(untested).
Title: Re: How do I use inventory objects?
Post by: BaJankens on Tue 25/03/2014 13:36:42
Khris, I tried putting your code in where it says and it's not recognizing "player". So, I switched them to the player's name and then it wouldn't recognize that. My scripting skills are still new, so I'm not sure what to do.
Title: Re: How do I use inventory objects?
Post by: Khris on Tue 25/03/2014 16:13:29
That's weird, because player should be recognized anywhere.

Open GlobalScript.asc, then go down to the function named "on_mouse_click". The default one looks like this:
Code (ags) Select
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) {
    ...
  }
  else if (button == eMouseRight || button == eMouseWheelSouth){
    ...
  }
  else if (button == eMouseMiddle) {
    ...
  }
  else if (button == eMouseWheelNorth) {   
    ...
  }
}

You can put the code I posted after any of the button-blocks, so the two else if-blocks line up with the others, for example in between lines 11 and 12 of the above snippet.
Title: Re: How do I use inventory objects?
Post by: BaJankens on Tue 25/03/2014 17:31:40
It says "parse error at 'player'", so I switched it to the main character's script name (cTinak), and it then says "parse error at 'cTinak'".
Title: Re: How do I use inventory objects?
Post by: BaJankens on Tue 25/03/2014 17:35:13
Okay, I think I found the problem. Where it says

if (player.ActiveInventory != null) player.ActiveInventory == null; // active item? drop it

I changed it to

if (player.ActiveInventory != null) player.ActiveInventory = null; // active item? drop it

Also, I'm still not entirely sure what this code does :/

Title: Re: How do I use inventory objects?
Post by: BaJankens on Tue 25/03/2014 17:37:36
ooooooooooooooooooooookay, nm. I got the potion and right clicked it and it worked :DDDDDDDDDDDDDD. Thank you so much for the help, you have no idea how much this means to me.
Title: Re: How do I use inventory objects?
Post by: Khris on Tue 25/03/2014 17:57:23
Right, sorry, I didn't expect that typo causing that error.
What it does is if you right click oven an inv item, while you're already "carrying" one, it is dropped, otherwise the clicked item is interacted with.
Title: Re: How do I use inventory objects?
Post by: Mandle on Wed 26/03/2014 02:02:53
Quote from: BaJankens on Tue 25/03/2014 17:35:13
if (player.ActiveInventory != null) player.ActiveInventory = null; // active item? drop it

Also, I'm still not entirely sure what this code does :/

It does what Khris said above, and here are the details of why:

if (player.ActiveInventory != null) : The symbol != means "not equal to" and "null" means "nothing" so this part says "If the player currently has an inventory item selected". Note the double negative of "not equal to nothing" becoming "equal to something" in actual meaning.

player.ActiveInventory = null; : In AGS code there is no "then" following the "if" statement but this part is the "then" result of the first test clause. So it means "then the currently selected inventory item becomes nothing."

So:

if (player.ActiveInventory != null) player.ActiveInventory = null;

MEANS:

"If the player currently has an inventory item selected then the currently selected inventory item becomes nothing."

or in even more straightforward language:

"If the player had something selected then now they don't."

Hope this helps you understand a bit more about coding.