Creating a inventory system!

Okay so let me first get started on what I am creating, it is a text based game like Zork except I have made up the story line myself. I plan to make is so that you can find items and use them on other objects in the game. The problem is I don't know where to begin. An example of this would be... the player find a key and has to use it on a door later in the game. How would I make it so that his inventory is constantly updating? So that if he goes back to the same point in the story he wont lose the "key" that he had gotten?

Let me explain it a little more... the character spawns... he has the option of going North, East, South or West. He cant go North and cant go South, if he goes west he finds a locked door. If he goes east he finds a abandoned building and finds a key. (blah blah more interesting story stuff) then he goes back to the old part where he started and heads west and unlocks the door. The inventory needs to be updated so that when he finds the key it appears in his inventory and that when he uses the key it disappears.

I know this is very confusing and all, so I would like to say a massive thanks in advance!

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iostream.h>
#include <memory>
#include <istream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{

int choice, choice2, choice3;
for(;;)

    {
    cout << "--------------------------------------------------------------------------------\n";
    cout << "Welcome to Kroz!\n";
    cout << "Please report and bugs to Jon_p3rron@hotmail.com. Thanks :)\n";
    cout << "IMPORTANT: There is currently a glitch when having a choice option. If you enter and letters the code will begin to spam uncontrolably. If this haapens you will have to restart the game. Thanks for understanding! :)\n\n"; 
    system("pause");
    cout << "Krow v1.0 08/09/2011 Alpha Version (incomplete)\n\n";
    cout << "--------------------------------------------------------------------------------\n";
    system("pause");
    cout <<"\n\n";
    cout << "Would you like to play?\n";
    cout << "1 - Yes!\n";
    cout << "2 - No.\n";
    
    
    string name;
    cout << "Choice:";
    cin>> choice;
    switch (choice)
    
                {
           case 1:
                
                cout << "Well good luck and have fun!\n";                
                cout << "Please enter your name:";
                cin >> name; 
                cout << "\n\n";
                cout << "Hello, " << name << " and welcome to Kroz! I am your guide through this game and can be as a guide for new players and for old players. But first of as your guide I have to state what version of the game your on: Kroz 1.0 08/09/2011. This game has frequent updates and should be checked back on every once and a while for a update! :) But other than that I say... Good luck!\n\n";
                cout << "Your in the middle of a street. Destruction all around you, deserted cars and stores, you find your self in a small town, your not really sure where. All the buidlings around you have shattered windows and rusted doors. It appears there hasn't been anyone around for a long time.\n\n";
                system("pause");
                cout << "\n\n";
                cout << "So when you get a small description like this you get 4 choices: go North, East, South or West. You can try each choice if you want, you can always go back the direction you came unless there is something blocking your route back.\n\n";
                loop3:
                loop2:
                cout << "1-North\n";
                cout << "2-East\n";
                cout << "3-South\n";
                cout << "4-West\n";
                cout << "Choice:";
                cin>> choice2;
                switch (choice2)
                                {
                                case 1:
                                     cout << "Hmm... It appears you cannot go this way.\n\n";
                                     goto loop2;
                                     break;
                                case 2:
                                     cout << "There is an abandined store. Would you like to explore it?\n\n";
                                     cout << "1-Yes!\n";
                                     cout << "2-No.\n";
                                     cout << "Choice:";   
                                     cin>> choice3;
                                     switch (choice3)  
                                            {
                                            case 1:
                                                 cout << "Most of the shelves in the store are empty. You check behind the cashiers counter and oddly find something...\n";
                                                 system("pause");
                                                 cout << "You found a knife!\n";
                                                 cout << "You head back to the street.\n\n";
                                                 goto loop3;
                                                 break;
                                            case 2:
                                                 cout << "You decide not to go in as it is to frightening.\n\n";
                                                 goto loop3;
                                                 break;
                                            default:
                                                    cout << "I'm sorry, that option isn't available.\n\n";
                                                    goto loop3;
                                                    }
                                     break;
                                case 3:
                                     cout << "You head farther down the road.\n\n";
                                     break;
                                case 4:
                                     cout << "There is an abandoned house. Would you like to explore it?\n\n";
                                     break;
                                default:
                                        cout << "I'm sorry, that option isn't available.\n\n";
                                        goto loop2;
                                }              
                break;  
                
           case 2:
                cout << "Aww... Well maybe some other time?\n\n\n";
                system("pause");
                return 0;
                break;
           default:
                cout << "\n\nI'm sorry, that option isn't available.\n";
               
                }                    
    }  
         system("pause");
         return 0;
}
Well, normally I would recommend and Item class and an Inventory class to manage things such as those, however, based on your code, a much simpler (albeit less flexible) would be to use bool flags.

1
2
3
4
bool hasKey = false;

if (playerGetsKey())
hasKey = true;


You could set it to default values in the beginnings of your "loops" to get the respawn effect you described.
Thanks, I'm very new to C++ if you couldn't already tell.

Well I don't want to object to "respawn". I want the character to have the option of using the key. Once they have used it "poof" its gone like it was never in the code.

EDIT: I'm having trouble inserting your code. A message appears and says that the code
'playerGetsKey is undeclared'
Last edited on
That was just there to show you where/how hasKey is set to true. You would probably set this inside a case statement from a choice that gives the player the key, or lets them use it.
Topic archived. No new replies allowed.