Changing Variables

Hello everyone, I am a 2nd year student of video game animation and I have recently begun taking a programming class.

My question is about how to create a changing variable for the Health and Mana of a character.

Its not meant to be run in an engine or anything just compiled and used in C++ to display as text.

here is my current line of code

cout << "Voidling " << VoidlingHealth-iceboltDmg << "HP\n\n" << Playername << " " << Health << "HP, " << Mana-iceboltCost<< "Mana\n\n";


Now what I want is for the VoidlingHealth Variable to display as the subtracted number when i use it in another line of code.

VoidlingHealth has a value of 20, iceboltDmg has a value of 5. So it displays the voidling as having 15HP after I run it which is great but when I write the VoidlingHealth variable in a new line of code it goes back to the original value of 20 how can I make it so that the variable changes when I subtract the iceboltDmg
Last edited on
1
2
3
4
5
6
7
int VoidlingHealth = 20; // Define/initialize the variable
int iceboltDmg = 5;

if (hit)
  VoidlingHealth -= iceboldDmg;

cout << "Voidling " << VoidlingHealth << ....
Last edited on
Expressions in a line of code are evaluated but the resultant value is copied into a temporary value then discarded. What you wrote isn't displaying the current value of VoidlingHealth, it's displaying the value of the expression Voidlinghealth - iceboltDmg, which is 15. The health variable is still 20. If you want to store the resultant value for use later you need to store it in another variable, or you can use a compound arithmetic operator. Something like this:

VoidlingHealth -= icebolDmg;

If you want to use that instead you might have to use parentheses around the expression to make sure it evaluates properly however, since it might output the health variable before the expression is evaluated. Associativity is a tricky subject. :) You could do it like this:

1
2
3
4
5
6
7
8
9
cout << "Voidling "
      << (VoidlingHealth -= iceboltDmg)
      << "HP\n\n"
      << Playername
      << " "
      << Health
      << "HP, "
      << Mana-iceboltCost
     << "Mana\n\n";



If you want to keep track of the original health of the voidling you'll either have to use a seperate variable like VoidlingMaxHealth, or use a variable to store the current health of it and use that variable in place of VoidlingHealth.
Last edited on
Thanks so much for the Help~
We've only taken two classes so far and so the teacher has yet to explain aloot of this things
This is meant to be a simple homework i which we just have to Add, subtract, multiply and divide Variables which is extremely simple so i decided to do it sort of like a text based game.
Now There is a line in which the "Player" is asked to input an "Incantation"
Being the name of the Spell i.e Icebolt
How can I make it so nothing happens unless the person inputs the correct word
Also is the (VoidlingHealth -= iceboltDmg) Reusable?
and can I use it with other functions such as adding or dividing?
For example (Voidstrike /= weakeneffect)
Last edited on
Sure can:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

1
2
3
4
5
6
7
8
9
10
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b
For the incantation, is there only one option? If there is, then you can use

1
2
3
4
5
string playerSpell;
while(playerSpell!="Icebolt")
{
     //have them type again
}


Or if it's giving the player one chance, then
1
2
3
4
5
6
7
8
9
10
11
12
if(playerSpell=="Icebolt") //change spell name at your leisure
{
//cast spell
}
else if(playerSpell=="Firebolt")
{
//cast that spell
}
else
{
//do nothing, or inform them it failed
}

If there is no indication that the player messed up, you don't even need the else bracket.


And yes, the +=,-=,*=,/= are reusable.
Topic archived. No new replies allowed.