Jump to content


Photo
* * * * * 7 votes

[Tutorial] How To Create A Simple Inventory


  • Please log in to reply
41 replies to this topic

#1 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 04 March 2014 - 09:48 PM

  • Title: How to create a simple inventory
  • Description: Tutorial about inventory systems
  • GM Version: GM8.1
  • Registered: No
  • File Type: .gm81
  • File Size: 10Kb
  • File Link: .gmk only, ZIP archive
  • Tags: inventory, tutorial, arrays, 2d, simple, simple inventory system, system, easy

Summary
This tutorial will show you how to easily create a simple (but powerful) inventory systems.
 
A lot of people don't know how to create inventory systems for their games, so I'll be showing you how in this tutorial.

First off, set up some basic variables.
In this example, we will need to make a few variables:

  • A variable to determine whether the inventory menu is open or not
  • A variable to limit how many items a user can have in their inventory

So, here's how we're going to do it:
Create an object and call it obj_inventory.
Make a new event_create.gifCreate Event and drag a action_code.gif Execute Code action into it.
Enter the following code into the editor:

/*-----------------*\
|--Basic Variables--|
\*-----------------*/
showinv = false; //Determines if the inventory should be displayed or not
globalvar maxInvSlots; //Determines the maximum amount of items that the player can have at once
maxInvSlots = 9; //Setting maxInvSlots
  • showinv = false: This means that the inventory menu is not open. When it is open, we will set this to true.
  • globalvar maxInvSlots: This line is declaring the variable maxInvSlots. We put globalvar in front of it so we can use it in any script in the game instead of just the object that this script is in.
  • maxInvSlots = 9: This sets the value of our maxInvSlots variable. Coding is weird, so if we set it to 9, we will have 10 slots.

Now we need to create the inventory slots.
The easiest way to do this would be to use this code:

//Create the inventory slots
for(i=0;i<=maxInvSlots;i+=1)
{
    global.inventory[i,0] = "";
}
  • for(i=0;i<=maxInvSlots;i+=1): I'm not going to explain For loops, but you can read up on them here. This loop cycles for every slot in maxInvSlots.
  • global.inventory[i,0] = ""; This creates a new array for us which will hold all of the user's items. Since our maxInvSlots variable is 10, it would make the array 10 times (global.inventory[1-10,0]).

Now, we can create our item database
Your script should look like this so far:

/*-----------------*\
|--Basic Variables--|
\*-----------------*/
showinv = false; //Determines if the inventory should be displayed or not
globalvar maxInvSlots; //Determines the maximum amount of items that the player can have at once
maxInvSlots = 9; //Setting maxInvSlots

//Create the inventory slots
for(i=0;i<=maxInvSlots;i+=1)
{
    global.inventory[i,0] = "";
}

This is nice, but we can add more to it. Let's do our item DB. Enter the following code into the editor:

/*---------------*\
|--Item Database--|
\*---------------*/

//Test Item One//
global.items[0,0] = "Test Item One"         //Name
global.items[0,1] = "This is a test1"       //Desc
global.items[0,2] = 0                       //Type (Item, Potion, Weapon)
global.items[0,3] = 1                       //Price

//Test Item Two//
global.items[1,0] = "Test Item Two"         //Name
global.items[1,1] = "This is a test2"       //Desc
global.items[1,2] = 1                       //Type (Item, Potion, Weapon)
global.items[1,3] = 2                       //Price

//Test Item Three//
global.items[2,0] = "Test Item Three"       //Name
global.items[2,1] = "This is a test3"       //Desc
global.items[2,2] = 2                       //Type (Item, Potion, Weapon)
global.items[2,3] = 3                       //Price
  • global.items[0,0] = "Test Item One": The first 0 is the item ID, and the second one is the property. Think of it this way: array[0,0]: The item is a cell phone. array[0,0] = "iPhone": The name is "iPhone". What we are doing here is creating an item, and giving it certain properties.
  • global.items[0,1] = "This is a test1": This is the second property of item 0; The description.
  • global.items[0,2] = 0; This is the third property of item 0; The type. 0 = item. 1 = potion. 2 = weapon.
  • global.items[0,3] = 1; This is the fourth property of item 0, and it is optional. It's the price in which you can sell/buy it.

