Vector Inventory problem fixed

This is my implementation of a vector based inventory system. I had a lot of help from AbstractAnon,jlb, && cire. If anyone has any recommendation or any fixes on a bug i dont see please feel free to share. The commands i used are just the commands in the api i am using (Dark gdk). It should be pretty simple to change based on other APIs.
Main Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "DarkGDK.h"
#include "Inventory.h"

void DarkGDK( )
{
	dbSyncOn( );
	dbSyncRate( 60 );

	dbLoadImage("back.jpg",1);
	Inventory ui;
	ui.AddItem("Sword",1,"Weapon",150);
	ui.AddItem("Health Potion",1,"Potion",70);
	


	while( LoopGDK( ) )
	{
		
		ui.Update();
		dbSync( );
	}
        ui.~Inventory();

}

Inventory class
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "darkgdk.h"

#include "Item.h"
#include "WeaponItem.h"
#include "potionItem.h"

#include <vector>
#include <string.h>
#ifndef INV_H
#define INV_H

class Inventory
{
public:
	std::vector<Item*> items;
	Inventory()
	{
		inMenu = false;
		timer = 0;
		s=0;
	}
	~Inventory()
	{
		for(unsigned i = 0; i < items.size(); i++)
		{
			delete items[i];
		}
	}
	void AddItem(std::string name, int amnt, std::string type, int price)
	{
		
			if(type.compare("Weapon")==0) 
			{
				Item *ITEM = new WeaponItem(name,type,amnt,price);
				items.push_back(ITEM);
			}
			if(type.compare("Potion")==0) 
			{
				Item *ITEM = new PotionItem(name,type,amnt,price);
				items.push_back(ITEM);
			}
			
		//and so on for each item 
		
	}

	int Select(int x, int y, std::string name, int amnt, std::string type, int price, unsigned position )
	{
			char *cName = &name[0u];
			dbText(x,y,cName);
			dbSetCursor(x+ 100,y);
			dbPrint(amnt*1.0);
			char *cType = &type[0u];
			dbText(x+150,y,cType);
			dbSetCursor(x+ 300,y);
			dbPrint(price*1.0);
		
		if(position==s)
		{


			if(dbReturnKey()==1  ) return 1;

			dbInk(dbRGB(0,255,0),dbRGB(0,0,0));
			
			dbText(x,y,cName);
			dbSetCursor(x+ 100,y);
			dbPrint(amnt*1.0);
			dbText(x+150,y,cType);
			dbSetCursor(x+ 300,y);
			dbPrint(price*1.0);
			dbInk(dbRGB(255,255,255),dbRGB(0,0,0));

		}
			
			return 0;
	}
	void Update()
	{
		if(timer>0) timer--;
		if(dbDownKey()==1 && timer == 0 )
		{

			if(s<items.size())s++;
			if(s>=items.size()) s = 0;
			timer = 10;
		}
		if(dbUpKey()==1 && timer == 0 )
		{
			
			if(s>=0)s--;
			if(s<0) s = items.size();
			timer = 10;
		}
		
		if(dbKeyState(15)==1 && timer==0 && inMenu == false)
		{
			inMenu=true;
			timer=30;
		}
		if(dbKeyState(15)==1 && timer==0 && inMenu == true)
		{
			inMenu=false;
			timer=30;
			dbCLS();
		}
		
		if(inMenu==true)
		{
			
			for(unsigned i =0; i < items.size(); i++)
			{
				items[i]->Update();
			}
			for(unsigned i =0; i < items.size(); i++)
			{
				if(Select(10,i*32,items[i]->get_Name(),items[i]->get_Amount(),items[i]->get_Type(),items[i]->get_Price(),i)==1 && timer==0)
				{
					items[i]->Use();
					if(items[i]->get_Amount()==0) items.erase(items.begin()+i);
					timer = 80;
					dbCLS();
				}
				
			}
		}
	}



protected:
	bool inMenu;
	int timer;
	int s;


};
#endif 

I am only going to post the WeaponItem class
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
29
30
31
32
33
34
35
36
37
38
39

#ifndef WEAPON_H
#define WEAPON_H
class WeaponItem : public Item
{
public:
	WeaponItem(std::string name, std::string type, int amount, int price)
	{
		Item::name = name;
		Item::type = type;
		Item::amount = amount;
		Item::price = price;
		Item::isEquiped = false;
	}
	~WeaponItem()
	{

	}
	virtual void Use()
	{
		Item::Equip();
	}
	virtual void Update()
	{
		//dbText(10,300,"NOOO");
		if(Item::is_Equiped()==true) dbText(10,300,"Is Equiped");
		if(Item::is_Equiped()==false) dbText(10,300,"not Equiped");
	}



protected:


};



#endif 
Last edited on
Am I blind or do I not see any code ...?
You are blind, look closer...
std::vector<Item*> items;
Why the pointer?
oh @TarikNeaj, he didn't have code before, he edited his post.
@RPA I wrote that before he edited his post and added code, it was sarcasm :p
Last edited on
oooooooh lol, nice abbreviation for me!!
jlb when i dont use a pointer i get a ton of errors. Im not sure what would happen if i did it without the pointer its just the way i know works lol. would you suggest something else?
I recommend staying away from pointers unless it is absolutely necessary. And if it is absolutely necessary then you should consider using smart pointers.

when i dont use a pointer i get a ton of errors.

What errors?

when i use

std::vector<Item> items;

then
1
2
 Item item = new WeaponItem(stuff);
items.push_back(item);


I get
\inventory.h(36) : error C2440: 'initializing' : cannot convert from 'WeaponItem *' to 'Item'
Oh i was not thiking so would this
WeaponItem item(name,type,amount,price,damage,img,health);
be a better idea?
IMO, yes.
So after some tinkering i have found that it is better to use pointers for the class because with out using pointers you cannot use polymorphism, that i am aware of, this disables the ability to use virtual functions for each item derived from the item class instead when you call
1
2
3
4
for(i = 0; i < items.size(); i++)
{
items[i].Use(); // this will call Use() from the item class not based on the type of item 
}

and with polymorphism you can do this
1
2
3
4
for(i = 0; i < items.size(); i++)
{
items[i]->Use(); // this will call Use() from the class that is derived from item with a virtual function 
}
Topic archived. No new replies allowed.