Text-based RPG: Inventory Load Issue

Aaand I'm back again with another question. I had a save/load system implemented in my text-based RPG, but I didn't have any inventory data stored in it previously (seeing that I didn't have an inventory system implemented until a few moments ago). I figured out how to save all of my inventory data to a file (which I will show in the code below), but I'm having issues figuring out how to write the loadGame function. The problem I'm having is that I don't know how to sort the data from the .txt file I created with the SaveGame function back into the game as it was before it was saved (i.e. "Faded Helmet" loads as an armor piece w/ defense, "Rusty Dagger" loads as a weapon with low/high damage, etc). The code I'm pasting below was copied into a new project that has none of the RPG's code -- just the inventory system (so I can figure out this save/load issue). Any help with this would be greatly appreciated!

EDIT: And yes, I'm aware that my Item.h file is incredibly bulky. Once I figure this out I'll split it accordingly into an Item.cpp file. I'm more concerned with getting this to work properly at the moment. ;)

EDIT #2: On a side note...is there a more efficient way to implement the inventory vector than having to declare it as a global? That's the only way I can figure out how to do it where the same inventory vector data is kept throughout the program.

Global.h
1
2
3
4
5
6
7
8
9
#ifndef GLOBAL_H
#define GLOBAL_H

#include "Item.h"
#include <vector>

vector<BaseItem *> inventory;

#endif 


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
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
#ifndef ITEM_H
#define ITEM_H

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

class BaseItem {
private:
	string name;
public:
	enum ItemType { 
		IT_NONE		 = 0,
		IT_INVENTORY     = 1,
		IT_WEAPON	 = 2,
		IT_ARMOR	 = 3
	};

	BaseItem(const string &n) : name(n) { }
	const string &getName() const { return name; }
	virtual bool isItemType(ItemType t) const = 0; 
};

class InventoryItem : public BaseItem {
protected:
	int strRequired;
	const int weight;
public:
	InventoryItem(const string &n, int w) : BaseItem(n), weight(w) { }
	virtual bool isItemType(ItemType t) const { return ((t == ItemType::IT_INVENTORY) != 0); }
	int getWeight() const { return weight; }
};


class Weapon : public BaseItem {
protected:
	int damage;
	int strRequired;
public:
	Weapon(const string &n, int d, int str) : 
		BaseItem(n), damage(d), strRequired(str) { }

	Weapon(const string &n, int d) : 
		BaseItem(n), damage(d), strRequired(0) { }

	virtual bool isItemType(ItemType t) const { return ((t == ItemType::IT_WEAPON) != 0); }
	int getDamage() const { return damage; }
	int getStr() const { return strRequired; }
};

class Armor : public BaseItem
{
protected:
	int defense;
	int strRequired;
public:
	Armor(const string &n, int d, int str) :
		BaseItem(n), defense(d), strRequired(str) { }

	Armor(const string &n, int d) :
		BaseItem(n), defense(d), strRequired(0) { }

	virtual bool isItemType(ItemType t) const { return ((t == ItemType::IT_ARMOR) != 0); }
	int getDefense() const { return defense; }
	int getStr() const { return strRequired; }
};

#endif 


main.cpp
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
#include "stdafx.h"
#include "Item.h"
#include "Global.h"
#include <iostream>
#include <fstream>

using namespace std;

void saveGame();

int main() {	
	inventory.push_back(new InventoryItem("Blue Mushroom", 1));
	inventory.push_back(new InventoryItem("Worn Lantern", 5));
	inventory.push_back(new Weapon("Rusty Dagger", 4, 2));
	inventory.push_back(new Weapon("Knotted Staff", 7, 3));
	inventory.push_back(new InventoryItem("Torn Rope", 7));
	inventory.push_back(new Weapon("Rusty Sword", 12, 3));
	inventory.push_back(new Armor("Worn Glove", 5, 3));
	inventory.push_back(new Armor("Faded Helmet", 4));

	saveGame();
	cin.get();
	return 0;
}

void saveGame()
{
	std::ofstream fout;
	fout.open ( "SaveData.txt" );
	
	for(int i=0; i<inventory.size(); i++) 
	{
		BaseItem *item = inventory[i];
		fout << item->getName();
		fout << endl;
		
		if (item->isItemType(BaseItem::IT_INVENTORY)) 
		{
			InventoryItem *inv = (InventoryItem *)item;
			fout << inv->getWeight() << endl;
		}
		
		if (item->isItemType(BaseItem::IT_WEAPON)) 
		{ 
			Weapon *wep = (Weapon *)item;
			fout << wep->getDamage() << endl;
			fout << wep->getStr() << endl;
		}

		if (item->isItemType(BaseItem::IT_ARMOR)) 
		{
			Armor *arm = (Armor *)item;
			fout << arm->getDefense() << endl;
			fout << arm->getStr() << endl;
		}	
	}

	fout.close();
}


Last edited on
Topic archived. No new replies allowed.