Invalid inventory index 0?

Started by Joseph, Tue 01/02/2011 14:28:49

Previous topic - Next topic

Joseph


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
Spread the love...One.Turtle.At.A.Time.
http://www.youtube.com/watch?v=y0A77rohcyg

Khris

Pretty much everything that's numbered starts out at 0, except for some reason inventory items.

Try this:

Code: ags
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();
}

Joseph

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  :-* :-* ;)

Spread the love...One.Turtle.At.A.Time.
http://www.youtube.com/watch?v=y0A77rohcyg

Knox

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?

Code: ags

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?
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

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.

Joseph

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 :=
Spread the love...One.Turtle.At.A.Time.
http://www.youtube.com/watch?v=y0A77rohcyg

Khris

Try this:

Code: ags
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();
}

Calin Leafshade

Is the UpdateInventory() call still needed in 3.2? I dont think I've ever used that function.

Knox

#8
Ok I found "loseAllInv" somewhere in these threads, by adding it to what I wrote below, should be ok now?

Code: ags

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...
--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

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. :=

SMF spam blocked by CleanTalk