basic HasInventory scripting problems

Started by Ahdur, Wed 08/07/2009 15:41:11

Previous topic - Next topic

Ahdur

Hi, I am simply trying to check if my character has a certain item in his inventory and, if so, continue with the game.
However, every time i try following the format in the manual : "bool Character.HasInventory(InventoryItem *item)" I am told that the variable of the character's name has already been imported or is already defined.
Please can somebody show me how to declare this boolean? Specifically I need to know what format I need to use to type in the name and inventory item assuming the character's name is cBob and the inventory item is iGold

Also, assuming I can get help with this, would it then be correct to enter:
if ((cBob.HasInventory(iGold)) == false)
 {
   [game code here]
 }

I have read and re-read the manual and scoured the forums but I cannot find any help.  This is all new to me so please have patience! I'm trying to teach myself basic coding principles at the same time as making the game but i'm sure the answer is simple because I cannot find the same question anywhere!

Anyway, thanks in advance!

GuyAwesome

#1
As a rule of thumb: If it has an entry in the manual, it already exists - no need for you to declare it. (Now bring on some older AGSers to give a list of exceptions to that :P)  Look at the sample usage in the manual
Code: ags

if (player.HasInventory(iKey))
{
  Display("The player has the key!!");
}


That's all you need to do, not the initial declaration.

The bracketed part of the condition - (player.HasInventory(iKey)) - is really shorthand for (player.HasInventory(iKey) == true). So, to make your example work you'd just need to add the '== false', like so
Code: ags

if (cBob.HasInventory(iGold) == false)
  {
    [game code here]
  }


Note the number of brackets in the conditional line. The extra brackets you've got around cBob.HasInventory won't cause any problems in this case - what you've got works out to if ((cBob.HasInventory(iGold) == true) == false) which is just a bit redundant - but may cause logic errors in more advanced conditions.

monkey0506

Another shorthand you can use is to add the boolean NOT operator !

Just as Guy said (player.HasInventory(iKey)) is shorthand for (player.HasInventory(iKey) == true) we can invert it by throwing in the boolean NOT: (!player.HasInventory(iKey)) is shorthand for (player.HasInventory(iKey) == false).

If you're not familiar with or are uncomfortable with this particular method of shorthand then feel free not to use it in your own scripts, but understand that this is how the code will be compiled. So checking that you parenthesis line up as intended is always very important. As a general rule of thumb, unless there's a mathematical operator (+, -, *, /) inside the parenthesis it's most likely getting interpreted as a boolean expression (function parameter lists excluded of course).

Welcome to the forums by the way Ahdur, and just so you know you can enclose your code like Guy did by adding these tags around your code:

[code] insert code here [/code]

Which would give us:

Code: ags
 [i]insert code here[/i] 


;)

SMF spam blocked by CleanTalk