Inventory Setup

hello everyone

lately i have been having a problem with making an inventory in my c++ rpg game

this game uses SFML/opengl(later on) and im wondering how i would make an inventory where i can:

Have multiple bags of a set number of slots(1/2/3/4/5 bags with 9 slots each)
have code that can show the items in an inventory
there is also equipment that you can use(and the equipment has levels that determine its strength/intel/etc)
also you can move the items between bags(an extra thing)

im basically asking what types of functions i should use to make this inventory(arrays,vectors,etc) and it would help if someone could provide a basic setup for this

thanks in advance
I would make a bag class, a slot class, and classes for all your items (I looove classes)

something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class item{
protected:
//blah blah characteristics
public:
//blah blah methods
};

//add subclasses for your items (your "equipment")

class container{
protected:
item *items; //could use vector if desired
public:
// maybe make some setter and getter methods, and probably in your constructor 
// initialize items to the number of items in the container
};

class slot: public container{
//any extra setup or characteristics being a slot may entail
}; 

class bag{
protected:
slot *slots; // same vector usage possibility
public:
// I would set up your constructor to describe the number of slots in the bag and items per slot
// you could also make them dynamic with good programming, and vectors if you want
};


for the display you can make the objects display themselves or you can display them yourself. Just add some graphics variables to each subclass of your item class and have them display their image based on their own image variable (or animation, or whatever)
alright i like that idea(also i love classes too xD)

i will try this out

EDIT: okay ive been looking at this but i dont completely understand it.

would i be able to set this to a certain player?

if so then how would i do such a thing?

im just asking for a bit more detail...

any tutorials that can help me out( ive tried google and all i got was problems that people posted....
Last edited on
hey ive been looking at vectors and other tutorials involving an inventory.

this is what i have thought of


in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<Item*> test;

    Weapon *sword = new Weapon();
    sword->itemID = 199; //this determines the items own identity. i may have done this wrong, but im trying to make the bags slot show this in the console
    test.push_back(sword);

    Consume *potion = new Consume();
    test.push_back(potion);

    Consume *potion2 = new Consume();
    test.push_back(potion2);

    cout << "Element 1: " << test.at(0) << endl; //this shows up as i believe an encrypted code(0x0f633n is an example)im trying to make this show the item's ID(itemID) if i could just know how to call for this information correctly i think i would be okay..
    cout << "Element 2: " << test.at(1) << endl;
    cout << "Element 3: " << test.at(2) << endl;



in the item.hpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Item
{
    virtual string myType() {return "Blank";};
};


class Weapon: public Item
{
    string myType() {return "Weapon";};
    public:
    int itemID;
};

class Consume: public Item
{
    string myType() {return "Consume";};
};



my main problem with this is that i have no idea how to access the information in the item(stats or other characteristics) and show them in the bag

im wondering if im going in the right path...

can somebody help me out with this?
Is it a problem with accessing the information once it's in the vector?

also you should enable PM's

also classes default to private, if you want to use the myType function, make it public.
Last edited on
yes its a problem with accessing the information once its in the vector..

i just dont know how to call for it like for example: if i make sword have the itemID then how do i use it for other purposes(other than put to public, forgot that part) or probably change it?


EDIT: also im wondering how to set that inventory to the player(this game is planned to be online, i know, bad choice) that way its not used by anyone else?
Last edited on
Access members of your vector as you would an array.

VectorName[(int)indexNumber]->method_name();

Again. If you want specific help you should enable Private Messages...
so it would be:

test[0]->itemID;

is that it? also my PMs are enabled
1
2
3
4
class Player{
private:
  inventory meaningful_name;
};
Topic archived. No new replies allowed.