Getting Item Names? [SOLVED]

Started by Atelier, Sun 02/05/2010 21:08:32

Previous topic - Next topic

Atelier

How could I obtain the name of every item the player has (in the inventory), and then add the names individually to a list box? I played around with custom properties but didn't seem to get anywhere.

Cheers,
Atelier

monkey0506

Code: ags
int i = 0;
while (i < Game.InventoryItemCount) {
  if (player.InventoryQuantity[i]) {
    lstInventory.AddItem(inventory[i].Name);
  }
  i++;
}

Atelier

#2
Thanks, just tried your code and this is the message I get:   Character.InventoryQuantity: invalid inventory index 0.   Everything works fine until I input the command in-game. Is it me?

It seems if I set i = 1 the error message doesnt come up, and when I try it now everything appears; apart from the very last item in the inventory node, even when player starts with item. Why is this?

discordance

. . . that makes it sound like the inventory array starts at 1. Anyway, you'll want to change the condition to:

Code: ags

if (i <= Game.InventoryItemCount) {


otherwise it'll drop out at inventory count - 1, and miss the last one.

Atelier

Now only the first item is displayed. But no worries, I'll just add a dummy item at the end. Thanks both! :)

monkey0506

Yeah I always forget that the inventory array is 1-indexed whereas the other arrays like character, gui, hotspot, and object are all 0-indexed (like normal arrays). :=

So the proper code should be something like:

Code: ags
int i = 1;
while (i <= Game.InventoryItemCount) {
  if (player.InventoryQuantity[i]) {
    lstInventory.AddItem(inventory[i].Name);
  }
  i++;
}


Your last post is worrying me though..what do you mean by "Now only the first item is displayed"? It should still be iterating every inventory item, but it's only adding the ones the player has in their inventory.

Atelier

Hm, I missed out the = after <. Doh. Thanks again, everything resolved.

SMF spam blocked by CleanTalk