It looks like sky beat me to it. However, I already took the time to type this up so I'm gonna post it anyways ^^
Ok, so the first thing you have to do is create a new code in "Global Code". At the very top, put this:
- Code: Select all
#define NUMITEMS 256
#define NUMSLOTS 12
//Change 256 and 12 to the number of items in your game and the number of slots in your inventory respectively
Then, you'll need to create two structures. One will be used for items, and the other will be used for slots:
- Code: Select all
struct Item
{
//If you want your item to have a name, comment out the next line
//char name[256];
int destroyable;//Set this to 1 if it can be destroyed
int hp;//Hitpoints : This is for if an item is destroyable
int stackable;//Set this to 1 if it can be stacked
//Add any other variables here
};
struct InventorySlot
{
int id;//This is item number that the slot holds
//Make sure to set id to -1 if it doesn't hold anything
int num;//This is how many items are in this slot
};
Make sure that you've read all the comments before proceeding
Ok, now you have to create the spaces of memory that will hold your different items as well as the state of your inventory:
struct Item Items[NUMITEMS];
struct InventorySlot Inventory[NUMSLOTS];
//Do you see how I just used the NUMITEMS and NUMSLOTS?
As you can see, I've made arrays out of the two structures we have. Now if you look back when we created the InventorySlot structure, you'll see an var in it called 'id'. This is used to "point" to one of the items in the "Items" array. This basically just says what type of item the slot is holding. But there's a problem. How do we tell if a slot is empty? If it is set to zero, that will just refer to the first item. To fix this problem, we need to use a number that is less than zero to say that a slot is empty, like -1. So, if a slot's id is set to -1, we know that it's empty
Ok, so now let's create a function that will clear the inventory (set it all to -1):
- Code: Select all
void ClearInventory()
{
int i;
for(i = 0; i < NUMSLOTS; i++)
{
Inventory[i].id = -1;
}
}
So that was pretty simple. Now, let's create a function that will add items to the inventory:
- Code: Select all
void AddItem(int ID)
{
int i;
for(i = 0; i < NUMSLOTS; i++)
{
if(Inventory[i].id == -1)
{
Inventory[i].id = ID;
Inventory[i].num = 1;
break;
}
if(Inventory[i].id == ID && Items[Inventory[i].id].stackable != 0)
{
Inventory[i].num++;
break;
}
}
}
So what this does is it basically takes a number for ID and then searches the inventory for either an empty slot, or one with the same id. If it has the same id, then it checks to see if that item is stackable, if it is, then it increases the number of items in that slot. If not, then it continues searching.
So here's some example code. Put this in a text actor's creation event:
- Code: Select all
ClearInventory();
Items[0].stackable = 0;
Items[1].stackable = 1;
AddItem(0);
AddItem(1);
AddItem(1);
sprintf(text, "%i %i\n%i %i\n", Inventory[0].id, Inventory[0].num, Inventory[1].id, Inventory[1].num
I hope that helps you
And if you need help with anything, or don't understand it, just ask