Yet another inventory system

Updated Today (Feb 12, 2022)! An update (v0.2) with performance improvements has been released. It should replace the current version in projects where you want to store many items.

Grab it here


Inspired by Twiger_Fluff’s implementation of lists, I created this small module for inventory management. I’m using it and I think it’s simple enough to be easily used by anyone in their project.

There are currently four “functions”:

add - adds one or more units of an item to an inventory;

remove - removes one or more units of an item from an inventory;

count - indicates how many units of an item there are in an inventory;

display - writes the content of an inventory on the screen.

I may implement other functions if there are requests for them. I’ve considered using the weight factor, but I haven’t needed that yet.

How does this work?

  1. Download the inventory.txt file and add it to your project. You don’t need to create any special variables in startup.txt.

  2. Create string variables in startup.txt for the inventories you need. For example, we can have a pocket and a backpack with items. In that case, you should put the following in startup.txt:

*create backpack ""
*create pocket ""
  1. If you need to use the count function, you must have a numeric variable in startup.txt as well so that you can check the amount. That is,

*create howmany 0

  1. Have fun!

Some examples of use:

Put a torch in the backpack:
*gosub_scene inventory add "backpack" "torch" 1

Put two apples in the backpack:
*gosub_scene inventory add "backpack" "apple" 2

Put 50 coins in the pocket:
*gosub_scene inventory add "pocket" "gold" 50

Remove 15 coins from the pocket:
*gosub_scene inventory remove "pocket" "gold" 15

Count the coins in the pocket:
*gosub_scene inventory count "pocket" "gold" "howmany"
(The variable howmany will contain the amount of coins that are in the pocket; howmany has to be in quotes)

Display the contents of the backpack on the screen:
*gosub_scene inventory display "backpack"

Note: in case you try to remove a larger amount of a certain item than is available, the item is completely taken out of the inventory and an error will NOT be triggered. You should previously check with the count function if there are enough items in the inventory when necessary.

Please let me know if you find any bugs.

13 Likes

I tried it out and it’s pretty straight forward!
Thanks mate!

2 Likes

An update (v0.2) with performance improvements has been released. It should replace the current version in projects where you want to store many items.

2 Likes

The link in the first post still points to the older version, where’s the new v0.2?

Thanks for the warning. I have updated the links in the first post. Please let me know if you find any issues.

1 Like

This is excellent and exactly what I needed. I understand because of the way ChoiceScript is written that this would be too much, but I really want the display list to be alphabetical instead of the order they’re added. That’s just my OCD talking when I see lists like that. haha. Thank you very much for this very easy to understand and use.

2 Likes

I truly understand your need, so I implemented a special version for people with OCD that lists the items alphabetically. :wink: You can get it here.

Please note that for this to work, the list items cannot contain any characters other than letters, numbers and spaces. I haven’t tested it extensively, so if you find any bugs let me know.

3 Likes

You are amazing. I will try this this weekend when I sit down for my writing! Oh I am so excited for this!

2 Likes