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?
-
Download the inventory.txt file and add it to your project. You don’t need to create any special variables in startup.txt.
-
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 ""
- 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
- 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.