Combine inventory items: add new inventory item at the place the old one was

Started by bx83, Sun 24/03/2019 18:10:30

Previous topic - Next topic

bx83

Is there any way to get the ID of an inventory item?
I want to combine two objects:

Code: ags

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?

Crimson Wizard

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

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

int index1 = GetItemIndexInInventory(charsInvWindow, iStuff1);
int index2 = GetItemIndexInInventory(charsInvWindow, iStuff2);
... calculate your index for AddInventory


SMF spam blocked by CleanTalk