0

I'm making a game in batch, and I have the player lose 5 HP in the beginning of the story, depending on their choices. However, after the program subtracts 5 HP from the original HP (100), it displays correctly, but then it reverts back to 100 HP, instead of 95.

Variables at the very top of the program

:: Stats
set Health=100
set Food=100
set Water=100
set Infection=100
set Stamina=100

Code where the player loses health (Note :StatChange, that calls correctly with 95 HP)

:Beans
setlocal disableDelayedExpansion
call :colorEcho2 7 "Finally deciding that you're going to have Canned Beans for breakfast, you grab a knife from the kitchen counter and    work the top off. While eating the beans straight from the can, you cut your lip! It starts to bleed."
Echo .
set /a Health=Health-5
Echo.
call :colorEcho2 C "# ALERT; You have lost 5 Health Points! You are now at %Health% HP!"
setlocal enableDelayedExpansion
Echo.
Echo.
pause
cls
call :StatChange
Echo.
call :colorEcho 7 "You quickly grab a rag and hold it to your lip, stopping the bleeding. The can didn't cut very deep, so it should heal  quickly. Once your lip has stopped bleeding, you carefully finish your breakfast, and drink some water."
call :colorEcho 7 "You decide the knife would be useful, so you stuff it in your Daypack that you grabbed from your bedside table."
setlocal disableDelayedExpansion
Echo.
Echo.
call :colorEcho2 E "Acquried Knife! +2 Damage to Zombies!"
setlocal enableDelayedExpansion
set Weapon="Knife"
Echo.
Echo.
call :colorEcho 7 "You look towards your cupboard, and see the Bacon still lying there. You contemplate on whether or not you should take  it with you."
Echo.
Echo.
Echo Are you going to take the Bacon with you?
Echo Your options are:
call :colorEcho C "================="
Echo.
Echo 1. Ew, no way^^^!
Echo 2. Sure, why not? More food means better chance of survival.
call :colorEcho C "================="
Echo.
Echo Please enter the number corresponding to your answer.
set /p Answer4="Enter your choice: "
Echo.
setlocal disableDelayedExpansion
if "%Answer4%"=="1" call :colorEcho2 7 "You decide not to take the Bacon!"
setlocal enableDelayedExpansion
Echo.
if "%Answer4%"=="2" (
    call :colorEcho 7 "You stuff the Bacon into your backpack."
    Echo.
    Echo.
setlocal disableDelayedExpansion
    call :colorEcho2 E "Acquired Bacon!"
setlocal enableDelayedExpansion
    set /a Bacon+=1
    Echo.
    Echo.
)
pause
exit /b

Where the variable goes back to 100, even though I did not add anything (The block before exits to here)

cls
call :StatChange
Echo.
Echo.
call :colorEcho 7 "You walk into your storage room. You see lots of crates, some filled to the top with items. You look around, wondering  what to take."
Echo .
call :colorEcho 7 "You see a med pack, an MRE, some tablets, an Adrenaline syringe, a grenade, and a flare."
call :colorEcho 7 ". You look at your backpack, and you realize that you may not have enough space in the future if you take everything. You decide to take only 2 items."
Echo .
Echo.

Block where the stats are displayed

:StatChange
call :colorEcho A "Current Health = "
call :colorEcho C " %Health%"
Echo.
call :colorEcho A "Hunger = "
call :colorEcho C " %Food%"
Echo.
call :colorEcho A "Thirst = "
call :colorEcho C " %Water%"
Echo.
call :colorEcho A "Infection = "
call :colorEcho C " %Infection%"
Echo. 
call :colorEcho A "Stamina = "
call :colorEcho C " %Stamina%"
Echo.
exit /b

Player loses 5 HP, calls normally

https://i.stack.imgur.com/L4vLG.jpg

At the top left, shown '95' for current health, is called correctly at 95

https://i.stack.imgur.com/v9Fba.jpg

What?!?! It went back to 100! That's where my problem is.

