Inventory system for multiple items

Hello everyone, I have a test story on twine (sugarcube2, the latest version on web), really just to figure out how to organize an inventory system along with buying and selling items using currency (gold).

I have some basic code that I used from another user’s topic here, which gives a brief overview of items (names, descriptions, and displaying the information when welcomed at the shop). I added other parts to the first section of code that the user wrote, and my code is just below it. If you visit the link, you’ll understand, but the code I wrote is as follows:

<<set $shop to "shop">>

<<set $itemStrawberry to "strawberry">>
<<set $frogLiver to "frogman liver">>
<<set $magicPole to "Dophire">>
<<set $donut to "doughnut">>
<<set $cheese to "block of cheese">>

Our $shop currently carries:"

A $itemStrawberry, for [[5 gold]]
Some $frogLiver, with unknown whereabouts, for [[16 gold]]
The $magicPole, a sword of legend, sworn to have slain a dragon one thousand years ago, for [[12 gold]]
A $donut filled with...something? for [[3 gold]]
A $cheese from a live and very healthy goat, for [[7 gold]]

Most of it is very basic, I know, and I’ve only recently (within the past year or so) really advanced in my coding abilities. I mainly write in python 3, so I tried to use knowledge of that language to change the player inventory. I’ve only coded the option to buy frog liver, which is bought for 16 gold, and the resulting passage looks like this:

"Great choice, my friend!"

<<set $inventory to $frogLiver>>

You have $inventory in your inventory.

"would you like anything else?"

you have 4 gold left.

$donut for [[3 gold|inventory1]]

With the idea that the user starts with 20 gold in their pouch, only a donut can be bought. When you buy a donut, the following will display:

<<set $inventory to $frogLiver, and $donut>>

your inventory has: $inventory

The problem occurs when I test my code. I made some changes from the original, which was the inventory being set to its original value and then also including the donut, but the second variable is the only one acknowledged. Basically, every time I click play, it will say my inventory has a donut in it. I want it to say I have my existing item(s), and whatever new item I just added to it.

I’m not sure how to create more than one shop as well. Say I’m in the village and want to buy some fresh(?) goat cheese, and then want to buy medicine in the town for a hefty price. How could I do that? Like if the village has different stock (milk, honey, fish bait, etc) than the town (shield, sturdy net, wrench, etc), how could I show that difference?

The currency variable is its own thing, I’ll try to search some on that so this topic isn’t too loaded with questions, but if I can’t find anything that helps on it, I may ask for more guidance on how this works.

I’m sorry if this is a repeated topic, I was searching about it for maybe half an hour but with so many users and different ideas, this may just be a unique topic that’s never been brought up; however, if it has, I would appreciate being redirected to it.

About inventory, you may look at Chapel’s. It is absolutely not the only way to do so, but Chapel’s macro are well known and you might get help more easily from other people.

About the shops, I’m not sure I understand your problem. So, basically I’d tell you to put macros to buy milk, honey, etc. in the village passage, and the macros to buy shields, nets, etc. in the town passage.
And if some food rations can be bought both in village and in town, there’s a macro for food rations in both places.

1 Like

you know python. $inventory is a variable that can store single values like $frogLiver but can also be a List (in javascript and twine, called an Array).

<<set $inventory to [$frogLiver, $donut]>>

they can be accessed like you access any value in a List (python)/Array (twine)


1 Like

I’m not too familiar with Chapel, I’ve mostly experimented with Sugarcube, but thank you, I’ll look into it when I’m not busy with school and such! If it’s a more popularly used language then, yeah, it’ll likely be easier to find answers than this, so thanks for the recommendation. :slight_smile:

The shops idea is mostly something I’m not used to, I’ve never properly coded a merchant type system with different stock, and I figured if it was something that could be helped, it’d be better to ask earlier and not later when it’s even more of a struggle/bother. Thank you so much for the help! I may have some difficulties but I’ll try to read up on other topics before I ask other questions if they can be answered that way.

Thank you so much!!! This is super helpful!! I understand python a lot better and this was much easier to understand than when I was reading up on macros. I didn’t think of adding the actual list to the code, but this makes total sense. Arrays didn’t make any sense to me so thank you again! This was a life saver. :grin:

1 Like

Chapel is the username of a person in the Twine community. He has contributed code examples and macros that make certain common tasks like inventory creation easier within Sugarcube.

1 Like

Yes, I realized that soon after I checked out his work on github. I mistook his name as the story format, Chapbook, so I’m sorry for that confusion. Thank you for clarifying, though! I’ve been using several of the macros he’s created (I would assume they’re creations from the extensive information on all of them) for my project and it’s definitely a lot easier to work with than trying to use the other macros on the other Sugarcube documentation because I understand very little of it all, honestly. Thank you and also @souppilouliouma for the help!! I hope you enjoy your Turkey Days (if you celebrate it)!

Sorry this reply is so late, I didn’t realize I hadn’t posted the draft!