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.

I’m not sure which format I’m using. I’m using the default setting, I just opened Twine and started writing. I’m still quite new to this software. (can we assume harlowe? I haven’t started much into my story and can always switch to harlowe anyway if it turns out to not be the format I am using.)

I’ve seen videos outlining how to write an inventory system in Twine. Making an inventory page in Twine by Austin Lim is what I’m going off of.

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.

This can be done with an if else statement, am I right?

How would I go about creating the data-structure for the good and bad items? Especially the third temporary array/dataset.

I am someone who has limited knowledge in HTML and coding in general. So it would be great if you could explain it in a way that is understandable for a newbie like me. :slight_smile: Sorry.

Here’s how I implemented the inventory system for Lucifugus:

In the initialization code, I defined a datamap with all the objects, using the id property as the key.

(set: $objects to 
(dm: 
"fosforo", (dm:"id","fosforo","name", "fósforo","loc","None"),
...
))\

The loc property tracks where the object is, using a location passage name ("Inventory" meaning an object is carried).

How an object is taken (_item is the datamap value):

(set: _key to _item's id)\
(set: $objects's _key's loc to "Inventory")\

and dropped (again, _item is the datamap value):

(set: _room to (passage:)'s name)\
(set: _key to _item's id)\
(set: $objects's _key's loc to _room)\

At one point I needed to check if the user is carrying anything, so I do it like this:

(set: _carried to (find: _obj where _obj's loc is "Inventory", ...(data-values:$objects)))

and verify (if: _carried's length is not 0)

It’d be straightforward to add a good/bad flag to the object data and adapt the filter to calculate the rate of good and bad objects.

Edit: to implement the object limit you can check if the inventory is full (when the length of carried objects is 5), and either prevent the action disabling a link, or abort the action printing an error message.

3 Likes