Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Sun 02/05/2010 21:08:32

Title: Getting Item Names? [SOLVED]
Post by: Atelier on Sun 02/05/2010 21:08:32
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
Title: Re: Getting Item Names?
Post by: monkey0506 on Sun 02/05/2010 21:57:43
int i = 0;
while (i < Game.InventoryItemCount) {
  if (player.InventoryQuantity[i]) {
    lstInventory.AddItem(inventory[i].Name);
  }
  i++;
}
Title: Re: Getting Item Names?
Post by: Atelier on Sun 02/05/2010 22:16:45
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?
Title: Re: Getting Item Names?
Post by: discordance on Sun 02/05/2010 22:28:33
. . . that makes it sound like the inventory array starts at 1. Anyway, you'll want to change the condition to:


if (i <= Game.InventoryItemCount) {


otherwise it'll drop out at inventory count - 1, and miss the last one.
Title: Re: Getting Item Names?
Post by: Atelier on Sun 02/05/2010 22:33:28
Now only the first item is displayed. But no worries, I'll just add a dummy item at the end. Thanks both! :)
Title: Re: Getting Item Names? [SOLVED]
Post by: monkey0506 on Mon 03/05/2010 01:51:30
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:

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.
Title: Re: Getting Item Names? [SOLVED]
Post by: Atelier on Mon 03/05/2010 10:20:18
Hm, I missed out the = after <. Doh. Thanks again, everything resolved.