Now that we have a database for our items, lets move on to actually obtaining said items.
To do this, we need to create a new script. To do this, go to Resources at the top and press Create Script.
Call this script "ItemPickup" and put the following code into it:

for(i=0;i<=maxInvSlots;i+=1)
{
    if global.inventory[i,0] = "" {global.inventory[i,0] = argument0; i = maxInvSlots}
}

Instead of explaining what each line does, I'm going to explain what the script does.
This script cycles through all of our slots, and finds the next empty one. Once it finds one, it adds the item argument0 to the inventory. You will define argument0 as any of our items. Then, it sets i to maxInvSlots so the for loop stops running.
 
Now, let's create 3 event_keypress.gif key-press events.

  • In the first one: 
    ItemPickup(global.items[0,0]);
  • In the second: 
    ItemPickup(global.items[1,0]);
  • In the third:
    ItemPickup(global.items[2,0]);

This will pick up the item with the ID of the first number in our array.
 
Let's let the user show the inventory
By default, the inventory will be hidden. Create a new event_keypress.gif key-press event and add a action_code.gif Execute Code action to it. Enter this code into the editor:

if(showinv==true) {
    showinv=false;
} else if(showinv==false) {
    showinv=true;
}

Now, create a new event_draw.gif Draw event
Add a action_code.gif Execute Code action to it. Enter this code into the editor:

if(showinv==true) {
    draw_text(x+16,y+16,"Inventory");
    for(i=0;i<=maxInvSlots;i+=1)
    {
        draw_text(x+16,y+32+(16*i),global.inventory[i,0]);
    }
}

It's time to run the game!
Create a room and put obj_inventory anywhere.
Then, run the game!
Once it's open, press 1, 2 and to add your items to the inventory. Once you've done that, press i and see the items pop up on screen! Press again to close the menu.
 
Congrats! You've made an inventory!
 
I really hope I've helped all of you great developers!
-nlg


  • 10

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 25708 posts
  • Version:GM:Studio

Posted 04 March 2014 - 09:59 PM

This is a NICE tutorial! Well done!


  • 2

U1FVsm3.png

40799.png


#3 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 04 March 2014 - 10:16 PM

This is a NICE tutorial! Well done!

Thank you! I was kind of scared because this is my first tutorial ever. I'm glad you like it :)


  • 2

#4 geig3r

geig3r

    GMC Member

  • GMC Member
  • 20 posts
  • Version:GM:Studio

Posted 09 March 2014 - 02:05 AM

This is a NICE tutorial! Well done!

 

I second that.  On top of being one of the best presented examples I've seen, it provides a very clear, simple and straight forward example of a common mechanic used in games.  Nice work!


  • 0

#5 marcus9135

marcus9135

    GMC Member

  • GMC Member
  • 21 posts
  • Version:GM:Studio

Posted 07 April 2014 - 07:28 AM

How would a set sprite to each of these items? I tried myself but have failed


  • 0

#6 iiRomano

iiRomano

    GMC Member

  • GMC Member
  • 879 posts
  • Version:Unknown

Posted 08 April 2014 - 09:48 PM

 

This is a NICE tutorial! Well done!

 

I second that.  On top of being one of the best presented examples I've seen, it provides a very clear, simple and straight forward example of a common mechanic used in games.  Nice work!

 

I second that.


Edited by iiRomano, 10 April 2014 - 07:05 PM.

  • 0

#7 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 14 April 2014 - 03:54 AM

 

 

This is a NICE tutorial! Well done!

 

I second that.  On top of being one of the best presented examples I've seen, it provides a very clear, simple and straight forward example of a common mechanic used in games.  Nice work!

 

I second that.

 

Thank you :)


Edited by non lamo gamo, 03 May 2014 - 08:58 PM.

  • 0

#8 jessica100

jessica100

    GMC Member

  • GMC Member
  • 30 posts
  • Version:GM:Studio

Posted 14 April 2014 - 06:12 AM

how would I go about making it so the items have sprites, and I pick them up on collision with player? and also, how can I test if an object is in my inventory so I can use it? thank you! it works great for a first tutorial :)


  • 0

#9 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 03 May 2014 - 08:58 PM

