C++ RPG Tutorial‎ > ‎

Items And Collectables

Now that our player can create a new game, customise their character and move around some areas, it's time to add the next big thing: items!
Incorporating items, collectables pick-ups or whatever else they are referred to can be confusing for some people. I myself have dabbled in several ways of implementing items that can be easily stored and accessed, from simple variables for each item to large arrays for every type of item imaginable. Here we shall keep it simple and use just one array for every type of item. This method does have some disadvantages, however it is very easy to utilise, and most importantly, easy to expand.
 
Now then, let's get to work.
 
The Code
 
First we need to create the array that will store the information for each item the player has. For this we will use a 2d array of integers, sized to be several columns wide (exactly how many depends on your game) and the amount of items the player can carry tall.
 
First, create a variable that will store the number of maximum items the player can have. It will of course be a global variable. The variable I shall use will be a constant, but I have encountered several games where the player can store variable amounts of items, and so in that case it would not be.
 
const int MAX_ITEMS = 100; // I think a maximum of 100 items should suffice
 
Now this is done, we can create the array. As said before, the exact width depends on how many different qualities each item can have. (Bear in mind that we are using one array for every type of item) I shall use a value of 10, as this will definitely be enough for a simple game. It can always be modified if need be, of course. Since this will be accessed from anywhere, it shall be global too.
 
int playerItems[MAX_ITEMS][10]; // Big enough for every item the player can hold
 
You may be thinking: "Why are we using an array of integers? Surely we need to store the item name too?".
If you are, you are correct in that we need to store the names, but for that we will create a new array made of strings, which we shall fill with the appropriate names and link to playerItems.
 
To do that, we shall fill this new array with every name needed and reference to it by line using a number in the first column of playerItems, namely playerItems[0].
This allows us to easily and efficiently store both the name and the item information.
 
Now that's out of the way, we can put it into our code.
 
At the moment, all we need to do is declare it, as we haven't made any items yet.
However, as we are using an array of strings, we need to use string.h. Put this line new line at the top of the file, in the #includes
 
#include<string>
 
Now we can safely declare the array, and not be shouted at by the compiler because we declared a variabke of a non-existent type.
 
As you hopefully already know, a string is just a group of characters. This means that instead of just storing 'a' we can store 'armadillo' in a single variable. Very useful. Of course, as with almost every method, there are different ways of doing it, but I prefer this one as it provides a lot of functionality in an easy to use system.
 
Alright then, now let's create our array of names. Since we have no items, a simple declaration of a global variable will do.
 
string itemNames[] // We shall expand it later, so give it an undefined size
 
Excellent, now we can store all of the relevant information for the items.
 
Since we've done that, now we can let the player find some items!
We'll start off by giving them some beginning items when they start a new game, and then we'll add items that can be picked up.
 
First though, we need to be able to keep track of how many items the player actually has, otherwise me might overwrite another item when we give them a new one.
To do this, we'll just use a simple integer variable, as it obviously doesn'y need to be any bigger than the maximum amount of items the player can hold at any one time. (If the player can only hold 50 items, they are never going to be holding 51!)
 
Declare this global variable. Make sure you assign it 0, or it won't work later on.
 
short playerItemCount = 0;
 
In my case I have made it a short because the player definitely won't have more than 65 535 items! (The size of a short int)
 
Great, now let's add the items themselves.
When the player starts a new game, as well as moving them to the correct area, we shall also give them a couple of necessary items, such as clothes, and maybe a weapon. (If you're feeling generous)
 
To do this we need to fill in the correct information relating to the item in the next availabe space in playerItems. Since playerItemCount stores how many items the player has, adding one to it will bring us to the next available space. Simple!
However, since arrays in C++ are zero based, the first space in the array is accessed by playerItems[0]. Since this is where we want to put the information when the player doesn't have any items we don't actaully need to do any addition at all! Even better!
 
Now we know what to do, let's do it. This code will be placed just below where you first moved the player in newGameFunc(), but don't write it yet, it needs some explaining first.
 
playerItems[playerItemCount][0] = 0;
playerItems[playerItemCount[1] = 1;
playerItems[playerItemCount[2] = 5;
playerItemCount++; // The player has recieved an item, so tell the program to remember that
 
This wil be easier to explain and understand with pictures, so here you go:
 
 
In my example, the item's name will be equal to whatever name is in itemNames[0].
The item 'type' is simply a number that we will use later to help the program identify what the item is, e.g. a weapon, potion or a key.
Becuase my array has a size of 10, each item can have up to 8 variables unique to it (size - 2 for the name and type boxes). What these variables mean is dependent of the item's type.
 
In this case the type is 1, which for me means 'weapon', and becuase its type is 'weapon' the value of 5 means its 'attack strength'.
If a type of one meant 'item of clothing' the 5 probably wouldn't translate as '5 attack'.
 
The last line adds one to (increments) playerItemCount. If we didn't do this after every item, then the next item would just overwrite the first!
 
Now you know how it works, you can add the lines of code you need for each item. Remember, don't add more variables than you can store in your array!
 
Once you've done that, we will give the items some names. 
 
 
Comments