Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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;
  }
}
share|improve this answer
add comment

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.

share|improve this answer
    
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 –  Annalyne Sep 20 '12 at 10:53
    
@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. –  Martin Sojka Sep 20 '12 at 10:56
    
thanks for this one. I will be waiting. I have some problems regarding this, I appreciate any help possible. Thanks! –  Annalyne Sep 20 '12 at 10:57
    
hmmm, how can I call the function? is it by template(); ? –  Annalyne Sep 20 '12 at 11:37
    
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. –  Annalyne Sep 20 '12 at 11:46
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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