Inventory limit

Started by Hemuuuli, Sun 01/05/2011 11:15:28

Previous topic - Next topic

Hemuuuli

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.

barefoot

#1
oops





I May Not Be Perfect but I Have A Big Heart ..

Sephiroth

#2
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:

Code: ags


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


Then you could have :

Code: ags


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:

Code: ags

   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++;
   }



monkey0506

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:

Code: ags
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.

Sephiroth

#4
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.

Hemuuuli

Thanks! I got all working now the way I wanted.

monkey0506

@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! :)

SMF spam blocked by CleanTalk