If you're interested, I'm working on a "plugin" for Twine/SugarCube 2 to make it easier to work with inventories. It's called the "Universal Inventory System", or "UInv" for short. You can download the current version of UInv from my GitHub section here. To add it to your Twine project, just copy the contents of the "UniversalInventorySystem.js" to the bottom of your JavaScript section, edit in your default items and bags, and then you're ready to use it.
Default versions of your items would go in the "ItemData" function like this:
Items.healingpotion = {
name : "healing potion",
plural : "healing potions",
category : "magic",
cost : 25,
description : "A red potion that tastes like candy and increases your health when ingested."
};
Items.stonyshield = {
name : "stony shield",
plural : "stony shields",
category : "magic",
cost : 100,
description : "An enchantment that creates a wall of stone between you and an enemy."
};
Items.coins = {
name : "coin",
plural : "coins",
cost : 1,
description : "The coin is the realm's standard unit of trade."
};
You can also use the "Item Builder" in the UInv Help File to build items like this (the help file is included in the .zip file you can get from the GitHub site).
Then, you could give the player an inventory like this:
<<set UInv.CreateBag("backpack")>>
<<set UInv.AddItem("backpack", "stonyshield")>>
<<set UInv.AddItem("backpack", "healingpotion", 4)>>
Your inventory includes <<=UInv.DisplayItemList("backpack", "plural", "nothing", ",", "and", "name")>>.
That would create a bag called "backpack", then add 1 "stonyshield" and 4 "healingpotion" to it, and then display the contents of the backpack to the player.
If a player wanted to use the "stonyshield" item, then you could do something like this:
/* use up a "stony shield" enchantment, if you have one */
<<if UInv.BagHasItem("backpack", "stonyshield")>>
<<set UInv.DeleteItem("backpack", "stonyshield", 1)>>
/* add your code here for what effect that would have */
<</if>>
/* if that was the last "stony shield" enchantment then you won't see it in the item list anymore */
Your inventory includes <<=UInv.DisplayItemList("backpack", "plural", "nothing", ",", "and", "name")>>.
And then there are a bunch of other things you could do:
/* create a chest and add a "stony shield" enchantment to its contents */
<<set UInv.CreateBag("chest")>>
<<set UInv.AddItem("chest", "stonyshield")>>
/* move a "healing potion" from the backpack to the chest */
<<set UInv.MoveItem("backpack", "chest", "healingpotion", 1)>>
/* move all of the "stony shields" from the chest to the backpack */
<<set UInv.MoveItem("chest", "backpack", "stonyshield")>>
Your backpack contains <<=UInv.DisplayItemList("backpack", "plural", "nothing", ",", "and", "name")>>.
Your chest contains <<=UInv.DisplayItemList("chest", "plural", "nothing", ",", "and", "name")>>.
/* sell a "healing potion" item to a store for 90% of cost (rounded down) */
<<if UInv.BagHasItem("backpack", "healingpotion")>>
<<set UInv.AddItem("backpack", "coins", Math.trunc( UInv.GetItemPropertyValue("backpack", "healingpotion", "cost") * 0.9) )>>
<<set UInv.DeleteItem("backpack", "healingpotion", 1)>>
<</if>>
Your backpack contains <<=UInv.DisplayItemList("backpack", "plural", "nothing", ",", "and", "name")>>.
UInv isn't quite complete yet, so some things may change, but it's far enough along that you could use it for stuff like the above. See the UInv Help File if you have any questions on the UInv functions (note: the help file is pretty bare bones right now, it will get filled out when it's close to complete).
Currently I'm adding the ability for items to have "pockets". Once I'm done with that I plan to add the following:
- "health bars" (which you can see a preview of in the "UInv_Image_Pre-Load_Test.html" file)
- a "clothing mannequin" for dressing and equipping characters
- an improved drop-down menu (see a preview of it in the help file's "item builder" when adding images)
- a "shop" to make it easy for people to implement buying and selling items.
You could do those on your own currently, but I'd like to make an interface so that it's easy for anyone to create those things.
Hope that helps! :-)