0

I am making a game that includes orcish enemies. I have one problem, how do initialize their HP, MP, Attack, defense, and speed without writing different code for each like this:

int orcishHP = 50;
int orcishMP = 5;
int orcishAttack = 15;
int orcishDef = 10;
int orcishSpeed = 20;

Isn't there some way to initialize all that when I refer to the orc, like this:

int orcishStats(){
   int HP = 50
   etc...
}

So instead of calling orcish MP HP and all that stuff, I have all of it in one one place.

If this made sense, please help. If this didn't make sense, don't help.

  • That's why we have OOP :) Declare base class `Unit`, create an inherited class `OrcUnit` and set these values in constructor, overriden properties or wherever else. – Yeldar Kurmangaliyev Nov 20 '15 at 04:15

2 Answers2

0

Here's the correct way to do this:

You define your attributes in a class/struct, and when that struct is initialized, the constructor(OrcishAttributes()) is called:

struct OrcishAttributes
{
  int m_orcishHP;
  int m_orcishMP;
  int m_orcishAttack;
  int m_orcishDef;
  int m_orcishSpeed;
  OrcishAttributes(int orcishHP, int orcishMP, int orcishAttack, int orcishDef, int orcishSpeed)
  {
    m_orcishHP=orcishHP;
    m_orcishMP = orcishMP;
    m_orcishAttack = orcishAttack
    m_orcishDef = orcishDef;
    m_orcishSpeed = orcishSpeed;
  }

}

class Orc
{
  public:
  Orc(OrcishAttributes oa)
  {
     m_orcAttributes = oa;
  }
  private:
    OrcishAttributes m_orcAttributes;
}

int main()
{

  OrcishAttributes oa(50,5,15,10,20);
  Orc* o = new Orc(oa);
}
wizurd
  • 3,541
  • 3
  • 33
  • 50
  • 1
    You should use the constructor initializer list, instead of assignment statements inside the constructor body – M.M Nov 20 '15 at 04:32
  • Although for non-primitive types, you should use a member initializer list, rather than setting the values in the body of the constructor; otherwise, you end up default constructing the non-primitive types, then replacing them with new values a moment later. – ShadowRanger Nov 20 '15 at 04:32
  • Thanks for the help, I haven't learned classes and structures yet, any article or book you could recommend so I don't have a problem similar in the future? – compilererror Nov 20 '15 at 04:32
  • `OrcishAttributes oa = new OrcishAttributes(50,5,15,10,20);` is an error. `OrcishAttributes oa(50,5,15,10,20);` would be better. – M.M Nov 20 '15 at 04:32
  • @ISuckatCPlusPlus [see here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for book list – M.M Nov 20 '15 at 04:33
  • @wizurd same for `Orc o` – M.M Nov 20 '15 at 04:37
0

That's why we have OOP.

You can define a base class, lets say Unit. It can store common members like fields or methods:

class Unit
{
protected:
    int HP;
    int MP;
    int Attack;
    int Def;
    int Speed;

public:
    int getHPplusMP()
    {
        return HP + MP;
    }
};

Then, you can create inherited classes and initialize these values in its constructors:

class OrcUnit : public Unit
{
public:
    OrcUnit()
    {
        HP = 20;
        MP = 0;
        Attack = 13;
        Def = 4;
        Speed = 5;
    }
};

class ElfUnit : public Unit
{
public:
    ElfUnit()
    {
        HP = 25;
        MP = 15;
        Attack = 15;
        Def = 1;
        Speed = 8;
    }
};

Then, you will be able to work with it:

ElfUnit elf;
OrcUnit orc;

Unit u = elf;
cout << u.getHPplusMP();

Unit u = orc;
cout << u.getHPplusMP();

In general, it is definitely a good idea to learn OOP very well before trying to implement games.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • This is possible but not necessarily advisable, it's simpler to just have `Unit` with another member saying whether it's an orc or an elf. Beginners tend to over-use inheritance. Which approach to go with is something you learn with much experience in C++ – M.M Nov 20 '15 at 04:31
  • @M.M I just have provided one of many possible solutions :) Not sure about C++, I am a C# developer, and I would definitely use this approach if I have developed a game. Actually, this question is pretty opinion-based. – Yeldar Kurmangaliyev Nov 20 '15 at 04:35