# Gw Temp

## Tutorial - 'Item Systems' by videogamerx2

An item about Gamemaker posted on

### Blurb

A tutorial on how to make an item system in GameMaker.

### Body

*GAMEMAKER Items System Tutorial*

-By VideoGamerX2

Overview:

This tutorial will instruct you in-depth with examples on how to create an item system for an rpg made in Game Maker.

This tutorial will be written all in GML (Game Maker Language), in a format easy enough for someone who has just started GM to understand.

The items:

This system can have any number of items, but the calue must be set in the beginning of the game. I will be using a system that supports up to 99 items.

Bear in mind as the value is raised/lowered processing time and complexity acts accordingly (though nothing severe).

The method:

This system uses two arrays, an array is a a list of values, and is also known by the term "matrix." It is basically a good way to store large quantities of

numbers in an easilly addressable fashion.

The first array is a 2x99 array. This means it has 99 columns and 2 rows. Each column represents a certain item in its corresponding slot in the player's

inventory. The first row holds the value for item id, and the second row holds the quantity of that item.

The second array is a list of corresponding strings for item ids. Note that this should be a total number greater than the first array, or equal if you want

the player to be able to hold all the items at once. Having a larger number of items than slots results in a limited inventory, which can be desirable or

undesireable.

So if the first array is 2x99, you could make the second array also 2x99. There would be 99 pairs of entries, one for item name and one for item

description.

These would be set at the start of the game. I will work with a smaller itemset for purposes of this example, with a max of 10 items, but your game will

likely have more.

Creation event:

for(i=0;i<=10;i+=1)
{itemarray1[i,1]=-1
itemarray1[i,2]=-1}

Now we have set up the player's inventory. This sets all item slots to empty, with a value of -1 (which will be interpreted as an empty slot).

Next we must set up the item name list:

//0
itemarray[0,0]="Potion"
itemarray[0,1]="Gives Back 10 HP"
//1
itemarray[1,0]="Hi-Potion"
itemarray[1,1]="Gives Back 100 HP"
//2
itemarray[2,0]="Mega-Potion"
itemarray[2,1]="Gives Back 1000 HP"
//3
itemarray[3,0]="Max-Potion"
itemarray[3,1]="Fully Restores HP"
//4
itemarray[4,0]="Ether"
itemarray[4,1]="Restores 1 MP"
//5
itemarray[5,0]="Super-Ether"
itemarray[5,1]="Restores 10 MP"
//6
itemarray[6,0]="Max-Ether"
itemarray[6,1]="Fully Restores MP"
//7
itemarray[7,0]="Elixer"
itemarray[7,1]="Restores 50% HP and MP"
//8
itemarray[8,0]="MegaElixer"
itemarray[8,1]="Fully Restores MP to Entire Party"
//9
itemarray[9,0]="Strength Medicine"
itemarray[9,1]="Gain Strength +1"

As you can see, this must be adapted on a per-game basis, and for larger games, you may want to do this using a text file and a script. Note that this

method allows for in-game modification of item names, so this could be adapted to many things, for example, a key item that is used once could be renamed to

a new one, or you do stuff like add the name of a player inputted by the user, like "Alex's Personal Sheild"

From this point, we have set up an item database, and now we need to manipulate it, namely we must find a way to add/change items, display them, and sort

them.

Say the player has found a potion item. First thing to do is check if there are any potions already, and if so, add the potions to that, otherwise we must

add them to the first available empty slot.

I would suggest creating a message box script, and displaying a message like "Found" and then itemarray[0,0] using two draw_text() functions to the effect of

"You have found 10 Potions" (You will need to have a variable for itemsfound, the id of the found item and if it is >1, add the "s" to the end of the word to

indicate pluralism)

Next step is to do this:

a=0
for(i=0;i<=10;i+=1)
{
if itemarray1[i,1]=idfound
{itemarray1[i,2]+=itemsfound;a=1;break}
}
if !a{for (i=0,i<=10;,i+=1){if itemarray1[i,1]=-1{itemarray1[i,1]=idfound;itemarray1[i,2]=itemsfound;break}}}

We have now scanned the database and added in the item.

To draw the items:

Do something like this:

for(i=0;i<=10;i+=10))
{
if itemarray1[1,0]!=-1{
draw_text(5,i,itemarray[i,0])}
}
screen_refresh()
keyboard_wait()

This will draw all the current items and wait for keyboard input,how to do this depends on the desired aesthetics, I would suggest creating a cursor that

responds to commands and holds the current slot, which displays the corresponding description for that slot in a box at the top of the screen.

To switch the items, you would just switch the values for the two slots requested to be switched.

I hope this helps people out there to understand item systems in GM, and will lead them to create better rpgs on the maker than have been seen in the amateur

Game Making community, as you can probably see since this item system is built from scratch the possibilities are infinite, while in rm2k/3, they are

limited.

PM any questions to videogamerx2 on the whahay.net Gaming World forums.