how would I go about making it so the items have sprites, and I pick them up on collision with player? and also, how can I test if an object is in my inventory so I can use it? thank you! it works great for a first tutorial :)

Working on that now!


  • 0

#10 iiRomano

iiRomano

    GMC Member

  • GMC Member
  • 879 posts
  • Version:Unknown

Posted 03 May 2014 - 09:11 PM

Working on that now!

How would I save and load with this inventory system?


  • 0

#11 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 03 May 2014 - 09:13 PM

 

Working on that now!

How would I save and load with this inventory system?

 

Saving and loading is a whole nother topic, but the data should carry over when using GameMaker's default save/load functions. I can make a tutorial for saving and loading, but that will be when I finish my updated inventory system tutorial :)


  • 0

#12 iiRomano

iiRomano

    GMC Member

  • GMC Member
  • 879 posts
  • Version:Unknown

Posted 03 May 2014 - 09:16 PM

Saving and loading is a whole nother topic, but the data should carry over when using GameMaker's default save/load functions. I can make a tutorial for saving and loading, but that will be when I finish my updated inventory system tutorial :)

Awesome, good job on the tutorial! :)


  • 0

#13 raz899

raz899

    GMC Member

  • New Member
  • 9 posts
  • Version:Unknown

Posted 23 June 2014 - 04:18 PM

This is an awesome tutorial, thank you, I do have one question though, how would you be able to have it so that I draw the description alongside the Item I picked up. 

 

Would I have to create an additional slot ? 

Then draw next to the appropriate slot? 

 

 

for(i=0;i<=maxInvSlots;i+=1)
{
    global.inventory[i,1] = "";

 

}

  • 0

#14 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 04 July 2014 - 03:34 PM

 

This is an awesome tutorial, thank you, I do have one question though, how would you be able to have it so that I draw the description alongside the Item I picked up. 

 

Would I have to create an additional slot ? 

Then draw next to the appropriate slot? 

 

 

for(i=0;i<=maxInvSlots;i+=1)
{
    global.inventory[i,1] = "";

 

}

 

I'm currently working on a tutorial that explains the process of expanding your inventory's potential. This is just a rough guess, but I will fully cover it in the next tutorial.

if(showinv==true) {
    draw_text(x+16,y+16,"Inventory");
    for(i=0;i<=maxInvSlots;i+=1)
    {
        draw_text(x+16,y+32+(16*i),global.inventory[i,0]);
        draw_text(x+48,y+32+(16*i),global.inventory[i,1]);
    }
}

You can change x+48 to whatever offset suits your needs.

Cheers!

-nlg


  • 0

#15 denton

denton

    GMC Member

  • GMC Member
  • 55 posts
  • Version:Unknown

Posted 25 July 2014 - 11:45 AM

Anyone have ideas about getting sprites to apply to the  inventory yet? I'm attempting but no luck so far.


  • 0

#16 rv_matt

rv_matt

    GMC Member

  • GMC Member
  • 10 posts
  • Version:GM:Studio

Posted 28 July 2014 - 04:37 PM

Really good, this is a perfect beginning for an Inventory system!! ;)


  • 0

#17 Yrnwyl

Yrnwyl

    GMC Member

  • New Member
  • 10 posts
  • Version:GM:Studio

Posted 17 August 2014 - 02:15 AM

 
Now, let's create 3 event_keypress.gif key-press events.

  • In the first one: 
    ItemPickup(global.items[0,0]);
  • In the second: 
    ItemPickup(global.items[1,0]);
  • In the third:
    ItemPickup(global.items[2,0]);

This will pick up the item with the ID of the first number in our array.
 

Are these on the "Hero" or are they scritp like the "itemPickup"?

And do I have to have a unique event for each Item that you can pickup in the game?

Also If I have to assign each item a unique event Can I bind them all to one key?


Edited by Yrnwyl, 17 August 2014 - 02:15 AM.

  • 0

#18 Xezog42

Xezog42

    GMC Member

  • GMC Member
  • 56 posts
  • Version:GM:Studio

Posted 09 September 2014 - 11:38 AM

Nice tutorial! How can I "ask" for stuff though? F

