In Trouble

This is a discussion on In Trouble within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a small RPG and I'm having trouble figurig out how to attach an inventory to a ...

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    In Trouble

    I'm trying to create a small RPG and I'm having trouble figurig out how to attach an inventory to a being when it's being created.

    Please take a look at my code

    Being.h
    Code:
    class Being
    {
    public:
                 
           void makeInv(Being& target);
           void atk(Being& target, Being& attacker);
           void takeDmg(Being& attaker, Being& target);
           
           void setHp(int HP){hp = HP;}
           void setSp(int SP){sp = SP;}
           void setFlee(int FLEE){flee = FLEE;}
           void setHit(int HIT){hit = HIT;}
           void setAtkPower(int ATKPOWER){atkPower = ATKPOWER;}
           void setMultip(int MULTIP){multip = MULTIP;}
           void setInvNumSet(int INVNUMSET){invNumSet = INVNUMSET;}
           
           int getHp(){return hp;}
           int getSp(){return sp;}
           int getFlee(){return flee;}
           int getHit(){return hit;}
           int getAtkPower(){return atkPower;}
           int getMultip(){return multip;}
           int getInvNum(){return invNumSet;}
                  
    protected:
              
              int hp;
              int sp;
              int flee;
              int hit;
              int atkPower;
              int multip;
              int invNumSet;
          
    };
    
    #endif
    Being.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    #include "Enemy.h"
    #include "Hero.h"
    
    void Being::makeInv(Being& target)
    {
         string *pInvNum;
         pInvNum = new string[target.getInvNum()];
         
         for(int i = 0; target.getInvNum() > i; i++)
         {
         pInvNum[i] = "\nEmpty Slot !!"; 
         cout << pInvNum[i] << endl;
         }     
    }
    
    void Being::atk(Being& target, Being& attacker)
    {
               int fleeRoll, fleeRoll2;
               
               fleeRoll = (target.getFlee() - attacker.getHit() );
               
               time_t seconds;
               time(&seconds);
               srand((unsigned int) seconds);
    
               fleeRoll2 = rand() % 100;
               
               if(fleeRoll < fleeRoll2)
               {
               cout << "\nHit !!";
               takeDmg(attacker, target);
               }
               else
               cout << "\nMiss !!";
    
      
    }
    
    void Being::takeDmg(Being& attacker, Being& target)
    {
         int temp;
         
         temp = target.getHp();
         temp -= (attacker.getAtkPower() * attacker.getMultip() );
         target.setHp(temp);
    }
    Enemies are the same as this class, so I won't waste space posting it twice.

    Hero.h
    Code:
    #ifndef HERO_H
    #define HERO_H
    
    #include <iostream>
    
    #include "Being.h"
    
    class Warrior : public Being
    {
    public:
           
          Warrior()
          {hp = 100, sp = 10, flee = 120, hit = 100, 
          invNumSet = 10, atkPower = 10, multip = 2; };
          
          void warriorAtk();
         
    };
    Anyone got any tips on how to attach the inventory to the instance when the constructor is called?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Assuming you have an item class. You could make a vector of item pointers in your being class.
    Code:
    #include <vector>
    
    using namespace std;
    
    class item;
    
    class being {
        public:
            void addtoInv(item*);
            item getfromInv(string);  // The name of the item perhaps
        private:
            vector<item*> Inv;
    };
    Last edited by SlyMaelstrom; 04-04-2006 at 04:11 PM.
    Sent from my iPadŽ

  3. #3
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Could you explain exactly what you are doing there. Still really unsure about pointers.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Sure. Let's pretend you have a higharchy of item classes. Then, all you have to do is declare an instance of each item. Then all rooms and beings will have an inventory that's simply a vector of pointers to an item. In theory, though, there is only one of each item object, but of course it could be pointed to multiple times. Then to use it, you simply call the items action and change the pointer to it to NULL (assuming it's a one use item). I found this easier than dynamically allocating and deleting objects every time you get or use them.

    If you're having trouble understanding this, then you should put off the RPG and try and learn pointers better. Once you get a good concept of that, jump back into the RPG if you wish.
    Last edited by SlyMaelstrom; 04-04-2006 at 05:14 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

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