# Problem with nested if elements in batch file (text rpg)

@echo off

:An
@echo Everything has a start...
set /A hp=10
pause
cls
@echo You are a teenage %_inputgen%
pause
cls
@echo This story starts here...
pause
cls
@echo You wake up alone in your room, it's still dark.
@echo 1: Get out of bed
@echo 2: Try to sleep
SET /P _1Dec= What you want to do?
cls
if %_1Dec%==1 (
@echo Now you finally got up. You turned the light on. The first thing you saw was a blinding flash of light.
) else (
@echo You lazily closed your eyes again. It's not morning right now. Why would you get up so early. You managed to fall asleep again easily...
@echo When you finally get up, it's already morning and you can feel all the light even through your closed eyes.But you feel rested and in better mood
set /A hp=%hp%+1
)
pause
cls
@echo Now you can see. But there's one problem. You don't like what you see.
@echo Your mom once said, that you have the most untidy room on the whole world.
@echo But nevermind your mom. The room is quite big, but most of the place is taken by your table, bed and a gigantic wardrobe.
@echo 1: Search around a bit
if %_1Dec%==1 (
@echo 3: Get some sleep
SET /P _2Dec= What do you want to do?
) else (
@echo 2: Go out of your room
SET /P _2Dec= What do you want to do?
)
if %_2Dec%==1 (
@echo First you open your wardrobe.
@echo The first thing you notice (apart from the incredible mess) is a little silver flashlight, currently turned on.
@echo 1: Take it
@echo 2: Let it here
SET /P _2.5Dec= Take it?
if %_2.5Dec%==1 (
set /A flsh=1 >inventory.bat
color 0a
@echo Altough it seems basic, use it as wisely as possible…
color 0
) else (
@echo You don’t exactly need a flashlight right now, so why to take it and make your pockets heavier?
)
@echo Once you’ve done everything you wanted, you turn your back on the wardrobe.
@echo You sit to your table, right in front of the giant pile of books and papers.
@echo Taking a look at the book on the top, you recognize a cover of your favourite book,
@echo Looks like you’ll have to sit down and read…
) else (
Pause
)


As it gets to the line with if %_2Dec%==1..it just crashes.. Can someone please tell me why?

• Does it give you any error messages? – Bálint Jun 4 '19 at 9:12

There are multiple problems here, firstly, the parentheses in one of the echo messages messes up the nesting. You need to surround it with double quotes:

@echo "The first thing you notice (apart from the incredible mess) is a little silver flashlight, currently turned on."


Secondly, for reasons that are beyond me, the variable _2.5Dec expands when the _2Dec if statement runs. Since it doesn't have a value by then, it fails and the program stops. You need to use setlocal enabledelayedexpansion somewhere before the outer if statement and then use !_2.5Dec! in the inner if statement.

You'll also run into quite a bit of trouble later if you use the wrong "apostrophe",  isn't the same as '

My main advice in using batch is don't.

• Using a .` in a variable name is odd. – Vaillancourt Jun 4 '19 at 15:43
• @AlexandreVaillancourt Batch is odd. It's absolutely fine with it – Bálint Jun 4 '19 at 16:42
• Yeah, I guess so. I like your main advice. – Vaillancourt Jun 4 '19 at 16:51