or instance: "what item type is the item in slot 4/ what item is in slot 4" but also maybe something like "<loop through all>, if the item is type 1 (weapon) draw sprite_blabla, image index 1. (a picture of a weapon)"

another thing, how could I make things stack? Maybe even another line in the item database, saying if it is stackable or not, that would be cool. (everything stackable would be great too if that makes it simpler).

Last one: How can I make items be removed? For instance eating food or drinking a potion, but also equipping a weapon or armor.

 

Would be awesome if you could answer maybe a few of them, especially the first one! Thanks for the tutorial again.

 

EDIT: Just remembered another queston :P how can i "ask" how many items are in the inventory?


Edited by Xezog42, 09 September 2014 - 11:39 AM.

  • 0

#19 Xezog42

Xezog42

    GMC Member

  • GMC Member
  • 56 posts
  • Version:GM:Studio

Posted 13 September 2014 - 07:24 PM

Does anyone know how to do the stuff? :/


  • 0

#20 phillipPbor

phillipPbor

    GMC Member

  • GMC Member
  • 308 posts
  • Version:GM:Studio

Posted 13 September 2014 - 07:39 PM

I want to know as well...
  • -1
I need my reputation backed up. my Cave Story fan game was work in progress

#21 NotWhoYouThink

NotWhoYouThink

    GMC Member

  • GMC Member
  • 1061 posts
  • Version:Unknown

Posted 17 September 2014 - 12:05 PM

<snip>

Really helpful! Thanks!


  • 0

This forum is archived. The above may not reflect my current opinion, or may be irrelevant now.


#22 Lucky Duck

Lucky Duck

    GMC Member

  • Banned Users
  • 155 posts
  • Version:GM:Studio

Posted 17 September 2014 - 12:07 PM

Thanks a lot.. I have been reviewing tuto's. It's one of the most helpful of those.

 

********

MODERATOR EDIT:  Several posts removed.  Please use the topic to discuss the tutorial.  Other discussions belong in a PM.


Edited by chance, 17 September 2014 - 01:33 PM.


#23 Moondrop

Moondrop

    GMC Member

  • New Member
  • 7 posts
  • Version:GM:Studio

Posted 25 September 2014 - 12:16 PM

To draw a sprite, you first need to add the sprite index in your inventory array, for example

global.items[0,0] = "Test Item One" //Name
global.items[0,1] = "This is a test1" //Desc
global.items[0,2] = 0 //Type (Item, Potion, Weapon)
global.items[0,3] = 1 //Price
global.items[0,4] = spr_ItemOne //sprite

Then, in the draw event, you need to use the commande draw_sprite instead of draw_text, pointing at the right porperty of your array :

    if(showinv==true) {
        draw_text(x+16,y+16,"Inventory");
        for(i=0;i<=maxInvSlots;i+=1)
        {
            draw_sprite(global.inventory[i,4],0,x+16,y+32+(16*i));
        }
    }

Here, we have it point at the associated sprite name, using subimage 0, at the right coordinates. Make sure you match properly the name of your sprite in both the sprite itself and the array.

 

Hope this will help.

 

As for managing stacking, numbers of item and so on, don't really have the time to put it in details now, but here's some general "how to" that should help you figure it out : basically, it's the same as we did just above, add a "property" in your array for "quantity" that you can call back with adding a draw_text pointing at it in the code above, next to the draw_sprite. To "remove" one, you'll need to set up a condition checking if the quantity is >0 and either draw it or not, though it'll need some code adjustement to avoid gaps in the inventory.


  • 0

#24 adamu

adamu

    GMC Member

  • GMC Member
  • 124 posts
  • Version:GM:Studio

Posted 13 October 2014 - 10:51 PM

ty for a great tut! good job mate!

 

i did anything as u said and at the end i got stucked with an error..

 

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Key Press Event for <Ctrl> Key
for object keyCTRL:

