I'm having a bit of trouble with this. I'm creating a cute little text adventure game for practice, so I can learn the basics of coding in C++.
I decided to use a vector for player inventory. It works great. It's a vector of objects, and I can put in or take out objects, or create a list of them for the "inventory" command, and it all works great.
But I tried to do the same thing for my rooms, so they could have their own "inventories", and it didn't work. The only way I can think of that these two vectors are different is that one is contained in an object and one isn't.
Specifically, I tossed this line into my Global Variables section:
Code:
vector<Object> player_inventory;
"Object" is a class I've defined for objects in the game environment. It contains some basic data, like their name, weight, and description.
When I run my command to list the player's inventory, I call this function:
Code:
int Inventory_List()
{
cout << "\nYou are carrying:\n";
if (player_inventory.size() == 0) {cout << "\nNothing\n";}; //checks whether there are no objects in the player's inventory
if (player_inventory.size() > 0)
{
int n;
n = player_inventory.size();
do
{
n--; // this is necessary because the elements in the vector are numbered starting with 0
cout << endl << player_inventory[n].title;
} while (n != 0);
cout << endl << endl;
};
}
And it works fine.
So, since "cout << player_inventory[n].title" successfully outputted the title of an object in the player_inventory vector, I tried to use identical code to output the title of objects in the room's inventory:
Code:
cout << Room_001.room_contents[n].title
But this code returns nothing. It's basically identical to my player inventory code, but I've changed the names. Everything is declared and I get no compilation errors. I've added objects into the room_contents vector. If I add those same objects to the player inventory, they'll list just fine.
So the only difference I can think of is that my player_inventory vector is a global vector, but my room_contents vector is inside a "Room" object. Should that really make a difference?
I'm sorry if my code snippits are a bit disjointed, but I think you'll get the jist.