This tutorial is created for Game Maker 5 and 6 and assumes a basic knowledge of GML. For this tutorial advanced mode must be enabled. Please note that
there are many methods of creating an inventory and that this is only one method and that there will very likely be better and faster methods available.
I have tried to make a tutorial for this as user friendly as possible. Where there are differences within code between Game Maker 5 and Game Maker 6 I
have placed both codes. All other codes should be compatible with both versions.
Note: this script uses inventory as a variable name and hence it will not work if you have an object, room, sprite or anything else also named inventory.
How to start
First you must think out for your self what you want to have in the inventory.
Which items should be in it. For the tutorial I am going to use the following
items:
lives
sword
large sword
bow
money
We now need to decide how many of each item you may have and how many you get
at the game start
lives (Max. 10, start with 3)
sword (Max. 1 start with 1)
large sword (Max. 1 start without it)
bow (Max. 1 start without it)
money (no max. so add a max of lets say: 999999. start with 10)
For the inventory I am going to use one room in which all
items are displayed as sprites with a number in the corner showing how many
of the item you got. For this inventory I am going to use an array to store
all the data in. We must first decide what data this is. In our case we will
store the following data:
Name of the item
Description of the item
Sprite of the item
Max amount of the item
Current amount of the item
To store this data I am going to use a 2D array. Every item will receive a
number. Within a 2D array you can now store for every item several things. All
this information must be declared at the start of the game. This will go like:
Example code
global.inventory[0,0]:='Lives'; //Name of the item global.inventory[0,1]:='This is the amount of lives you got.'; //discription of the item global.inventory[0,2]:=sprite_lives; //the sprite of the item global.inventory[0,3]:=10; //the maximum value of the item global.inventory[0,4]:=3; //the start value of the item global.inventory[0,5]:='' //The code which needs to be executed when the item is selected
Now you have declared the lives item in your inventory. Please note that All
information is stored in a global variable so every object can read this data
at any time. In my case I gave every piece of info it's own number. For instance
the sprite has received number 2. Also the item lives has received number 0.
If you now want to declare the sword item you change the 0 after global.inventory[
to a new number. You can call the sword for instance 1. Then declaring the sword
will go:
Example code
global.inventory[1,0]:="Sword"; //Name of the item global.inventory[1,1]:="A simple cheap sword"; //discription of the item global.inventory[1,2]:=sprite_sword_basic; //the sprite of the item global.inventory[1,3]:=1; //the maximum value of the item global.inventory[1,4]:=1; //the start value of the item global.inventory[1,5]:="global.weapon:="sword";" //The code which needs to be executed when the item is selected
Now you can use the same code for all the other items as well. You then get
a long script in which every single specification is declared for every item.
Place this script in the Game Start event. The last item in the list of declarations
is the code to execute when the item is selected. You could execute a script
at that time or change a variable. This tutorial does not descripe how to do
this.
Drawing the inventory
You now need to draw the inventory system. In this tutorial I will explain
how to do it in a special room for the inventory where everything is stored.
To draw the inventory a special object is used that is only placed in that room.
To make the inventory look nicely I will take the following sizes:
all the sprites of the items are 100x100
the room is 640x480
Since we have 5 items to display of each 100 width it requires a width of at
least 100*5=500 pixels. Since the room is 640 width we can place the inventory
completely on one line and draw in the rest of the screen the description of
the items. If we have 5 items we have 6 empty spaces between the items. So we
have 6 items of a width of (640-500)/6=23.3 pixels width
Now lets start with the drawing code. We need to draw the items one by one
on the screen. We have five items. So first we need to have a loop that draws
the sprites of all the objects (Of cource the drawing code goes in the drawing
event):
This code will draw the five inventory images on the screen on the top line
as the screenshot shows. However you only want the item to be displayed if it
is in your posession. So you need to use (Of cource the drawing code goes in
the drawing event):
draw_set_font(fnt_your_font); //font to use draw_set_color(c_black); for(i:=0; i<=4; i+=1) { ifglobal.inventory[i,4]>0 then { draw_sprite(global.inventory[i,2],-1,23.3+i*(100+23.3),23.3); } else { draw_rectangle(23.3+i*(100+23.3),23.3,23.3+i*(100+23.3)+100,123.3,false); } }
If the item is not in your this code will draw a white square on the coordinates
the sprite should be drawn at. An other thing what you would like to have is
that the number of items you have of the object is displayed with the image.
You however only want that with items of which you can have more then one. So
use the code (Of cource the drawing code goes in the drawing event):
Now on the right button corner the number is being displayed of the number
of items you have. Now comes the most difficult part of it all. You need to
make it so that when you move your mouse over the image it displays the objects
discription and clicking on it will execute the given code. For this you can
use the following code (Since this code draws the meaning of the object you
are hovering over you must put this code in the drawing event as well):
Example code
Game Maker 5
//if the mouse is going at the correct vertical height:if mouse_y>23.3 and mouse_y<123.3 then{//check every item seperatelyfor(i:=0; i<=4; i+=1)
{//if the mouse is at the correct x coordinateif mouse_x > (23.3 + i*123.3) and mouse_x < (123.3 + i*123.3) then{//set the font and draw the description
font_size:=14;
font_align:=fa_center;
font_color:=c_white;
draw_text_ext(320 , 140 , global.inventory[i,1] , -1 , 630);
//if you click on the itemif mouse_button=mb_leftthen{execute_string( global.inventory[i,5]);
}}}}
Game Maker 6
//if the mouse is going at the correct vertical height:ifmouse_y>23.3 andmouse_y<123.3 then{//check every item seperatelyfor(i:=0; i<=4; i+=1)
{//if the mouse is at the correct x coordinateifmouse_x > (23.3+i*123.3) andmouse_x < (123.3+i*123.3) then{draw_set_font(fnt_your_font);
draw_set_halign(fa_center);
draw_set_color(c_white);
draw_text_ext(320 , 140 , global.inventory[i,1] , -1 , 630);
//if you click on the itemifmouse_button=mb_leftthen{execute_string( global.inventory[i,5]);
}}}}
Now the only thing we still need to do is to create the possibility to get
more items. For this you can use the script:
Example code
//argument0 is the item you want to add//argument1 is the amount you want to addglobal.inventory[argument0,4]+=argument1;
ifglobal.inventory[argument0,4]>global.inventory[argument0,3] then{global.inventory[argument0,4]:=global.inventory[argument0,3];
}
Simply add this script to your game as a script. Lets assume you named it add_item.
Then when you want to add 10 gold coins and you gave the item gold the number
4 use:
Example code
add_item(4,10);
This will add 10 items of kind 4 to the inventory unless you have reached the
maximum number of items.
This code will take one live off your total and if it drops below 0 it will
show the hiscore and restart the game. Else it will only restart the current
room.
I have created a very basic GMD and GM6 to show how to use this code, get it here.
If you want to use multiple lines of items simply paste the code twice and replace
with every command the y coordinate with the new one.
In your iventory example, when you go to the iventory room WITHOUT clicking on any objects eg. bow, when you point the mouse at the empty space, the details are given when there isnt any sprite there, please correct it.
red posted at 2005-12-04 16:21:08
it says that inventory isn't a variable in global.inventory and it also says that there are a wrong number of arguments wen u draw the rectangle
If global.inventory does not exist you forgot to add the creation code. As for the draw_rectangle error. If you use GM5 then use the GM5 code, if you use GM6 then use the GM6 code. Sounds simple doesn't it .
Ben posted at 2005-12-10 04:59:49
what is this, I a to bord to bother
Ben posted at 2005-12-10 05:03:27
oh this is very good work simon, its very good,I can probaly remake the game www.runescape.com if I get a bigg anuof network, sorry for the last post
,Great coding =)
Ben posted at 2005-12-10 05:08:29
sorry, but I just dont get 3-D stuff, and I tryd to get post on GM fourm but stupid parent block dont let me, Dang parent block,
well first I can't see why when I try to make a 3D modle on code its always... I'll just shut up for now, Post Back if im doing somthing stupid!
Ben posted at 2005-12-10 05:11:41
oh, and coool stuff you got here, I got a sprite library, if in need of graphics ask me, plz Lol my post are LAME
,Ben
Jacob posted at 2006-02-04 07:32:10
i keep getting an error and its driven me crazy u see its says that if global.inventory[i,4]>0 then
is an unknown variable or out of array index plz help me.
Jacob posted at 2006-02-04 07:34:05
Ohh and im using version 6.1 could that have anything to do with it
Jacob, check to make sure you don't have a resource named inventory as that's the most commom bug made by people.
jacob posted at 2006-02-07 01:50:09
i did do that but it still says that its unknown
variable iv been reading a book and it tells me the language but urs looks all wrong i dont know plz help me out
Jacob posted at 2006-02-07 02:12:19
not the language it dont seem wrong its the for loop it seems wrong
Jacob posted at 2006-02-09 15:43:54
Im srry its not the for loop and its a known variable but its the array index is out of bounds
Jacob posted at 2006-02-10 00:09:49
do you know how to fix plz help me as soon as possible iv been working on it for 3 weeks striaght.
James posted at 2006-02-10 00:40:10
wheres global inventory?
Jacob posted at 2006-02-10 02:35:51
i make it in a script called Begin_global and then i put it in an object that i call Game_Start and use an event called game start and execute the script and thats all.
12.220.170.156 posted at 2006-02-21 01:03:40
WTF IM NEW AT GM WTF ARE U TALKIN ABOUT GUYS!!!!!!!!!!!!!!!!!!!!!!
12.220.170.156 posted at 2006-02-21 01:05:49
bitchez
Yushatak posted at 2006-03-01 23:48:20
I've never used 2d+ arrays in GM, and I've been using GM for a looong time. Good tut, and a more convenient way than layering normal arrays as I've tried
patriotsfan122 posted at 2006-03-14 21:59:04
alright guys..this is weird..
me and my friend are using GM 6.1 and were trying to make a zelda like game....we have done 3 levels so far..and we wanted to try an inventory.But since we are new to GM this whole code think is way beyond us. Where do we paste the script for an inventory. And is each block of script that is described a seperate script? or is it one long thing?
Doophus2 posted at 2006-03-21 20:51:20
I am getting a error in the fixed code for GM 6.1.
for(i:=0; i0 then
unexpected symbol in expression at 15. can somebody help
To my knowledge my script does not contain an error. Doophus2 is referring to a bug in the script of Stijn192 above.
In order to get me to fix something it's useful to tell what is broken.
schalk posted at 2006-05-26 09:59:42
hey can any post a example of the inventory on here as a gm6 file plz
I'm using gm5(somehow gm6 doen't work on my computer) and i made a game but when i press space to go to my inventory it says this
"
Error in
action number 2
of draw event
for object inventory_controller:
in script draw_script
error in code line 11
if global.inventory[i,4]>0 then
at position 14:Uknow varirable inventory or array index out of bounds
"
ralfi posted at 2006-08-09 03:24:53
i dont understand
Kody: i need help with this code posted at 2006-08-12 04:11:25
global.inventory[1,5]:="global.weapon:="bow";Player:equiped" //The code which needs to be executed when the item is selected
Kody posted at 2006-08-12 05:36:26
How do i fix this
Error in
action number 1
of game start event
for object inventory_controller:
global.inventory[1,5]:="global.weapon:="Bow";" //The code which needs to be executed when the item is selected
And this
Error in
action number 2
of draw event
for object inventory_controller:
The code you define is a string and you place that between "". However you use "" within this string as well which makes Game Maker think that the end of the strong is halfway. Use:
global.inventory[1,5]:="global.weapon:='Bow';"
What's the error you get with the draw command. Does the sprite exist?
@Regbay, make sure you don't have a resource or variable named inventory. See the note at the end of the tutorial.
@schalk, the last paragraph contain a download link to a .gm6 and .gmd.
@ralfi, this script assumes a basic knowledge of GML. Have a read through some tutorials on GML so you can better understand this tutorial.
there is one problem in the inventory,the discription apears even if there is no item
david R posted at 2007-02-13 18:42:37
GM doesn't recognize "inventory" as a variable, i know there has to be a creation code, but what creation code? what do you mean by that??
tom posted at 2007-03-21 20:58:40
lol
Vizierde posted at 2007-03-25 01:33:26
QUOTED FROM ABOVE: [GM doesn't recognize "inventory" as a variable, i know there has to be a creation code, but what creation code? what do you mean by that??]
In the Object's "Creation" event, you need to define the variable. Not sure which variable, I was just browsing the comments here. :3
tom_2 posted at 2007-04-11 17:46:57
untill today i didnt know this game maker exsisted and i used it and this wrked fine for me.
whay are peoplesaying its wrong
Kriogenic posted at 2007-04-28 18:24:11
hmm just a little problem how do i get to the drawing code where i can actually use scripts? thats where im having problems i cant find anywhere and ive looked for a while....
adslfkjad posted at 2007-05-05 02:29:15
can you yive a game maker example, with the game...
Vai'kesh posted at 2007-06-27 11:51:17
but explain us where can we put those codes, especially the first one. if you are so tough teach us don't treat us with superiority please
Z-Mez Prosuctions posted at 2007-08-07 12:58:54
a lot of good source code for you to mess around with and you can get it to work to your specs, but no offense but you should work on it a little more, the raw codes don't work, it needs modding so it will work irght, i got it to work though so thanks for the source
hey thank you so much, I didn't think it was workin' but when I ran it in debug and watched the expression global.inventory[0,4], and wolk over my coins, it went up by ten like I wanted, thank you, your in my game info, lol
evil tax-man procecution posted at 2007-10-13 02:26:08
Cool. Really helped out my game. Can you make a tutorial to show how to make your characters shoot "missilles" - and yes, i'm a newbie
Jarvist posted at 2007-10-23 23:18:35
The code "add_item(4,10);" doesn't work for me, and the inventory screen just pops up pure black. What am I doin' wrong?
frank posted at 2007-12-09 19:47:48
i dont get what u have to do
frank posted at 2007-12-09 19:49:54
DO U PUT CREATE THEN PUT ONE CODE AND PUT ALL THE EXAMPLE CODES IN IT
jerry posted at 2007-12-09 19:55:12
can u put it into vidio form
frank posted at 2008-01-20 20:56:44
i dont understand make vidio if possible
Alex posted at 2008-01-21 22:47:58
I'm using much smaller sprites for my inventory so i think its screwing up the code. They are like 24 by 32. Only the first spr shows up and the others are black square. I'm not completely familiar with GML so i'm not quite sure where to edit the code for this.
Billy posted at 2008-02-03 03:37:08
best inventory tutorial i've seen but still a bit tricky. i want the health to go up when i click something so far i have
global.inventory[0,5]:=health=+20 //The code which needs to be executed when the item is selected
but it dosnt seem to do anything? what do i do
Pete posted at 2008-04-18 11:56:39
Billy that code is defining health and telling it to go up by 20... but do you even have a variable named health which starts at a certain number to add 20 on to... if not it wont do anything...
Jonny posted at 2008-06-04 20:56:04
That's a really great code you've got! There's one thing that I don't understand, however. In the line
for(i:=0; i<=4; i+=1);
how does that work? I realize it's telling the next function to apply to the variables in variable i, but... my head explodes when I try to understand! How does it work?
random posted at 2008-06-09 23:39:27
umm can any1 make a example for this script? if u do please give me a link
appleday5@yahoo.com
Shrooblord posted at 2008-06-17 15:39:46
Hey hey everyone,
is there a way to KEEP Inventory between rooms, say levels.
Like if you had a really cool weapon, and went to a dungeon to use it, which would mean you`d have to switch rooms, you`d be RATHER disappointed, maybe even rampaged if you figure out the sword isn`t there anymore!
you need to let it save
(on inventory.sav or else)
then on the next level load all wats in your inventory by load the file you saved(inventory.sav or else)
Byr posted at 2009-01-01 02:22:31
thanks, this was a little useful in deciding what item properties to keep track of in my own system.
the downside of this system, is that it only lets you have one inventory - the player's
drezta posted at 2009-01-30 18:19:22
@Byr im sure if you sit back and think for a moment and look carefully and the way global variables are used in game maker you will fine a way of using as many INSTANCES of obj_InventorySystem.inventory[] as you like.
rizki posted at 2009-07-01 03:30:54
> (
error
Shadowrend posted at 2009-07-07 12:22:35
Can you post an example plz
Simon Donkers posted at 2009-07-09 09:06:43
Shadowrend, if you read the tutorial you'll notice that an example is included at the end.
skillsurfer posted at 2009-07-12 06:50:25
I don't get codes! How the crap do i use them? Could you just do this to help me and make me an example.
1: make example
2: Save as, save in My documents as whatever you want to name it(No Excutables)
3:Go to your email
4: in to write my email, skillsurf@gmail.com
5: click attachment, attach your saved file.
6: send it to me
i will not copy your design i will learn off it. If i use your design i will give full credit to you.
Once again my email is
skillsurf@gmail.com
do not send me spam mails...
skillsurfer posted at 2009-07-12 06:54:41
also i am using Gamemaker 7
ThisIsStupid posted at 2009-10-11 23:21:41
why couldn't the MAKERS of game maker just have inventory and everything else on the internet people want, without STUPID coding. It's not that I don't understand coding I just think it's stupid to have to write a hundred freakin lines of text to acheive such a small feat.
me posted at 2009-11-18 23:50:18
@ThisIsStupid
that's because mark overmars made gamemaker as an engine for everybody to be able to make games. it's not specialized for making any specific type of game or genre, but can be used to make nearly any 2d game. if he started specializing it, it would lose it's ability to be effective for other types of games. ultimately it's up to the end user/designer to create their own systems. if you want inventory functions,without writing a bunch of gml's, make an extension for gm. it's why Gm has them, to add functionality.
gameproman posted at 2010-02-12 05:22:34
i don't understand any of this!!!!!
Chiller posted at 2010-03-13 14:21:56
@ThisIsStupid
You'll need to do some coding!
That one guy with GM8 posted at 2010-04-17 05:30:35
Could you update this tutorial for coding usage with GM8? I don't have GM6 or GMD. I can't download them becuase of computer problems with "older" files... :S
F34rlix posted at 2010-06-04 03:05:21
im wondering if u are informed of a type of coding called basic, rather quite simple i was wondering if u could convert this to that kind of code id greatly appreciate it. it shouldnt be that hard i believe game maker uses a similar coding style of blitzmax, a simpler product, thus some rather minor changes would be in order but, im rather illeducated with gm's advantages in code processing. anyway take care, F34rlix.
Stewart posted at 2010-07-27 08:19:18
Cool tutorial, just took some code from it, thanks. The poeple who are commenting here are amazing. It sounds like they didn't even read the tutorial, XD. Or missed the bit at the start" This tutorial assumes a basic knownagle of GML"
chris posted at 2010-09-24 10:08:57
how do i make a inventory with a scroll box?
Ryder358 posted at 2010-10-03 10:04:48
You guys are fuckin twits, you have to use "code" to achieve higher quality games, and usually, you have to learn the code in the first place, you cant just expect that when making a game it'll all fall together on its own occord.
If your so confused by GML use the in built fuckin drag and drop, and if you cant do that, you shouldnt even be trying in the first place.
Suggestion? LEARN THE CODE, or stop bitching about how this somewhat superb example fucked you over because you didnt know how to fuckin use it.