Lose all inventory items (Solved)

Started by Sparky, Mon 05/03/2007 07:48:58

Previous topic - Next topic

Sparky

I found an old thread that covers this:
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=20109.0
It seems it was written for the old LoseInventory function, and I'm unsure of how to rewrite it for Character.LoseInventory. I first tried the following loop:
   int i = 0;
   while (i < Game.InventoryItemCount) {
      player.LoseInventory(i); // error here
      i++;
   }
When I tried to compile AGS said "Type missmatch: cannot convert 'int' to 'InventoryItem*'. The error was in the line with the Character.LoseInventory call. So I tried this:
   InventoryItem *i = 0; // error here
   while (i < Game.InventoryItemCount) {
      player.LoseInventory(i);
      i++;
   }
Now when I compile I get the same error message, but it points to the variable declaration. As in the past I apologize for my scripting inneptitude.

Gilbert

Try this instead:
player.LoseInventory(inventory[ i ])


(keep i as int like the original codes.)

Sparky

That worked perfectly. Many thanks, Gilbot!

(deleted)

Sorry to dig up an old topic, here -- but there's a key correction needed to this method, for further reference.

Two things, actually:

Since "0" is an invalid index number for an item, the i variable should start at 1, instead.

Secondly, (and this is the part that got me) as long as (i < Game.InventoryItemCount), it won't check the highest numbered inventory item. ...probably the newest / last one you created in the editor.

Here's the function I cut together:

Code: ags

function LoseAllInv(this Character*){
int i = 1;
      while (i < (Game.InventoryItemCount + 1)){ // Check all inventory items in the game.
      if (this.InventoryQuantity[i] != 0){   // Character has this one...
      this.LoseInventory(inventory[ i ]);} //Character loses this item.
      i++;} // Check next item in the list.
}


For Example:

Code: ags
player.LoseAllInv(); 

Will cause the player character to lose all inventory items.

Trent R

Shouldn't the code work the same? Even with the i=0? (and Sparky's first batch of code, like Gilbot said)

IIRC, item 0 is null similar to hotspot 0. So basically it would call LoseInventory on nothing, and then continue on because of i++.

Of course, yours is still a better way to write it.

[Edit]: Oh wait, you're also right about the <, but I would use <= instead of +1


~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

SMF spam blocked by CleanTalk