https://i.stack.imgur.com/FaUOj.jpg

FudgeMuffins
  • 321
  • 2
  • 12
  • 2
    There are many `setlocal` commands but no `endlocal`... – aschipfl Dec 02 '16 at 16:19
  • 1
    Please [edit] your question to satisfy a [mcve]. Too much unknowns as currently written… – JosefZ Dec 02 '16 at 16:22
  • `setlocal` is not a switch - it must be matched by a corresponding `endlocal` (or ends if you reach `exit` or end-of-file) when any environment changes made since the `selocal` are backed ou and the original variables and values are restored. It might even help if you were to say **which** variable is unexpectedly not changing or is unexpectedly reverting. ((YOU** know - **WE** would have to closely analyse your code and make assumptions... – Magoo Dec 02 '16 at 16:26
  • Maybe that an unpaired `endlocal` appears somewhere in `:colorEcho2` procedure? – JosefZ Dec 02 '16 at 16:31
  • When leaving a sub-routine entered by `call`, there is also every `setlocal` block ended implicitly (as if there were as many `endlocal` commands as `setlocal`); I guess that is the main problem here... – aschipfl Dec 02 '16 at 18:02
  • Why that many `setlocal` ?? – J.Baoby Dec 02 '16 at 18:03
  • @Magoo , `%Health%` is unexpectedly changing. I've edited my question to include screenshots. @aschipfl How would I use `endlocal`? @J.Boaby , I use those commands for 2 different color blocks so I can use different types of punctuation. – FudgeMuffins Dec 02 '16 at 23:11
  • You use at least 8 times "setlocal". Each [`setlocal`](http://ss64.com/nt/setlocal.html) will **start** a new "local child-environment" from the environment it has been called. Changes to variables in one environment will not be visible in the parent environment (where you called the `setlocal` that created the environment). If you want to close an environment you created with `setlocal` you have to use `endlocal` (is probably also what @aschipfl means). So let me rephrase my question: why do you need that many `setlocal`? Can't you do it without? – J.Baoby Dec 03 '16 at 14:16
  • @FudgeMuffins Please take a look on my answer on [change directory command cd ..not working in batch file after npm install](http://stackoverflow.com/a/38676582/3074564) where I explained in detail what `setlocal` and what `endlocal` do with references to two more answers demonstrating explained behavior. – Mofi Dec 03 '16 at 14:41
  • @J.Baoby I tried without, the punctuation just dissapears. – FudgeMuffins Dec 03 '16 at 17:20
  • If by ponctuation you mean your exclamation marks (`!`) this is perfectly normal since you [enable the delayed expansion](http://ss64.com/nt/delayedexpansion.html) with `setlocal enabledelayedexpansion`. Delayed expansion causes `!` to be used to read from variables (you can then choose `!var!` or `%var%`, see link for info). Each time it sees `!` it will try to read a variable. – J.Baoby Dec 03 '16 at 17:34
  • In your script I see no reason to use delayed expansion and if that is the only reason why you use `setlocal` (correct me if I'm wrong), just put a `setlocal disabledelayedexpansion` at the top of the script and remove all other `setlocal ...`. By denabling delayed expansion you will give `!` its primary meaning back: just an exclamation mark. – J.Baoby Dec 03 '16 at 17:37
  • @J.Baoby If I use `setlocal disabledelayed expansion` it gives me `!param!` No idea why. – FudgeMuffins Dec 03 '16 at 18:36
  • You have a whitespace that shouldn't be there: `setlocal disabledelayedexpansion`. But it looks like you do have a variable `!param!` somewhere that uses delayed expansion. Where is it?? – J.Baoby Dec 03 '16 at 18:38
  • @J.Baoby Should we use a chatroom to prevent more notifications to the others? I don't have the privileges to make one however. – FudgeMuffins Dec 03 '16 at 18:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129701/discussion-between-j-baoby-and-fudgemuffins). – J.Baoby Dec 03 '16 at 19:22

0 Answers0