I keep getting a logic error when I go to fight a monster.
It says the life must be initialized and then I get a runtime error. So I put a value to it and it runs fine. But the question is, what happens after the fight. I don't want my character to to take all this damage and then win and then all of a sudden he has one hundred hit points again. I would just like to know the correct way to access the life class variable so that it shows up in the fight funtion and works how it is supposed to.

ENTITY.h
private:

int life;
------------------------------------------------------------------------

ENTITY.cpp
void ENTITY::SetLife(int newLife)
{
	 
	if (life < 0)
		life = 0;
}
int ENTITY::GetLife()
{
	return life;
}
}
---------------------------------------------------------------------------------
PLAYER::PLAYER()//derived from ENTITY class
{
SetLife(100);//Here the value of SetLife is stated
}
---------------------------------------------------------------------------------------
void fight()
{
PLAYER player;// derived from ENTITY
  
  int life = 100;// I don't think I should have to put a value to this variable if it's already stated in a class.


player.SetLife(life);
}

strange, every time you fight your creating a new player character, his life is already 100.

Why is void fight a local declartion, should it be a member of that class? And why are you using player.setLife? to set the new life of that player when its already should be in class scope?

Maybe ive read that code all wrong and perhaps fight is in another class? Perhaps the world entity functions class? If so you should not be making a new player but instead passing who is fighting what. That way you dont need to set health each time cause its contained in the player information.

I have the this fight in a function that is in the main file. Now I know I can just as easily put the Life variable in the main cpp and it's problem solved but I want to learn how to do it from a class the correct way. I don't have a fight class for now. I am just learning how to implement battle in my RPG.

"If so you should not be making a new player but instead passing who is fighting what. That way you dont need to set health each time cause its contained in the player information. "

That is exactly what I would love to figure out.

This function:

void ENTITY::SetLife(int newLife)
{
	 
	if (life < 0)
		life = 0;
}

Doesn't set the life at all. All it does is check to see if the current life is less than zero, and if so, sets it to zero. The input parameter is ignored. That's why the constructor for the PLAYER class doesn't actually set life to 100. A better function would be:

void ENTITY::SetLife(int newLife)
{
       life = newLife;	
 
	if (life < 0)
		life = 0;
}

Are you familiar with pointers? To make better fighting, you would have a pointer to a PLAYER, and then pass this pointer as a parameter to your fight functions. The way you're doing it now (as Acidburn pointed out), you create a new player every battle, and every new player gets his health set to 100 (assuming you fix the problem I mentioned above).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.