Push :: Execution Error - Variable Get -1.showinv(100011, 0)
 at gml_Object_keyCTRL_KeyPress_17 (line 1) -     if(showinv==true) {
############################################################################################
 

can u help me solve this out?

ty!


  • 0

#25 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 13 October 2014 - 11:04 PM

ty for a great tut! good job mate!

 

i did anything as u said and at the end i got stucked with an error..

 

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Key Press Event for <Ctrl> Key
for object keyCTRL:

Push :: Execution Error - Variable Get -1.showinv(100011, 0)
 at gml_Object_keyCTRL_KeyPress_17 (line 1) -     if(showinv==true) {
############################################################################################
 

can u help me solve this out?

ty!

It looks like showinv isn't declared. You probably forgot to make it global in the early steps :)

 

Cheers!

-nlg


  • 0

#26 adamu

adamu

    GMC Member

  • GMC Member
  • 124 posts
  • Version:GM:Studio

Posted 14 October 2014 - 12:04 AM

i get a lot of errors... :S what to do? T_T


  • 0

#27 TsukaYuriko

TsukaYuriko

    Remember... and never forget

  • Global Moderators
  • 9535 posts
  • Version:GM:Studio

Posted 14 October 2014 - 12:12 AM

Don't use an engine made for GM8.1 under anything but GM8.1. That helps in most cases. ;)
  • 0

yBGBXQa.pngmWOQbeq.png


#28 adamu

adamu

    GMC Member

  • GMC Member
  • 124 posts
  • Version:GM:Studio

Posted 14 October 2014 - 12:25 AM

i have game maker studio


Edited by adamu, 14 October 2014 - 12:26 AM.

  • 0

#29 TsukaYuriko

TsukaYuriko

    Remember... and never forget

  • Global Moderators
  • 9535 posts
  • Version:GM:Studio

Posted 14 October 2014 - 12:37 AM

...which means this engine was not made for your GM version, therefore you should not be using it. ;)
  • 1

yBGBXQa.pngmWOQbeq.png


#30 azatoch

azatoch

    GMC Member

  • New Member
  • 6 posts
  • Version:GM:Studio

Posted 15 October 2014 - 12:01 AM

I use it and works fine in GMS.


  • 0

#31 Liquid155

Liquid155

    GMC Member

  • New Member
  • 7 posts
  • Version:GM:Studio

Posted 18 November 2014 - 10:27 PM

Great tutorial! Working fine for me in Studio.

 

I've been playing around trying to create a way to remove items from the inventory but am having some trouble.

 

My current setup is that I have an item in my inventory and when I talk to an NPC I want it to be removed. I tried the following script:

//Script name is ItemRemove
// Loop through each Inventory Slot
for(i=0;i<=maxInvSlots;i+=1)
{
//If slot contains Item Name
    if global.inventory[i,0] = "argument0" { 
//Set slot to blank
    global.inventory[i,0] = ""; i = maxInvSlots 
    }
}

Then in my code when talking to the NPC:

//flower being the name of the object
ItemRemove("flower")

It doesn't crash or anything, the game just keeps running but the flower is still listed in my Inventory.

 

Is it a problem with argument0?

 

EDIT: lol, almost immediately noticed my mistake on looking at it again.

 

In my ItemRemove script I have argument0 in "" so the script was looking for an item called argument0, rather than the argument itself :) script should be:

//Script name is ItemRemove
// Loop through each Inventory Slot
for(i=0;i<=maxInvSlots;i+=1)
{
//If slot contains Item Name
    if global.inventory[i,0] = argument0 { 
//Set slot to blank
    global.inventory[i,0] = ""; i = maxInvSlots 
    }
}

Hope someone finds this useful.

 

EDIT2:

 

While we're here, I realise a better way to handle this would be to give each item a starting quantity of 0, increase it when the player picks it up, and only draw when the quanity > 0 but I can't seem to figure out how to do that.

///Draw Inventory Menu

if(showinv==true) {
    draw_text(view_xview[0]+10, view_yview[0]+10, "Inventory");
        for(i=0;i<=maxInvSlots;i+=1)
            {
//Loop through inventory array and if [x, 2] (representing item quantity) is greater than 0, draw the item name
            if (global.items[i,2] > 0)
                {
                draw_text(view_xview[0]+16,view_yview[0]+32+(16*i),global.inventory[i,0])
                }
            else{}
         }
}

Edited by Liquid155, 19 November 2014 - 10:28 PM.

  • 1

#32 cooltrain7

cooltrain7

    GMC Member

  • New Member
  • 18 posts
  • Version:GM:Studio

