Simple Inventory.

Don't know how to make something and can't find a tutorial? Ask for one here.

Simple Inventory.

Postby bubblemasterx » Sat Apr 21, 2012 12:59 pm

So I was simply wondering how to make a very basic RPG style inventory. What I want it to do is when you press a button (Probably 'B') it opens up. It has 12 slots. When you pick up an item, it goes to the first slot. If the first slots taken, to the second. I only want certain items to be able to stack. Then, when you right click on one of the items, your character uses it. And when you hit 'B' again, it closes. If this doesn't make sense, let me know
Thanks in advance.
bubblemasterx
 
Posts: 3
Joined: Sun Feb 05, 2012 3:51 am
Score: 0 Give a positive score

Re: Simple Inventory.

Postby skydereign » Sat Apr 21, 2012 2:09 pm

You can search around. There are several demos on inventories. You might try using Bee-Ant's from here. http://game-editor.com/forum/viewtopic.php?f=5&t=6854&p=61023#p61023
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Simple Inventory.

Postby SuperSonic » Sat Apr 21, 2012 3:10 pm

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 :wink:

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 :D

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 :P

And if you need help with anything, or don't understand it, just ask :wink:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Simple Inventory.

Postby Game A Gogo » Tue Apr 24, 2012 11:25 am

I'm just reading your code quickly so I may be wrong, but wouldn't it be easier for the inventory to be an array of Item structs?

so instead of
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
};


you'd have

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
};
Item inventory[NUMSLOTS];//Array of items


But if you want the items to be stackable, maybe this is better:

add a new define:
Code: Select all
#define MAXSTACK 64


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 invSlot
{
    Item stack[MAXSTACK];//One stack of item
};
invSlot inventory[NUMSLOTS];//Array of items


or you could have
Code: Select all
Item inventory[NUMSLOTS][MAXSTACK];
instead of two structs

again, sorry if I'm wrong xD I just woke up and I quickly read this in college
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple Inventory.

Postby bubblemasterx » Tue Apr 24, 2012 11:41 pm

Alright, I'm very new to coding, so I'm sorry to say neither of these makes any sense to me. It'd also be helpful if you stated which actors and events these scripts would go under. Thanks.
bubblemasterx
 
Posts: 3
Joined: Sun Feb 05, 2012 3:51 am
Score: 0 Give a positive score

Re: Simple Inventory.

Postby SuperSonic » Wed Apr 25, 2012 6:45 pm

Game A Gogo wrote:I'm just reading your code quickly so I may be wrong, but wouldn't it be easier for the inventory to be an array of Item structs?
I did make an array. However, the array is used to define all of the different items in the game (swords, shields etc). The inventory array is used to tell which of those items is in each slot and how many :)

bubblemasterx wrote:Alright, I'm very new to coding, so I'm sorry to say neither of these makes any sense to me. It'd also be helpful if you stated which actors and events these scripts would go under. Thanks.
Oh, I apologize. I assumed you were more advanced. In that case, it would be best to wait. Inventories are very complicated, and unfortunately, I don't know of a newbie friendly way to explain them :?
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score


Return to Tutorial Requests

Who is online

Users browsing this forum: No registered users and 1 guest