Creating Inventory – Part I : Basic

There are different ways to create inventory in GameMaker like Arrays, Lists, Grids e.t.c

I am going to create a Grid inventory, so it requires a little knowledge about Grids. We will make our inventory system by scripts. So these can be added to any projects and can used by every object in game.

Lets begin,

Creating Items Data

Each of our item in game will have a specific ID (a number), we will then use these  id’s to in our inventory.Create a script and name it “inventory_init” and write this code in it.

Script: inventory_init

///inventory_init()
enum ITEM {
WOOD = 1,
APPLE,
SWORD,
SHIELD,
POTION
};

*Note: Use simple global variables like global.ITEM_WOOD = 1,global. ITEM_APPLE = 2, global.ITEM_SWORD = 3 if you dont understand enums.

Now we have created ID’s for our items, we can access their ID by using ITEM.WOOD , ITEM.APPLE e.t.c

Creating Inventory:

Creating inventory is easy, we will use ds_grid_create(xSize,ySize) to create our inventory and then will return its index.So create a new script inventory_create and write this code in it.

///inventory_create(Rows,Columns)
var inventory = ds_grid_create(argument[0],argument[1]);
return inventory;

Now we can create multiple inventories, lets create a script that will put item in inventory.Create a script and name it “inventory_add_item” and put this in it.

///inventory_add_item(inventory,itemID,hSlot,vSlot)
ds_grid_set(argument[0],argument[2],argument[3],argument[1]);

Now we can add items in our inventory, lets create another script to get item from inventory from specific slot name it “inventory_get_item“.

///inventory_get_item(inventory,hSlot,vSlot)

return ds_grid_get(argument[0],argument[1],argument[2]);

To remove an item create a script “inventory_remove_item” and put this in it

///inventory_remove_item(inventory,hSlot,vSlot)
ds_grid_set(argument[0],argument[1],argument[2],0); //put 0 in it which means no item.

Now we need another script to destroy our inventory name it “inventory_destroy” and put this in it.

///inventory_destroy(inventory)
ds_grid_destroy(argument[0]);

Hurray! most basic things are done now lets create a in inventory from these scripts.

Lets a create an object, name it obj_player.

In create event we will initiate and create our player inventory with 5 rows and 5 columns totally 25 slots (5×5).

Create Event of obj_player

///Initiate inventory
inventory_init()
inventory = inventory_create(5,5);

//We will use them to order items
hSlot = 0; 
vSlot = 0;

Now we have created inventory its time to add items in it, we will use space key that will add random item to our inventory. Add this code in Key press Space event of object.

Key press Space Event of obj_player

///Add random item in inventory
if ( hSlot >= 5 ) { hSlot = 0; vSlot+=1; }
if ( vSlot >= 5 ) { vSlot = 0;}

var item = choose(ITEM.WOOD,ITEM.SWORD,ITEM.APPLE,ITEM.SHIELD,ITEM.POTION);
inventory_add_item(inventory,item,hSlot,vSlot);

hSlot+=1;

Now its time to draw inventory on our screen, put this code in draw GUI event of obj_player.

Draw GUI Event of obj_player

///Draw inventory
var slot_size = 48;
for (var xx = 0; xx < 5; xx+=1)
for (var yy = 0; yy < 5; yy+=1) 
{
draw_rectangle(xx*slot_size,yy*slot_size,xx*slot_size+slot_size,yy*slot_size+slot_size,true); //draw our slot
var item = inventory_get_item(inventory,xx,yy);
draw_text(xx*slot_size + 20,yy*slot_size+15,string(item)); //draw item id at that position
}

Create a room and put obj_player in it.

If you did everything right your result should look like this:

Capture

Press space multiple times and you will see some thing like this:

inv_2

By pressing space we add a new random item to our inventory. Remember those constants (enum) we created up there, the id of wood is 1 , 2 for apple e.t.c In our first slot we have ID 5 which is ITEM.POTION. Using enums makes easy to use in game.

Lets Add Icons for Items

Instead of drawing itemsID in inventory we are going to draw their specific icons. Download icons and add them in project.

Download Icons from here LINK

Make sure to center their origin,Your icons should look like these:

icons

Lets draw them edit code in Draw Event of obj_player

Draw Event of obj_player

///Draw inventory
var slot_size = 48;
for (var xx = 0; xx < 5; xx+=1)
for (var yy = 0; yy < 5; yy+=1) 
{
draw_rectangle(xx*slot_size,yy*slot_size,xx*slot_size+slot_size,yy*slot_size+slot_size,true); //draw our slot
var item = inventory_get_item(inventory,xx,yy);

switch (item)
{
case ITEM.WOOD : draw_sprite(spr_icon_wood,0,xx*slot_size + 24,yy* slot_size + 24); break;
case ITEM.APPLE : draw_sprite(spr_icon_apple,0,xx*slot_size + 24,yy* slot_size + 24); break;
case ITEM.SWORD : draw_sprite(spr_icon_sword,0,xx*slot_size + 24,yy* slot_size + 24); break;
case ITEM.SHIELD : draw_sprite(spr_icon_shield,0,xx*slot_size + 24,yy* slot_size + 24); break;
case ITEM.POTION : draw_sprite(spr_icon_potion,0,xx*slot_size + 24,yy* slot_size + 24); break;
}
}

Results:

inv_icon

You can now create multiple inventories, add items to your inventory, remove item from your inventory and destroy inventory.

You can download source code from here LINK

Thats all for this part I, I’ll post part II soon which will show you how to create inventory slots and expanded items and some more.

Thanks for reading.

-Humayun

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s