Implementing Inventory System for RPG

I am wondering how to make an inventory system for a text based RPG. Right now I have an abstract Item class with Armour and Weapon derived from it. I tried to do Item inventory[xyz], but that will only hold the variables from Item, not ones from Armour and Weapon. And I don't want to have a certain number of slots for each type of item, so any ideas?
dynamic allocation, but I think you're going about it wrong. hard to say from what you've said.

Share with me your code?
I can put the whole project into a zip file if needed, but here is the part for items:

Snip of another file
1
2
3
//...
enum stats {LVL, HP, MP, MPMAX, HPMAX, STR, MAG, SKL, SPD, DEF, RES, LCK, END};
//... 


Item.h
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
#ifndef _ITEM_H_
#define _ITEM_H_

#include "Include.h"

class Item {
private:
	string name;
	int value;
public:
	Item();
	void setName(string);
	string getName();
	void setValue(int);
	int getValue();
};

Item::Item() {
	name = "None";
	value = -1;
}

void Item::setName(string Name) {
	name = Name;
}

string Item::getName() {
	return name;
}

void Item::setValue(int amount) {
	value += amount;
}

int Item::getValue() {
	return value;
}


#endif 


Armor.h
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
#ifndef _ARMOR_H_
#define _ARMOR_H_

#include "Include.h"

enum armorType {
	HEAD, BODY, ARMS, LEGS, HANDS, FEET, RING1, RING2, RING3, RING4, NECK, EYE, SHIELD, BELT, NOARMOR
};

class Armor : public Item {
private:
	armorType type;
	int hpBonus;
	int mpBonus;
	int strBonus;
	int magBonus;
	int sklBonus;
	int spdBonus;
	int defBonus;
	int resBonus;
	int lckBonus;
public:
	Armor();
	void setBonus(stats, int); 
	int getBonus(stats);
	void setArmorType(armorType);
	armorType getArmorType();
};

Armor::Armor() {
	type = NOARMOR;
	hpBonus = 0;
	mpBonus = 0;
	strBonus = 0;
	magBonus = 0;
	sklBonus = 0;
	spdBonus = 0;
	defBonus = 0;
	resBonus = 0;
	lckBonus = 0;
}

void Armor::setBonus(stats choice, int value) {
	switch(choice) {
	case HP:
		hpBonus += value;
		break;
	case MP:
		mpBonus += value;
		break;
	case STR:
		strBonus += value;
		break;
	case MAG:
		magBonus += value;
		break;
	case SKL:
		sklBonus += value;
		break;
	case SPD:
		spdBonus += value;
		break;
	case DEF:
		defBonus += value;
		break;
	case RES:
		resBonus += value;
		break;
	case LCK:
		lckBonus += value;
		break;
	default:
		//If no match by now
		logger("Classes.h", __LINE__, "ERROR! Invalid stat bonus to set");
		break;
	}
}

int Armor::getBonus(stats choice) {
	switch(choice) {
	case HP:
		return hpBonus;
	case MP:
		return mpBonus;
	case STR:
		return strBonus;		
	case MAG:
		return magBonus;
	case SKL:
		return sklBonus;
	case SPD:
		return spdBonus;
	case DEF:
		return defBonus;
	case RES:
		return resBonus;
	case LCK:
		return lckBonus;
	default:
		//If no match by now
		logger("Classes.h", __LINE__, "ERROR! Invalid stat bonus to get");
		break;
	}
	return 0;
}

void Armor::setArmorType(armorType Type) {
	type = Type;
}

armorType Armor::getArmorType() {
	return type;
}


#endif 


Weapon.h
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
#ifndef _WEAPON_H_
#define _WEAPON_H_

#include "Include.h"

enum weaponType {
	PRIMARY, SECONDARY, DUALWIELD, TWOHAND, NOWEAPON
};

class Weapon : public Item {
private:
	weaponType type;
	int hpBonus;
	int mpBonus;
	int strBonus;
	int magBonus;
	int sklBonus;
	int spdBonus;
	int defBonus;
	int resBonus;
	int lckBonus;
public:
	Weapon();
	void setBonus(stats, int); 
	int getBonus(stats);
	void setWeaponType(weaponType);
	weaponType getWeaponType();
};

Weapon::Weapon() {
	type = NOWEAPON;
	hpBonus = 0;
	mpBonus = 0;
	strBonus = 0;
	magBonus = 0;
	sklBonus = 0;
	spdBonus = 0;
	defBonus = 0;
	resBonus = 0;
	lckBonus = 0;
}

void Weapon::setBonus(stats choice, int value) {
	switch(choice) {
	case HP:
		hpBonus += value;
		break;
	case MP:
		mpBonus += value;
		break;
	case STR:
		strBonus += value;
		break;
	case MAG:
		magBonus += value;
		break;
	case SKL:
		sklBonus += value;
		break;
	case SPD:
		spdBonus += value;
		break;
	case DEF:
		defBonus += value;
		break;
	case RES:
		resBonus += value;
		break;
	case LCK:
		lckBonus += value;
		break;
	default:
		//If no match by now
		logger("Classes.h", __LINE__, "ERROR! Invalid stat bonus to set");
		break;
	}
}

int Weapon::getBonus(stats choice) {
	switch(choice) {
	case HP:
		return hpBonus;
	case MP:
		return mpBonus;
	case STR:
		return strBonus;		
	case MAG:
		return magBonus;
	case SKL:
		return sklBonus;
	case SPD:
		return spdBonus;
	case DEF:
		return defBonus;
	case RES:
		return resBonus;
	case LCK:
		return lckBonus;
	default:
		//If no match by now
		logger("Classes.h", __LINE__, "ERROR! Invalid stat bonus to get");
		break;
	}
	return 0;
}

void Weapon::setWeaponType(weaponType Type) {
	type = Type;
}

weaponType Weapon::getWeaponType() {
	return type;
}


#endif 


I don't think dynamic allocation would help, because the problem I am having is that when I do Item inventory[100] each object in inventory can only hold the name and value of a piece of armor or a weapon, and nothing else. I know I could abstract those ints to the base class and combine the armor and weapon type enums into a single itemType enum, (and I will get to that eventually), but it's only a temporary solution, because I will be adding more things that differentiate armor and weapons later.
Alright, I misread your post. Yea dynamic allocation isn't the key. The simple solution is to have a a structure that holds these classes. it would look something like this.
1
2
3
4
5
     typedef struct pInventory{
                 Item       *pItem;
                 Weapon        *pWeapon;
                 Armor     *pArmor;                
     }INVENTORY;


and then create an object of that instead.
Last edited on
Topic archived. No new replies allowed.