Posted 22 January 2015 - 07:15 PM

To draw a sprite, you first need to add the sprite index in your inventory array, for example

global.items[0,0] = "Test Item One" //Name
global.items[0,1] = "This is a test1" //Desc
global.items[0,2] = 0 //Type (Item, Potion, Weapon)
global.items[0,3] = 1 //Price
global.items[0,4] = spr_ItemOne //sprite

Then, in the draw event, you need to use the commande draw_sprite instead of draw_text, pointing at the right porperty of your array :

    if(showinv==true) {
        draw_text(x+16,y+16,"Inventory");
        for(i=0;i<=maxInvSlots;i+=1)
        {
            draw_sprite(global.inventory[i,4],0,x+16,y+32+(16*i));
        }
    }

Here, we have it point at the associated sprite name, using subimage 0, at the right coordinates. Make sure you match properly the name of your sprite in both the sprite itself and the array.

 

Hope this will help.

 

 

Ive tried this and i get an error, wondering if anyone can help me ?

//Create event for the controler, i set the sprite
for(i=0;i<=maxInvSlots;i+=1)
{
global.cargo[i,0] = "";
}

global.items[0,0] = "Iron Ore"                        //Name  (Its name)
global.items[0,1] = "A piece of Iron ore"             //Desc  (Its Description)
global.items[0,2] = "Ore"                             //Type  (Item, ore, Weapon)
global.items[0,3] = "100"                             //Price (Its Price)
global.items[0,4] = spr_ore1                          //sprite



I then try to draw the sprite in the draw event of the controler

draw_sprite(global.cargo[i,4],0,x,y);

And i get this error:

 

Error in code at line 9:
   draw_sprite(global.cargo[i,4],0,x,y);
                      ^
at position 21: Unknown variable cargo or array index out of bounds

 

 

Any ideas ?


Edited by cooltrain7, 22 January 2015 - 07:17 PM.

  • 0

#33 Shadowblades

Shadowblades

    GMC Member

  • New Member
  • 6 posts
  • Version:GM:Studio

Posted 06 February 2015 - 02:04 AM

 

To draw a sprite, you first need to add the sprite index in your inventory array, for example

global.items[0,0] = "Test Item One" //Name
global.items[0,1] = "This is a test1" //Desc
global.items[0,2] = 0 //Type (Item, Potion, Weapon)
global.items[0,3] = 1 //Price
global.items[0,4] = spr_ItemOne //sprite

Then, in the draw event, you need to use the commande draw_sprite instead of draw_text, pointing at the right porperty of your array :

    if(showinv==true) {
        draw_text(x+16,y+16,"Inventory");
        for(i=0;i<=maxInvSlots;i+=1)
        {
            draw_sprite(global.inventory[i,4],0,x+16,y+32+(16*i));
        }
    }

Here, we have it point at the associated sprite name, using subimage 0, at the right coordinates. Make sure you match properly the name of your sprite in both the sprite itself and the array.

 

Hope this will help.

 

 

Ive tried this and i get an error, wondering if anyone can help me ?

//Create event for the controler, i set the sprite
for(i=0;i<=maxInvSlots;i+=1)
{
global.cargo[i,0] = "";
}

global.items[0,0] = "Iron Ore"                        //Name  (Its name)
global.items[0,1] = "A piece of Iron ore"             //Desc  (Its Description)
global.items[0,2] = "Ore"                             //Type  (Item, ore, Weapon)
global.items[0,3] = "100"                             //Price (Its Price)
global.items[0,4] = spr_ore1                          //sprite



I then try to draw the sprite in the draw event of the controler

draw_sprite(global.cargo[i,4],0,x,y);

And i get this error:

 

Error in code at line 9:
   draw_sprite(global.cargo[i,4],0,x,y);
                      ^
at position 21: Unknown variable cargo or array index out of bounds

 

 

Any ideas ?

 

 

