Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Monsieur OUXX on Wed 30/01/2019 14:44:15

Title: [SOLVED] how to completely empty inventory?
Post by: Monsieur OUXX on Wed 30/01/2019 14:44:15
It's very simple but I can't find it.
how to completely empty the player's inventory? Google didn't help and F1 didn't help. Or maybe I was using the wrong search terms.

EDIT : found it

Code (ags) Select
int ii=0;
  while(ii<Game.InventoryItemCount){
    cleech.LoseInventory(inventory[ii]);
    ii++;
  }
Title: Re: [SOLVED] how to completely empty inventory?
Post by: Crimson Wizard on Wed 30/01/2019 15:46:59
The above should work, but there are couple of things to optimise:

1) Since 3.4.0 AGS script supports "for" loops.
2) You may have to call LoseInventory multiple times per item if there are more than 1 instance of certain items.
Instead you may directly modify "InventoryQuantity" property. The only nuance is that you should call UpdateInventory afterwards to make sure inventory window is redrawn.

Code (ags) Select

for (int i = 1; i < Game.InventoryItemCount; i++)
    cCharacter.InventoryQuantity[i] = 0;
UpdateInventory();


LATER EDIT: fixed starting index.
Title: Re: [SOLVED] how to completely empty inventory?
Post by: Monsieur OUXX on Wed 30/01/2019 16:33:11
Quote from: Crimson Wizard on Wed 30/01/2019 15:46:59
The above should work, but there are couple of things to optimise:

1) Since 3.4.0 AGS script supports "for" loops.
2) You may have to call LoseInventory multiple times per item if there are more than 1 instance of certain items.
Instead you may directly modify "InventoryQuantity" property. The only nuance is that you should call UpdateInventory afterwards to make sure inventory window is redrawn.

Code (ags) Select

for (int i = 0; i < Game.InventoryItemCount; i++)
    cCharacter.InventoryQuantity[i] = 0;
UpdateInventory();


Noted. Thanks!

EDIT: actually the code that uses .InventoryQuantity crashes if the player doesn't have any of item "i". the engine refuses to set it to 0 because the index is out of bounds. I'll stick to LoseInventory for now.
Title: Re: [SOLVED] how to completely empty inventory?
Post by: Khris on Wed 30/01/2019 18:20:53
Code (ags) Select
  for (int i = 1; i <= Game.InventoryItemCount; i++)