Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: TDBFW1 on Tue 17/07/2018 20:11:46

Title: Removing Inventory Items
Post by: TDBFW1 on Tue 17/07/2018 20:11:46
I don't know if I'm missing something, but is there an easy way to remove all inventory items at once, without checking for it first?
Title: Re: Removing Inventory Items
Post by: Cassiebsg on Tue 17/07/2018 20:23:42
yes and no

You need a while loop (or something similar)
something like: check how many inventory items the player has, then subtract 1 (with LoseInventoryItem) until the player has 0.
Title: Re: Removing Inventory Items
Post by: Crimson Wizard on Tue 17/07/2018 20:48:42
Quote from: Cassiebsg on Tue 17/07/2018 20:23:42
You need a while loop (or something similar)
something like: check how many inventory items the player has, then subtract 1 (with LoseInventoryItem) until the player has 0.

It's faster with InventoryQuantity:
Code (ags) Select

// This removes literally all items from player's inventory
for (int i = 1; i < Game.InventoryItemCount + 1; i++) // inventory IDs begin with 1
    player.InventoryQuantity[i] = 0;
// This removes only particular items
player.InventoryQuantity[iMyItem.ID] = 0;
UpdateInventory(); // You must call this to force update on-screen inventory window

EDIT: fixed script.
Title: Re: Removing Inventory Items
Post by: TDBFW1 on Wed 18/07/2018 13:17:51
Thanks. It's funny, I just assumed there would be a simple function to reset the inventory.
Title: Re: Removing Inventory Items
Post by: TDBFW1 on Wed 18/07/2018 14:29:09
This doesn't really work for me, either in room_Load or room_AfterFadeIn. Either way, it brings up the same error message: Character.InventoryQuantity: invalid inventory index 0.
Title: Re: Removing Inventory Items
Post by: Crimson Wizard on Wed 18/07/2018 14:36:05
Right, the inventory IDs begin with 1, so
Code (ags) Select

    for (int i = 1; i < Game.InventoryItemCount + 1; i++)
        player.InventoryQuantity[i] = 0;
Title: Re: Removing Inventory Items
Post by: TDBFW1 on Wed 18/07/2018 14:51:44
Thanks, I accidentally typed in 0, instead of 1
Title: Re: Removing Inventory Items
Post by: Crimson Wizard on Wed 18/07/2018 14:52:56
Quote from: TDBFW1 on Wed 18/07/2018 14:51:44
Thanks, I accidentally typed in 0, instead of 1

I also typed 0 myself at first. :)
Also, it was InventoryItemCount not ItemCount. Don't remember this by heart.