How to change all variables

I need some help. Here is my problem. I give a variable a number, and then a bunch of other variables that include the first variable.

Well what gets me frustrated is that when I change the first variable later on, using something like 'Variable1++;' or 'Variable1=50;' the rest of the numbers don't change along with it.

I know this because I printed it out on the screen before and after changing the variable to another number.

Also this is more of a skill level for something like a RPG game. I'd like to know how to do it, and if there is an easier way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int playerlevel = 1;
int enemylevel = playerlevel;
int playerhp = playerlevel * 50;
int enemyhp = enemylevel * 250;

int main()
{

cout << playerlevel << endl;
cout << enemylevel << endl;
cout << playerhp << endl;
cout << enemyhp << endl;
int playerlevel = 2;
cout << playerlevel << endl;
cout << enemylevel << endl;
cout << playerhp << endl;
cout << enemyhp << endl;


    return 0;
}
1) Make playerhp a function like that:
1
2
3
4
5
6
//...
int playerhp()
{
return playerlevel*50;
}
//... 


2) Make a class containing private members playerlevel and playerhp and make some access function to work with variables
Why did you expect that changing the variable enemylevel would cause playerhp and enemyhp to change?

Each variable is a location in memory. So

int playerlevel = 1;

stores the value 1 in the location where the int playerlevel is located.

int enemylevel = playerlevel;

reads the value out of the memory where int playerlevel is located and then stores it in the memory location for enemylevel.

The two variables now have the same value, but they are stored in totally different locations in memory. So changing one location will not change the other.

If I understand your problem correctly, that you want to be able to change (only) playerlevel and have the other variables track the changes according to some fixed rules, then the C++ way would be a class.

Andy
Last edited on
It's because you're not recomputing their values.
If you want their value to depend on playerlevel, you must recompute them each time playerlevel changes or each time you read them.

Here's a small example using function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int playerlevel = 1;
int enemylevel(void) { return playerlevel; }
int playerhp(void) { return playerlevel * 50; }
int enemyhp(void) { return playerlevel * 250; }

int main()
{

cout << playerlevel << endl;
cout << enemylevel() << endl;
cout << playerhp() << endl;
cout << enemyhp() << endl;

playerlevel = 2;
cout << playerlevel << endl;
cout << enemylevel() << endl;
cout << playerhp() << endl;
cout << enemyhp() << endl;

    return 0;
}


A word of warning: my example works only because playerlevel is a global variable.
Thanks guys, especially you toum. That really helped me out. And most of my variables will be a global variable to make things simple. Unless I think differently later on.
Topic archived. No new replies allowed.