1
\$\begingroup\$

I have a question regarding declaring my inventory. I made it a string named inventory, with a constant number as its max value. The thing is, I want the user to use an item if he / she gains an item. The problem is, I do not know what syntax should I use to determine if the user has an item and use that item. Here's my code I just started:

so declaring the inventory:

    const int MAX_ITEMS = 15;
    string game_inventory [MAX_ITEMS];
    int itemnum = 0;

I have some items like potion, antidote, gems and others. I use the:

game_inventory[itemnum++] = "Potion"

to place items in my inventory. If I want to use the potion, IF I HAVE one, how can i make a function to check whether I have a potion or anything and use it?

\$\endgroup\$
2
\$\begingroup\$

The simplest "check inventory array" method in C++ looks like this:

#include <vector>
using namespace std;

template<typename array_type, typename item_type>
    bool is_in_inventory(array_type inv, item_type item)
{
        /*
         * Use the following instead ot "auto iter" if your C++ variant is older:
         * array_type::iterator iter
         */
        for ( auto iter = inv.begin(); iter != inv.end(); ++iter )
        {
            if( *iter == item ) { return true; }
        }
        return false;
    }
}

This assumes game_inventory is a standard C++ container, so something like std::array<std::string, MAX_ITEMS> or std::list<std::string>; but basically anything which implements the iterators properly will work.

Replace auto with type of game_inventory`::iterator if you're dealing with older variants of C++, as per the comment in the code.

You can also use the functions in <algorithm> for slightly less verbose (but not really quicker) ways to do several such operations. For example:

#include <vector>
#include <algorithm>
using namespace std;

template<typename array_type, typename item_type>
    bool is_in_inventory(array_type inv, item_type item)
{
    return find(inv.begin(), inv.end(), item) != inv.end();
}

Behind the scenes, this does exactly the same code as the example above.


Side note: You should consider using a custom class for the items and store element of that class - or at least elements of the "inventory_item" component of the items - in your inventory array.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ hmm, wait, is the method theItem already inside C++ as well? could you please more elaborate this? Do I have to make an iterator to make this work? :O \$\endgroup\$ – Annalyne Sep 20 '12 at 10:53
  • \$\begingroup\$ @Annalyne No, "theItem" is just shorthand for "the variable which holds the item or at least something which evaluates to "true" when using operator== on the stored item in the container". I'll edit the code to make a full function out of it. And no, you don't need to make an iterator, they are standard feature of C++ containers. \$\endgroup\$ – Martin Sojka Sep 20 '12 at 10:56
  • \$\begingroup\$ thanks for this one. I will be waiting. I have some problems regarding this, I appreciate any help possible. Thanks! \$\endgroup\$ – Annalyne Sep 20 '12 at 10:57
  • \$\begingroup\$ hmmm, how can I call the function? is it by template(); ? \$\endgroup\$ – Annalyne Sep 20 '12 at 11:37
  • \$\begingroup\$ that is because i made a function / constructor wherein the player opens the inventory and uses anything that is available in it. i wanted to show the existing items, and use that item..specifying it is even harder than I thought. \$\endgroup\$ – Annalyne Sep 20 '12 at 11:46
1
\$\begingroup\$

IMHO it would be far easier to quit using a string for items.

If you just store numbers as items' IDs, you can use an enum like

enum EItemID {
    EIID_POTION,
    EIID_SWORD,
    ...
}

Then, all you need to do is

bool CanUseItemByID(EItemID itemID) {
    bool ret = false;
    //begin looping with iterator or whatever it is
        if (items[i] == itemID)
            ret = true;
    //end loop
}

I think it's far easier, and you wouldn't be comparing str everytime you access items, which is far slower than comparing two integers.

I think you could do this, since by what you've showed, your inventory system is quite simple.

Besides that, you could even optimize it more by sorting inventory by ID (again, easier to sort integers than strings) and then doing binary search.

To use items then, you could use a std::map with that numerical ID as your key, and whatever you need to know about it as your value, quite simple too.

Hope it helps.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I've just seen it's too many years old. Mobile app trolled me a bit \$\endgroup\$ – jjimenezg93 Aug 21 '16 at 23:19
0
\$\begingroup\$

Ugly but maybe easier to understand than a templated example (Personally I'd just go with a std::map though ...)

for(int i=0;i<MAX_ITEMS;i++)
{
  if(!strcmp(game_inventory[i],"Potion"))
  {
    //use the potion:
    Do thing()...

    //remove item
    game_inventory[i]=NULL;

    //bail out of loop
    i=MAX_ITEMS;
  }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.