I also get an error and would like to know how to make sprites appear instead of text  :(


Edited by Shadowblades, 06 February 2015 - 02:04 AM.

  • 0

#34 LurchBrick

LurchBrick

    GMC Member

  • GMC Member
  • 141 posts

Posted 06 February 2015 - 03:49 AM

@cooltrain7 - Can you post what's in your pickup items script? This is most likely where your error is.


Edited by LurchBrick, 06 February 2015 - 03:54 AM.

  • 0

#35 laxeot

laxeot

    GMC Member

  • GMC Member
  • 100 posts
  • Version:GM:Studio

Posted 06 February 2015 - 07:34 PM

 

To draw a sprite, you first need to add the sprite index in your inventory array, for example

global.items[0,0] = "Test Item One" //Name
global.items[0,1] = "This is a test1" //Desc
global.items[0,2] = 0 //Type (Item, Potion, Weapon)
global.items[0,3] = 1 //Price
global.items[0,4] = spr_ItemOne //sprite

Then, in the draw event, you need to use the commande draw_sprite instead of draw_text, pointing at the right porperty of your array :

    if(showinv==true) {
        draw_text(x+16,y+16,"Inventory");
        for(i=0;i<=maxInvSlots;i+=1)
        {
            draw_sprite(global.inventory[i,4],0,x+16,y+32+(16*i));
        }
    }

Here, we have it point at the associated sprite name, using subimage 0, at the right coordinates. Make sure you match properly the name of your sprite in both the sprite itself and the array.

 

Hope this will help.

 

 

Ive tried this and i get an error, wondering if anyone can help me ?

//Create event for the controler, i set the sprite
for(i=0;i<=maxInvSlots;i+=1)
{
global.cargo[i,0] = "";
}

global.items[0,0] = "Iron Ore"                        //Name  (Its name)
global.items[0,1] = "A piece of Iron ore"             //Desc  (Its Description)
global.items[0,2] = "Ore"                             //Type  (Item, ore, Weapon)
global.items[0,3] = "100"                             //Price (Its Price)
global.items[0,4] = spr_ore1                          //sprite



I then try to draw the sprite in the draw event of the controler

draw_sprite(global.cargo[i,4],0,x,y);

And i get this error:

 

Error in code at line 9:
   draw_sprite(global.cargo[i,4],0,x,y);
                      ^
at position 21: Unknown variable cargo or array index out of bounds

 

 

Any ideas ?

 

Found your error! In your code, you're not doing global.cargo[x,x]=x, youre doing global.items[x,x]=x.

//Create event for the controler, i set the sprite
for(i=0;i<=maxInvSlots;i+=1)
{
global.cargo[i,0] = "";
}

global.cargo[0,0] = "Iron Ore"                        //Name  (Its name)
global.cargo[0,1] = "A piece of Iron ore"             //Desc  (Its Description)
global.cargo[0,2] = "Ore"                             //Type  (Item, ore, Weapon)
global.cargo[0,3] = "100"                             //Price (Its Price)
global.cargo[0,4] = spr_ore1                          //sprite

The above code should fix it! :)


  • 0

#36 aiat_gamer

aiat_gamer

    GMC Member

  • GMC Member
  • 115 posts
  • Version:GM:Studio

Posted 24 May 2015 - 01:08 PM

I can figure out how to add the item text AND the sprite to the draw event for the inventory...The pickup script only adds one thing to the inventory array. Can anyone help me?


  • 0

#37 Rares

Rares

    GMC Member

  • New Member
  • 10 posts
  • Version:Unknown

Posted 22 June 2015 - 03:32 PM

Does anyone know how can I make one that sticks to my view or something like that?And it blurs the screen a bit when I open it or something like that?

 

And I want to make a game where you can only carry two things so you will not need to open your inventory or something like that, so how can I make it stick so I can make it a bar or something like that.If anyone could help I would be thankfull!


Edited by Rares, 22 June 2015 - 03:33 PM.

  • 0

#38 Rares

Rares

    GMC Member

  • New Member
  • 10 posts
  • Version:Unknown

Posted 23 June 2015 - 06:35 AM

Hey Moondrop I tried your code to make the items appear as a sprite but I get this 

 

 
___________________________________________
ERROR in
action number 1
of Draw Event
for object obj_inventory:
 
Error in code at line 5:
               draw_sprite(global.inventory[i,4],0,x+16,y+32+(16*i));
                                  ^
at position 33: Unknown variable inventory or array index out of bounds

  • 0

