Multiple Copies of Items

What would be the easiest way to give a player multiple copies of the same item? For instance if I want them carrying 5 Healing Potions. I would also want it to show inventory as "Healing potion (x2)" or something similar. Not have it show twice in inventory.


I recently did this same thing for a game I’m working on. Instead of making a huge list of the same items, what I did is make one item and then use it as a sort of container to hold a quantity.

First, create your object (in this case, Healing Potion). This will be what your player sees and interacts with in order to use the item. Go to the attributes tab for this item and create a “quantity” attribute (set it to however many potions you want the player to have the first time they acquire a potion). For instance, if the player finds a random healing potion lying on the ground, set this to 1. If you give the player a couple at the beginning of the game, set it to 3 or 5 or whatever you want them to start out with.

Now, go to your inventory tab and under the section for “After Taking Object,” add this code:

Healing Potion.listalias = "Healing Potion: " + Healing Potion.quantity

This will allow it to display in your inventory with the amount. Next, you’ll want to make sure you can Use/Give it, and then move on over to the Use/Give tab. Under “Use (on its own),” add the following code. You could also set a verb, say “drink” and then add this for that, too.

  Healing Potion.quantity = Healing Potion.quantity - 1
  msg ("You decide to use a healing potion, restoring 15% of your health. There are " +Healing Potion.quantity+ " left.")
  Healing Potion.listalias = "Healing Potion: " + Healing Potion.quantity
  IncreaseHealth (15)
}
else if (Healing Potion.quantity = 0) {
  msg ("There are none left!")
}
else if ((Healing Potion.quantity > 0) and (player.health = 100)) {
  msg ("You're perfectly healthy right now, so you decide against it. ")
}
else if (((Healing Potion.quantity > 0)) and ((player.health > 85) and (player.health <100))) {
  Healing Potion.quantity = Healing Potion.quantity - 1
  msg ("You decide to use a healing potion, restoring 15% of your health. There are " +Healing Potion.quantity+ " left.")
  Healing Potion.listalias = "Healing Potion: " + Healing Potion.quantity
  set (player, "health", 100)
}```

This section of code can get a little confusing, but basically it sets it up so that the healing potion will restore 15% of the player’s health, making sure that it does not allow the player’s max health to go over 100%. If you have at least one healing potion, it will subtract one from the inventory and update the display, and if you are out of healing potions, it will display a special message. If the player is at full health, it will not allow them to use a healing potion. 

Finally, if you’d like to allow the player to see how many healing potions are grouped together before they actually take it, you can add this to the object “look at” description:

    msg ("A regular healing potion. There are currently " +Healing Potion.quantity+ " here. Using one will restore 15% of your health.")

In the game, whenever the player picks up a new healing potion, instead of actually adding a new object to their inventory, you simply add +1 to the Healing Potion.quantity attribute. Hopefully this helps you out. 

Accidentally forgot to hit enter on the second section of code, it should end where the three backticks (```) are. It won't allow me to edit it for some reason...


There is a library here that might help:
https://github.com/ThePix/quest/wiki/StackLib


Thank you!


generally, the things that need to be done (as seen within the stack library):

  1. find/get/create a container Object of a type of quantity of Object (and if the quantity is zero, destroy/remove the container object)
  2. find/count all the Objects of that type in your inventory and store it as an Integer Attribute on the container Object (and also lower that Integer Attribute as you use that type of object, if quantity is zero, destroy/remove the container object)
  3. handling displaying it, for example: potion (99)
  4. destroy/remove all of the objects once you got the container object and its integer attribute set up
  5. handle all other features/actions that you may want (for example, if you want to put/move/sell/etc-actions an object from that quantity... you'll need to create/move one/more of those quantity Objects, as you can't do so with your container object, viewing, storing, etc actions/features)

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums