I'm making a adventure game that (of course) has an inventory. I have solved this problem, but I wonder if there is a better to way to solve it. I have used arrays where i have stored the different locations of the items.
After that I have made a loop where it goes through all the arrays and couts the items that have the same location as the current room.
iloca = sword.ilocation;
iloca = hammer.ilocation;
Is there an another way to make it go through all the items and cout those which are in the room? I have only two items now, but when I'm going to make some more items, it would get a bit boring to define all the arrays and there would be lots of code.
ok umm i think you are looking for a vector as your solution. just using arrays doesnt seem to fit your situation very well. i assume that in your game you will want to add objects to your inventory.
this means you can either
1) use a vector of pointers that point to inventory objects
2) use a vector of inventory objects
and whenever you get a new inventory object you call either
1) inventory.push_back(new item);
if you don't really know what I am talking about please tell me and I will try to explain it more, I have been doing work with vectors lately so it is fresh in my head.
Sorry for not having replied to your post Shadow12345. I understood a bit of that what you told and I have now read about vectors. I fixed a another problem with them but I don't understand how vectors could fix the big problem. I have created the weapons with a weapons class and there I have made a constructor which I use to create all the different weapons.
And then I have made the arrays so I could go through every weapons item_location and name.
weapons(char* wname, double wweight, int w_damage, int i_pos, char* i_location)
name = wname;
weight = wweight;
weapon_damage = w_damage;
item_position = i_pos;
item_location = i_location;
//name, weight, wdamage, position, location
weapons sword ("sword", 0.9, 4, 1,"");
weapons hammer ("hammer", 1.5, 6, 1,"");
And here is the code which looks if the weapon is in the room and couts it if it is.
iloca = sword.item_location;
iloca = hammer.item_location;
iname = sword.name;
iname = hammer.name;
Is there a better way to do this?
for (int y = 1; y < 3; y++)
if (iloca[y] == room.strCurrent_room)
cout << iname[y] << endl;
Since you've already created a weapon class, there is no point in storing it's data in other variables. Either make an array (simple, but limited size) or a linked list (harder, but theoretically unlimited size).
Linked list node:
#define NrOfWeapons 12
cout << MyWeapons.Name;
typedef struct _NODE
You have to excuse me because I don't know how to use those things :( I only know that the one with the array needs a default constructor.
Isn't the way you have it set up now sufficient?
I've done quite a bit with parsers, but with QuickBASIC. I typically use a loop that checks for the item's presence in the room and if so display it.
for (int x=501; x<1000; x++)
if (item[x].location == playerlocation)
That method I would think would be good enough. When you use position to say if its in somebody's inventory, I use location for that as well (similar to Adventure Game Toolkit).
0 for NOTHING (place for items not in the game anymore)
1-500 for ROOMS
501-1000 for ITEMS
1001-1500 for ACTORS (player is first actor)
Much easier to implement with a single location statement.