#39 Le_Beholder

Le_Beholder

    GMC Member

  • GMC Member
  • 89 posts
  • Version:GM8

Posted 01 July 2015 - 05:18 PM

well. im glad you posted the summery, because otherwise the tutorial file itself is useless to me since i use GM 8.0 only.

 

i will try to follow the steps to see what happens, albeit, i still feel punished for using 8.0 still nowadays, which i know is totally bunk. but..... still cant help the feels...


Edited by TsukaYuriko, 01 July 2015 - 05:19 PM.
Massive quote removed

  • 0

#40 Hellsgremlin

Hellsgremlin

    GMC Member

  • New Member
  • 9 posts
  • Version:Unknown

Posted 23 August 2015 - 06:01 PM

Hey,  Had some trouble getting this to work.    After about a Hour reading your tut over n over too see why i was having a problem with all

 

ty for a great tut! good job mate!

 

i did anything as u said and at the end i got stucked with an error..

 

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Key Press Event for <Ctrl> Key
for object keyCTRL:

Push :: Execution Error - Variable Get -1.showinv(100011, 0)
 at gml_Object_keyCTRL_KeyPress_17 (line 1) -     if(showinv==true) {
############################################################################################
 

can u help me solve this out?

ty!

 

 

Anyone Else experiencing this error should note.

 

The problem is most likely with your script name....  "ItemPickup" ----Game maker coding is case sensitive & There is both a Capitol I and Capitol P in that name,  So rename the file with the proper uppercase letters.

 

Alternatively,

You can change the letters in the code - edit the letters in your 3 button press events shown below.


Edited by Hellsgremlin, 24 August 2015 - 03:47 AM.

  • 0

#41 gonzalez_berto

gonzalez_berto

    GMC Member

  • New Member
  • 11 posts
  • Version:GM:Studio

Posted 31 December 2015 - 05:31 AM

Anyone else know why there is an error when creating a Global.items[i.4] = INSERT SPRITE NAME HERE
when i call to draw it up draw_sprite(global.inventory[1,4] ?

Edit:
I FIXED IT. IF you are looking to display a sprite item instead of the text, go back to the ItemPickup Script.
dublicate that and call it itempickup2 or what ever. inside of it change it to this

for(i=0;i<=maxInvSlots;i+=1)
{
if global.inventory[i,2] = noitem_sprite {global.inventory[i,2] = argument0; i = maxInvSlots} ////PLEASE NOTE YOU SHOULD MAKE A BLANK SPRITE CALLED noitem_sprite
}

instead of "" it should be 0, and also the second box in the array should be set to whatever number you want to use to store the sprite index from your creation code.

In the creation code it would be
Global.items[0,2] = pickaxe_sprite
Global.items[1,2] = BronzeHelmet_sprite
etc.

in the draw event, modifie it so it says [i,2]


  • draw_text(x+16,y+32+(16*i),global.inventory[i,2]);
And finally in the pickup event (1 2 3 keys)
excute the Itempickup2 Script with what ever item you want to pick up, but with 2 in the second box of the array ( [0,2] , [1,2] , [2,2], [3,3]... etc )

One more thing. Delete the original Creation code and change it to this

//Create the inventory slots
for(i=0;i<=maxInvSlots;i+=1)
{
global.inventory[i,0] = "";
global.inventory[i,2] = noitem_sprite; <-------- you need to have a blank sprite for it to work.
}

this is very important.

Edited by TsukaYuriko, 31 December 2015 - 09:07 PM.
Double post merged

  • 0

#42 Geo_14

Geo_14

    GMC Member

  • GMC Member
  • 27 posts
  • Version:GM:Studio

Posted 06 January 2016 - 11:24 AM

Based on all the posts above, i created a simple inventory where you can pickup items, their sprites are added to the inventory and you can remove them from the inventory aswell.

 

Thanks for this great tutorial and everyone who posted solutions to adding sprites.

Here are some downloads (in different formats for different versions of gamemaker) for those who are having problems creating an inventory with sprites instead of text:

 

GameMaker: Studio

 

GameMaker 8.1

 

GameMaker 8


  • 0

                                                                                Nothing more...

                               ...Nothing less!

 

                                         qnOpkEa.png