Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Joseph on Tue 01/02/2011 14:28:49

Title: Invalid inventory index 0?
Post by: Joseph on Tue 01/02/2011 14:28:49

after monkey_05_06 answered my solva-ree-kneed question in http://www.adventuregamestudio.co.uk/yabb/index.php?topic=42750.0
i looked round for yet another question and found moneky_05_06 wrote a code i was looking for in http://www.adventuregamestudio.co.uk/yabb/index.php?topic=38897.0


but when i try code, i get character.inventoryquantity: inmvalid inventory index 0??????? :o >:( ???

what did i do wrong my character i want to swap with has none items and the one swapping has eight items, wa-da-fks dudes
Title: Re: Invalid inventory index 0?
Post by: Khris on Tue 01/02/2011 14:49:39
Pretty much everything that's numbered starts out at 0, except for some reason inventory items.

Try this:

void SwapInventory(this Character*, Character *otherCharacter) {
  if (otherCharacter == null) return;
  int i = 1;
  while (i <= Game.InventoryItemCount) {
    int ii = this.InventoryQuantity[i];
    this.InventoryQuantity[i] = otherCharacter.InventoryQuantity[i];
    otherCharacter.InventoryQuantity[i] = ii;
    i++;
  }
  UpdateInventory();
}
Title: Re: Invalid inventory index 0?
Post by: Joseph on Tue 01/02/2011 15:01:18
wowww krhis no longer error gets shown but my first item in list is now the last item after i use swapping code, how can i get so the order is always kept perfecto as it was for 1st guy inventory (well, a turtle that is not guy lol!!!1)

like this in a way to exlpain =

1st item of first guy goes to 1st slot of second guy inventory, 2nd item in first guys list goes exactly perfect to 2nd slot of 2nd guy's inventroy

again answers keep me front killing everyone here!! ! joke lol  :-* :-* ;)

Title: Re: Invalid inventory index 0?
Post by: Knox on Tue 01/02/2011 22:21:55
Ive been using Monkey's script for a while, but I was wondering if this is "as good"? What do you think guys? Can this help joseph?


void SwapInventory(this Character*, Character *otherCharacter)
{
  if (otherCharacter == null) return;
  int iItemCount = invInventory.ItemCount;
  int i = 0;
  while (i < iItemCount)
  {
    InventoryItem* iItemName = invInventory.ItemAtIndex[i];
    if (!otherCharacter.HasInventory(iItemName)) otherCharacter.AddInventory(iItemName);
    i++;
  }
}


I tested it out on my game and it seems to work...is this valid too?
Title: Re: Invalid inventory index 0?
Post by: Khris on Wed 02/02/2011 00:30:06
That code doesn't swap inventories, it adds everything to the other character's inventory that the main character has.
So if A's inventory has 5 items and B's none:
A.SwapInventory(B);
Now B has those 5 items, too, while A has kept them.
If B now loses 2 of them and the game "swaps" back:
B.SwapInventory(A);
A is now back at 5, or rather, didn't lose anything in the first place.

While this might be what the designer wants to do, SwapInventory to me is a method I'd use to change the player character but keep the inventory. That's what monkey's code does.
Title: Re: Invalid inventory index 0?
Post by: Joseph on Wed 02/02/2011 15:37:41
if i do the first code, still the order doesnt match, the item that is supposed to be first gets swapped last so its the last. i tried your code general but i think i go with the first one if someone can help me get it toswap the order OK!

plllleaase  :D ;D :o :=
Title: Re: Invalid inventory index 0?
Post by: Khris on Wed 02/02/2011 16:32:41
Try this:

void SwapInventory(this Character*, Character*otherCharacter) {
  if (otherCharacter == null) return;

  InvWindow*iw = invCustomInv;     // change this to script name of inv window GUI object

  InventoryItem*ii;
  int i = 0, j;
  int ic;
  while (i < iw.ItemCount) {
    ii = iw.ItemAtIndex[i];
    otherCharacter.InventoryQuantity[ii.ID] = 0;
    ic = this.InventoryQuantity[ii.ID];
    this.InventoryQuantity[ii.ID] = 0;
    j = 0;
    while (j < ic) {
      otherCharacter.AddInventory(ii);
      j++;
    }
    i++;
  }
  UpdateInventory();
}
Title: Re: Invalid inventory index 0?
Post by: Calin Leafshade on Wed 02/02/2011 20:49:50
Is the UpdateInventory() call still needed in 3.2? I dont think I've ever used that function.
Title: Re: Invalid inventory index 0?
Post by: Knox on Wed 02/02/2011 22:20:37
Ok I found "loseAllInv" somewhere in these threads, by adding it to what I wrote below, should be ok now?


void LoseAllInv(this Character*)
{
 int i = 1;
 while (i <= Game.InventoryItemCount)
 {
    if (this.InventoryQuantity[i] != 0) this.LoseInventory(inventory[i]);
    i++;
 }
}

void SwapInventory(this Character*, Character* otherCharacter)
{
 //*need to clear player's inventory after swap, and before swap, clear the target char inventory
 if (otherCharacter == null) return;
 int iItemCount = invInventory.ItemCount;
 int i = 0;
 
 otherCharacter.LoseAllInv();
 while (i < iItemCount)
 {
   InventoryItem* iItemName = invInventory.ItemAtIndex[i];
   if (!otherCharacter.HasInventory(iItemName)) otherCharacter.AddInventory(iItemName);
   i++;
 }
 this.LoseAllInv();
}


I personally don't use UpdateInventory() either...
Title: Re: Invalid inventory index 0?
Post by: monkey0506 on Thu 03/02/2011 06:48:17
The UpdateInventory function does what it specifies that it does in the manual. You don't need it if you're using AddInventory and LoseInventory, you only need it if you are directly altering the values in the InventoryQuantity array, and then only if an InvWindow is being shown at the same time.

And Knox, that LoseAllInv function will fail if the Character in question has more than one of any item. Instead, to lose the entire inventory, you should directly set the InventoryQuantity to 0. After you do that it's safest to call UpdateInventory just in case there is an InvWindow showing at the same time.

Also, for systems that do show multiple inventory items multiple times, you would be better off leaving the check for whether or not the player already has the item out. AFAIK if show multiple items is off then adding the same item at a different index will be ignored, and only the item at the first occurring index will be displayed. I could be wrong, so this might warrant some testing, but I think just leaving that condition out altogether would be the most generic route.

Oh, and there's no guarantee in that function that invInventory is using this Character, so that seems likely to cause issues, but it wouldn't be difficult to change the CharacterToUse property (and potentially switch it back at the end of the function if needed).

Sorry about that other code snippet referencing the items from 0 instead of 1 though. :=