RPG inventory, removing items

This is a discussion on RPG inventory, removing items within the Game Programming forums, part of the General Programming Boards category; So I have a basic Inventory system: Code: typedef struct sitem { string name; // A short name for the ...

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    RPG inventory, removing items

    So I have a basic Inventory system:


    Code:
    typedef struct sitem
    {
        string name; // A short name for the item
        string description; // A description of item
    }
    sitem;
    
    
    
    sitem sword = {
                      "Sword", "A big heavy sword."
                  };
                  
    sitem longsword = {
                          "Longsword", "A huge Long Sword!"
                      };
                      
    sitem poop = {
                     "Poop", "A piece of poop"
                 };
                 
    
    
    
    int main()
    {
        std::vector<sitem> inventory;
        inventory.push_back(longsword); // adds a longsword
        inventory.push_back(sword); // adds a sword
        inventory.push_back(poop); // adds a piece of poop
        
        
        for (std::vector<sitem>::const_iterator i = inventory.begin(); i != inventory.end(); ++i)
        {
            if (sword.name == "Sword")
            {
          //  i = inventory.erase();
            }
        }
        
        for (std::vector<sitem>::const_iterator i = inventory.begin(); i != inventory.end(); ++i)
        {
            std::cout << i->name << "\t" << std::endl << '\t' << i->description << endl;
        }
        
        return 0;
        
    }
    With this I am able to add items to the players inventory, but I don't know how to remove items. If I un-comment the orange line i get the following compiler error:

    " no matching function for call to `std::vector<sitem, std::allocator<sitem> >::erase()' "

    I would just like to be able to remove any given item in the inventory, I might have to add an id for each current item in there as well.
    Last edited by divineleft; 07-14-2006 at 11:38 AM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    inventory.erase(i);
    signature under construction

  3. #3
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    I tried that, but i get this error:

    609 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_iterator.h invalid conversion from `const sitem* const' to `sitem*'

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ah, remove the const_interator and replace it by iterator

    if you are just reading the items, use the const_iterator
    if you are modifying them use iterator.

    of course, a object referred via a const iterator cannot be removed (otherwise it would violate the constness)
    signature under construction

  5. #5
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Thanks a lot.

  6. #6
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Oh and btw, would anybody know how to add an id thing for the inventory when I can run a remove item function with the id number? Just wondering..

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,574
    Instead of a vector, use a map if the IDs will be unique or a multimap if not. One advantage is that you will not need to iterate through the container to find or delete elements.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Overload your remove function so you can pass it a name or a number. I'd suggest using your own class for objects, and making it a bit more generic. Your remove function would just be a "remove from what contains me" function. "Inventory" would just be an object that stores other objects.

    Or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Honestly, I'm not to great with maps. So far I fixed up to the orange line, I'm not sure what to do with the display() and remove() functions (I will do the function overload thing later)

    Code:
    void add(sitem stuff);
    void displaythings();
    void remove(int number);
    map<sitem, int> inventory;
    int amount;
    
    int main()
    {
    
        add(sword);
        add(poop);
        add(longsword);
        cout << amount;
        poop.id = 2;
        //remove (0);
        displaythings();
        
        
        
        system("Pause");
        return 0;
        
    }
    
    
    void add(sitem stuff)
    {
        inventory.insert( make_pair(stuff, 5));
    
    }
    
    --------------------------------------------------------
    
    void displaythings()
    {
        for (:map<sitem, int>::iterator i = inventory.begin(); i != inventory.end(); ++i)
        {
            std::cout << '(' << i->id << ')' << i->name << "\t" << std::endl << '\t' << i->description << endl;
        }
    }
    
    void remove(int number)
    {
    
        for (std::map<sitem, int>::iterator i = inventory.begin(); i != inventory.end();)
        {
    
            if (i->id == number)
            {
                i = inventory.erase(i);
                break;
            }
        }
    }
    Last edited by divineleft; 07-14-2006 at 06:13 PM.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,574
    maps offer specific functions you can use to access and change/delete elements based on the key. You don't need to build for loops to search and delete an item. It's much easier than that.

    Here's a tutorial on maps: http://www.cprogramming.com/tutorial/stl/stlmap.html
    And here is another one: http://cplus.about.com/od/stltutorial/l/aa120103e.htm
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Thanks

  12. #12
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Code:
    typedef struct sitem
    {
        string name; // A short name for the item
        string description; // A description of item
        int id;
    }
    sitem;
    
    sitem sword = {
                      "Sword", "A big heavy sword."
                  };
                 
    
    void add(sitem stuff);
    map<sitem*,int> inventory;
    
    
    int main()
    {
    
       add(sword);
        return 0;
    }
    
    
    void add(int stuff)
    {
         
        inventory.insert ( make_pair (const stuff, 18 ) );
    
    }
    If I change the map to something other than the sruct "sitem", it works (int, string ect..). I just can't get it to insert a typedef'ed struct!
    Last edited by divineleft; 07-15-2006 at 04:50 PM.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You add items by using std::make_pair() or creating the pair manually.

    In this snippet from my tile manager class you will see that I'm adding an object to the end of the list. This map is:

    std::map<DWORD,CTileGDI *> m_mapTiles;

    It uses DWORDs or 32-bit unsigned integers and to access CTileGDI objects. The DWORD can either be assigned, in which case if it is assigned an already existing DWORD, they are swapped - or you can use the following Add() which simply adds the item to the end of the map, assigning it a DWORD representing the current size of the map.

    Code:
    void CTileMgrGDI::Add(CTileGDI *pTile)
    {
      if (!m_bInit) return;
      
      pTile->m_dwID=static_cast<DWORD>(m_mapTiles.size());
      
      m_mapTiles.insert(std::make_pair( static_cast<DWORD>(m_mapTiles.size()),
                                        (CTileGDI *)pTile));
                                        
      
    }
    I'm casting map::size() to a DWORD to avoid the warning resulting from size() returning size_t instead of DWORD.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem
    By ExDHaos in forum C++ Programming
    Replies: 12
    Last Post: 05-22-2009, 05:50 AM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  4. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  5. Replies: 26
    Last Post: 06-15-2005, 03:38 PM

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