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.
1 |
<script src="plugins/inventory/InventoryManager-1.0.0.js"></script> |
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.
1 |
var myGame = new Kiwi.Game( '', 'myGame', myState, { plugins:['InventoryManager'] } ); |
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.
1 |
this.cowboy.inventory = Kiwi.Plugins.InventoryManager; |
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.
1 2 |
this.cowboy.inventory.createItem( 'coin' ); this.cowboy.inventory.createItem( 'bag' ); |
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.
1 2 3 4 5 6 |
//Sets all items in the inventory to have a 'money' variable with a default value of 0. Therefore all items currently in, and any added subsequently will have a money variable with a value of $0. this.cowboy.inventory.addVariable( 'money', 0 ); //Sets the variable 'money' on the 'coin' item to the value of 1, making coins worth $1. this.cowboy.inventory.setItemVariable( 'coin', 'money', 1 ); //Changes the variable 'money' on the 'bag' item by increasing the value by 4, making bags of coins worth $5. this.cowboy.inventory.changeItemVariable( 'bag', 'money', 5 ); |
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.
1 2 |
//Increases the number of coins in the inventory by 1 this.cowboy.inventory.changeItemCount( 'coin', 1 ); |
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.
1 2 |
//Removes 25 coins from the players inventory, if they have just bought a new hat, for example. this.cowboy.inventory.changeItemCount( 'coin', -25 ); |
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?
Recent Comments