Using the InventoryManager Plugin

Inventories are used in almost all games, whether they are rpgs, action, adventure or any other style or genre. They are helpful tools used to organise and store all the items the a player is carrying, or that exist in your game world. Inventories can be limited sized, so you can only carry a fixed number of items at once, or unlimited. Now we have a Kiwi plugin which allows you to create simple inventories for your characters with an endless number of options.

Download the source files for this example

In the example above control the cowboy using the A and D keys. Collect the falling coins by moving left and right. Our player has an inventory with two items, coins and bags, each worth a different amount of gold. Each time the player collides with an item it is added to their inventory, and their total gold count is updated. The total gold is calculated by taking the count of each item and multiplying it by the money value of the item.

What is an Inventory?

An inventory is a collection of items that either a gameObject or a game has. Inventories keep track of which items, and how many of each, they currently possess.

Downloading and Adding the Plugin

Download the InventoryManager plugin from here. One you have it installed, unzip the folder to the location of your game. We recommend containing all your plugins within a plugin folder. Inside the folder you will find two js files, a readme.txt and an example folder. Read through the readme.txt file to get an overview of the plugin. We will be using the InventoryManager-1.0.0.js file for our game as it contains the plugin. There is also a minified version of the plugin if you prefer.

To add the plugin to our game we need to add it as a script to our html file.

We also need to add the plugin to our game’s options variable. Here we specify that we are using one plugin which is called ‘InventoryManager’, which corresponds to the unique name located inside the plugin. This will boot the plugin and add it to our game so we can access it easily.

Now that we have added our plugin let’s look at how to use it.

Creating an Inventory

To add an inventory to your game or game object create an instance of the InventoryManager. This creates a blank inventory with no items.

Creating Items

Before items can be added to the inventory, they need to be created. This is just used to create the space and ids for each item in the inventory. Create new items by using the createItem function and specifying a unique id string. It is recommended to create all potential items for an inventory at the beginning of the game, and then changing the variables throughout, rather than creating items as you progress.

Item Variables

All items have a count and a description variable by default. The count is a number which specifies how many of this item are currently in the inventory, whilst the description is a string with a sentence or two describing what the object is. Additional variables, such as worth, weight, size, number of uses etc. can be added to a specific item using the setItemVariable function, or to all items in the inventory using the addVariable function. The changeItemVariable function can also be used but note that the value is added to the default value, instead of replacing it.

Adding and Removing Items

To add items to the inventory we just need to change the item’s count variable. This can be done either through the changeItemVariable function explained above or by using the changeItemCount function. This function increments the items count by the number specified.

Similarly, items can be removed from the inventory by passing a negative number through. Note that reaching a count of 0 does not delete the item’s location from the inventory, it just means the player, or user, has none of that item currently. There is no way of completely removing an item’s location from the inventory.


Now that you know how to use the InventoryManager Plugin to assist your game development, why not give it a try and see what you can come up with?


Share the joy
Comments 2

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">