Search code examples
batch-filematharithmetic-expressionsinteger-arithmetic

Arithmetic in Batch?


I'm making a game, and I have a Health variable to track how much health the player has as they go through the game. At one point, the player cuts their lip on a can (Uneventful, I know), and lose 2 HP. I'm trying to use Arithmetic to subtract 2 HP from the 'Health' variable (Which is still at 100 at that point), and then show how much health they have left, yet it still shows 100 HP after I tried to remove 2 HP. Am I doing it right?

:Beans
call :colorEcho 7 "You decide you're going to have Canned Beans for     breakfast. Yum. You grab a knife from the kitchen counter and work the top off. You eat the beans straight from the can. Ouch! You cut your lip! It starts to    bleed."
Echo .
set /a "_Health=_Health-2"
Echo.
call :colorEcho C "# ALERT; You have lost 2 Health Points! You are now at %Health% HP!"
Echo.
pause
exit

What happens:

what happens


Solution

  • Ok, I found out the answer thanks to @dbenham. Thank you!

    I had messed up the variable, and as dbenham pointed out, I was expanding %Health% when I was using _Health in set /a _Health-=2. I changed it to just set /a Health-=2, and everything seems to be working fine now.