Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Hemuuuli on Sun 01/05/2011 11:15:28

Title: Inventory limit
Post by: Hemuuuli on Sun 01/05/2011 11:15:28
I tried to search how to limit the number of items you can carry but I couldn't find any help. Maybe it just isn't very common newbie question. How do I do it?

Before anyone says that I don't need the limit because it is annoying etc. in my game you play as a librarian working in a library so I don't want player to be able to pick up every book of the library at the same time. :D Also if there is a command to delete every item in the inventory I might need that too.
Title: Re: Inventory limit
Post by: barefoot on Sun 01/05/2011 12:06:04
oops





Title: Re: Inventory limit
Post by: Sephiroth on Sun 01/05/2011 16:05:43
If you are using an inventory window to display the items, you should be able to get the number of inv items you re carrying with .ItemCount property, let's say your window's scriptname is gInvWindow:



function LimitedPickUp(InventoryItem *theItem, int max_items)
{
 if( gInvWindow.ItemCount < max_items )
 {
    player.AddInventory(theItem);
    return true;
 }
 return false;
}


Then you could have :



if( LimitedPickUp(iBook1, 10) == false )
Display("Sorry you cannot carry more items in your inventory.");



Maybe there's a more simple way, or you could even check for how many "books" the player has instead of how many inventory items. But I hope it helps.

Edit about delete all items, there may be another way to do this also but it should work:


  int i = 0;
  while(i < gInvWindow.ItemCount)
  {
    player.LoseInventory(gInvWindow.ItemAtIndex[0]); // replace '0' with 'i' if the inventory window isn't sorted automatically.
    i++;
  }


Title: Re: Inventory limit
Post by: monkey0506 on Mon 02/05/2011 04:10:45
Sephiroth, I'd actually suggest using a global variable instead of a function parameter for max items, so that you don't have to just pass the same value every time.

As far as losing all items, you should bear in mind that the user might have more than one of the same item in their inventory, so this is a more safe method:

int i = 1;
while (i <= Game.InventoryItemCount)
{
  player.InventoryQuantity[i] = 0;
  i++;
}
UpdateInventory(); // update any already shown InvWindows


You could also quite easily throw that into an extender method.
Title: Re: Inventory limit
Post by: Sephiroth on Mon 02/05/2011 04:29:39
Yes you're right about the global instead of a parameter, but the Game.InventoryItemCount, it's supposed to store the total number of created items in the game, not the number the player is holding, but it still works that way.

And yes, I knew there was a better/safer way to do it, I'm sorry I haven't experimented much with the inventory but I can see what you mean with the item quantity.
Title: Re: Inventory limit
Post by: Hemuuuli on Tue 03/05/2011 11:23:30
Thanks! I got all working now the way I wanted.
Title: Re: Inventory limit
Post by: monkey0506 on Tue 03/05/2011 13:30:56
@Sephiroth:

I'm aware that Game.InventoryItemCount does not reflect the number of items that the player currently has in his inventory, but you should also take into consideration that it is possible for other characters than just the player to hold inventory items. Those characters may not have an InvWindow assigned to show their inventory and you may want to clear out their inventory, so using the global max value is the safest generic way to program this. ;)

@Hemuuuli:

Glad you got it working! :)