Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: bx83 on Sun 24/03/2019 18:10:30

Title: Combine inventory items: add new inventory item at the place the old one was
Post by: bx83 on Sun 24/03/2019 18:10:30
Is there any way to get the ID of an inventory item?
I want to combine two objects:


cCharacter.Loseinventory(iStuff1);
cCharacter.Loseinventory(iStuff2);
cCharacter.Addinventory(iThing);


However, if I use cCharacter.Addinventory(iThing,0) this creates it at the top of the inventory (not necessarily where original two items were) and cCharacter.Addinventory(iThing) will create it at the bottom (also inconvenient) or some random location.

How can I guarantee that if I add iThing, it will end up where iStuff1/2 were?
Title: Re: Combine inventory items: add new inventory item at the place the old one was
Post by: Crimson Wizard on Sun 24/03/2019 18:19:54
You probably need item's index inside inventory. Since same item may be in two or more character's inventory at the same time, this index is not attached to item itself.
The only method I could find is InvWindow.ItemAtIndex array. But it works in reverse (find item by knowing index).

So you have to search it for item:
Code (ags) Select

int GetItemIndexInInventory(InvWindow* inv, InventoryItem* item)
{
    for (int i = 0; i < inv.ItemCount; i++)
    {
         if (inv.ItemAtIndex[i] == item)
             return i;
    }
    return -1;// not found
}


Then

Code (ags) Select

int index1 = GetItemIndexInInventory(charsInvWindow, iStuff1);
int index2 = GetItemIndexInInventory(charsInvWindow, iStuff2);
... calculate your index for AddInventory
Title: Re: Combine inventory items: add new inventory item at the place the old one was
Post by: bx83 on Mon 25/03/2019 07:26:55
Thank you, works a treat :)