Inventory problem

This is a discussion on Inventory problem within the C++ Programming forums, part of the General Programming Boards category; I'm making a adventure game that (of course) has an inventory. I have solved this problem, but I wonder if ...

  1. #1
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18

    Inventory problem

    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.
    Code:
    iloca[1] = sword.ilocation;
    iloca[2] = hammer.ilocation;
    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.
    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.

  2. #2
    Shadow12345
    Guest
    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);
    or
    2) inventory.resize(totalnumberitems);

    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.

  3. #3
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    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.
    Code:
    class weapons
    	{
    	public:
    
    		char* item_location;
    		int item_position;
    		double weight;
    		char* name;
    		
    		int weapon_damage;
    
    		//Weapons constructor
    		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 then I have made the arrays so I could go through every weapons item_location and name.
    Code:
    iloca[1] = sword.item_location;
    iloca[2] = hammer.item_location;
    
    iname[1] = sword.name;
    iname[2] = hammer.name;
    And here is the code which looks if the weapon is in the room and couts it if it is.
    Code:
    for (int y = 1; y < 3; y++)
    {
    	if (iloca[y] == room.strCurrent_room)
    		cout << iname[y] << endl;
    }
    Is there a better way to do this?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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).

    Array:
    Code:
    #define NrOfWeapons 12
    
    WEAPONS MyWeapons[NrOfWeapons];
    
    cout << MyWeapons[3].Name;
    Linked list node:
    Code:
    typedef struct _NODE
    {
       WEAPONS MyWeapon;
       _NODE* NextNode;
    }NODE;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    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.

  6. #6
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    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)
    {
    printf(item[x].name);
    }
    }

    -----

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21