The title is confusing i know, i couldn't think of the right name. Anyway, i have 2 integers, both in 2 seperate classes in 2 seperate files. I have main.cpp, test_01.h, Test_01.cpp, Player.h and Player.cpp. I ahve a function which takes an integer (a) as a parameter (from Test_01.h and Test_01.cpp). a sets another integer (attackPower) from the same class equal to a. The integer (health) in the other class (Player.h and Player.cpp), is then equal to health -= attackPower. But instead of giving me the right answer 75 (health = 100 and a is set to 25 when i call the function) it gives me the answer.
The "p" in main and the "p" in your class aren't the same thing.
I would be tempted to give the player class a TakeDamage function:
1 2 3 4
void Player::TakeDamage( int damage )
{
health -= damage;
}
Then maybe have an Attack function for the other class, passing in a target.
1 2 3 4
void Test01::Attack( Player &target )
{
target.TakeDamage( attackPower );
}
Ideally, a better way to do this would be to have a GameObject type or something like that, that already has the Attack and TakeDamage functions. Then you could inherit from that class. This way, you could pass in a reference to a GameObject, rather than a Player in the Attack function.
I figured it out with inspiration from iHutch105 (thank you). I made a void exactly the same as TakeDamage Above. I then made the void above as an int with an int parameter of your choice. it returns the int of your choice. Then you call take damage and for the parameters you put attack with the int of your choice.
Calling the functions
1 2 3 4 5 6
Player p;
Enemy e;
p.takeDamage(e.Attack(20));
TakeDamage
1 2 3 4 5 6 7
health = 100;
void Player::takeDamage(int damage)
{
health -= damage;
}
Attack
1 2 3 4
int Enemy::Attack(attackPower)
{
return attackPower;
}