Inventory Management System for games using CoronaSDK

Depending on the game that you are building, you might have the requirement to implement an inventory management system. Not the one that is used in Warehouses or Best Buys, but a simple one that keeps track of the items that you have picked up during the game.

Inventory management is not a new thing, since the first text adventure game that was programmed, inventory management has been an important factor in the playability of the game. This is integral to most adventure games.

Imagine the scenario in a text adventure,

> you see the following items
* an axe
* a bow and some arrows

what do you do?

> pick axe
You pick up the axe
> examine axe
It is an old rusty axe with a solid wooden handle, the hilt is shiny but covered with dust and blood.

and somewhere down the game you try

> shoot deer
shoot deer with what?
> shoot deer with bow
Cannot do that, you do not have the bow

that is because you picked up the axe and not the bow. In a similar scenario, lets say the adventurer can carry only 3 items and ever time he picks up a new item, the oldest item is dropped.

Now with iOS, Android ,WP7 and the other Mobile devices, the days of the text adventure are kind of over, no one would want to play the text adventures and type out the commands, however the point and click variety has become very popular. I recollect my first point and click adventure, it was "The Day of the Tentacle" it ran from a CD Drive and required a sound blaster card. For the young ones, computers did not come with a Sound Card and had no CD-Drives, which was kind of strange for me as the earlier generation of computers, 8-bit or 16-bit were all inclusive, like the ZX Spectrum, C=64, Amiga, Atari ST, and so on. They would allow picking up an item by clicking on it or interacting with the scene by clicking on the item.

So, enough of the background building, lets jump into how to code the inventory system in Corona SDK.

First things first, let us create the items list. This is important because we shall use this to refer to the items PropertyBag.

local items = {

This has just created a list of 10 items in our inventory. This is like the master list from where we can use the items. For a Text game, these can be descriptions of the items, for a Graphical (GUI-based) game these can be the items that we need to display as icons, so the filenames are used.

local slots = 5             -- This is the maximum number of items that we can carry
local inventoryBag = {}     -- This is where the items are stored in a table

Now we need to declare the five functions that we shall use, namely
local addItemToInventory
local removeItemFromInventory
local saveInventory
local loadInventory
local displayInventory
--The local functions that are used to manage Inventory

The addItemToInventory is used to add an item to the inventory, almost like a listItem in Visual Basic or a TableRow on the iOS. So the parameters it would take are itemIndex, inSlot and redraw.
The itemIndex is the item number from the items tables, this is a number from 1 to the max number of items in item.
The inSlot parameter determines where in the inventory would you want to place this item.
The redraw parameter is just used to pass if at the end of the addItem... function the display of the items has to be refreshed.

The logic behind the addItemToInventory is that we simply enter the itemIndex into inventoryBag at the inSlot using
inventory[inSolt] = itemIndex

Removing an item from the inventoryBag is simple as setting the item to 0, which we handle as No Item

Save Inventory is a function that is used to save the inventory items to file, the way to do that is iterate through the items and save them to the file using
local i
for i=1, slots do 
slots was identified as the variable that holds the maximum number of  slots (indicating the number of items the player can hold)

--Save all the items to propertyBag first
  prop:setProperty("Item"..i, inventoryBag[i])

--Now let us save these to file on the local drive

Loading is the same but in reverse, so what we do is load the data and set the structure of our inventoryBag as
local i
 for i=1, slots do
  local val = tonumber(prop:getProperty("Item"..i, 0))
  inventoryBag[i] = val

Now the last bit that we need to achieve is displaying the inventory
local i
  for i=1, slots do
    local itemsInSlot - inventoryBag[i]
   if itemsInSlot~=nil or itemsInSlot ~=0 then
In a GUI interface, you would not print the items but instead display them somehow. this is entirely up to you on how you do that. It could be as slots on the screen, it could be on a popup HUD type interface in a grid, it could be using the UIListView. So for simplicity, I am just displaying them on the console.

Now let us test the routine, there is no GUI for this test, all output is displayed and checked in the terminal. so
print("Loading Inventory")

addItemToInventory(3,1)  -- Add the Channel-Logo
addItemToInventory(6,2)  -- Add the Pink-Glasses
addItemToInventory(2,4)  -- Add the Black-Glasses
displayInventory()  -- Show what we have in our inventory
saveInventory()   -- Save the inventory to disk


Now at this point we can get the Items that are loaded from the system/game

Now, let us try to replace that with Pink Glasses

We can use the displayInventory() to see/show the contents of the items.
We can also use the saveInventory() to save the contents of the disabled items.

The entire code can be downloaded from the my GitHub and played with to see how it all works. So how this provided a starting insight into managing an inventory management system using corona SDK for a game.

Your comments and feedback are greatly appreciated,


  1. thanks very much for all your work, its great to see this tutorial

  2. What needs to be in the property file to make it work?


Post a Comment

Popular Posts