How to go about implementing a semi-complex inventory system?

Hello. New author here to Twine. I’m trying to work out an inventory system.

I want to implement a game with 8 items, but the player can only pick up 5. Some items are bad and some items are good. If the player gets too many bad items in their inventory it leads to a different ending. (like, say, 3/5 items are bad in their inventory).
So…

-How do I go about coding the 5 item limitation?
-How do I code the different percentages of bad and good items in the inventory so that they lead to different endings?
-How do I code it such that the player can check how much of their items are bad? (eg. 3/4 of your items are bad. 1/3 of your items are bad.)

Sorry if this sounds mega complex. I tried simplifying my problem the best I could!

Which Twine format are you using ? It definitely sounds doable (at least in Harlowe, and if it is doable in Harlowe, it probably is doable in Sugarcube as well), but it’s difficult to give you a concrete answer on how to do it without knowing the story format.

In general terms, you need a data structure to act as your inventory (in Harlowe, it could be an array or a dataset, for instance depending on what you need). You need to define it as early as possible, and then, anytime the player wants to pick something, you add it to the Inventory.
These data structures have a property called length, you can check it at any time. For instance, before picking something up, you make sure that the length is less than 5, otherwise, you say that the inventory is full and that they can’t pick anything new.

Now, for the good and bad items, you can deal with that by creating, as early as possible, another data-structure, the same as your inventory, but instead of starting empty, you fill it with the name of either your good or your bad things. Contrary to the inventory, you do not want to change the content of that list throughout your game (unless you want good items to turn bad or vice versa), it will just be used as a comparison.
Whenever you want to check the proportions of good/bad items in your inventory, you can create a third, temporary array/dataset/… (should be the same as your first two), and set it to your inventory - (minus sign) your list of bad(good) things, it will now contain all the things that are in your inventory, but which aren’t bad(good), and you can use the length to know how many such items the player has.

Once you know that information, you can print it to the player, or use it to determine which ending to trigger.

I’m sorry if it is not super clear, I promise it is not that complex, but it is difficult to explain that better without knowing which format you use.

2 Likes

Have you looked at this prebuilt inventory system from Chapel? The guides are very helpful.

Your criteria for your inventory system is very well explained. It shouldn’t be hard to get help to achieve the system you’re wanting.

I hope I’m not assuming too much here, but knowing that you’re new to Twine, one of the unfortunate things that Twine doesn’t really reveal is the story format system. (This was very confusing to me early on.) Think of Twine only in the context of being the editor. It’s a code editor and passage mapping tool. Within the editor, you can choose a story format. Each story format drastically changes the way you code/author your story. Harlowe is the default story format. It’s coding language is unique to itself, meaning switching to another story format is a decision you have to make at the beginning of your project.

The example @dsherwood provided requires that you switch your story’s format to SugarCube.

Personally, with how restricted/limited your inventory system sounds, I would just code it from scratch and use it as a learning exercise to get better with your chosen story format.

If you choose to continue with Harlowe (the default story format), get acquainted with data structures.

Once you let us know which story format you wish to use, we’ll provide more specific guidance.