How do I tell if a player has a certain inventory item?

Started by GoToHellDave, Tue 02/10/2012 09:15:54

Previous topic - Next topic

GoToHellDave

Hi Veterans.

I'm aware that there is already a thread on this topic but it is nearly ten years old and it doesn't seem to be relevant anymore.

I'm currently trying to detect if the player has a certain item the inventory and, if he does, turn on a specific dialogue option. Here is the code I am using:

Quoteif(cDave.inv6 != 0)
  {
option-on 7
  {
   else
   {
option-off 7
   }

I get an error telling me that "inv6" isn't a public member of 'character'. What am I doing wrong?

As always, any help is very much appreciated.

Crimson Wizard

There's no such variable as inv6 in character class. You mean "inv" array, in which case you access its elements by index, putting the index in square brackets like
Code: ags

cDave.inv[6];


I do not know which version of AGS you are using, but in the recent version "inv" is considered obsolete. If you are using AGS 3.+, I'd recommend to use one of the following:
Use either Character.HasInventory function (if you do not need to know exact quantity, just the fact of presence)
Code: ags

cDave.HasInventory(iInventoryName);

or Character.InventoryQuantity[] array (the exact substitute for "inv"):
Code: ags

cDave.InventoryQuantity[6];


GoToHellDave

Thanks for the quick reply. Unfortunately I'm still having trouble. I don't know if this is because I'm doing this in the dialogue script or because I've made another schoolboy error. My code now looks like this:

Code: AGS

  if(cDave.HasInventory(iFFPass))
  {
option-on 7
  {
    
  else
  {
option-off 7
  }


and I get the error "parse error at 'else'"

Also, if I remove the 'else' section of the statement I still get the same error.

Can you tell me what I'm doing wrong?


Crimson Wizard

Your second curly bracket is wrong: you use opening one '{', while you should use closing one '}' :)

GoToHellDave

Derp. Another silly mistake.

Thanks for the help.

SMF spam blocked by CleanTalk