1

I just started learning batch, and figured the best way to learn would be to give myself a project. I'm attempting a text-based RPG, and am hung up in one spot. I am trying to nest multiple IF-IF NOT DEFINED, and IF-ELSE "questions" in one "conversation".

if "%choice18%"=="talk hag" (
	if "%glove%"=="0" (
		if "%ketchup%"=="2" (
			echo.
			echo "AHH! Can it really be ye, Cramforth?" the hag asks. "If it be, then ye wouldn't mind answering a few personal questions bout us to prove it, would ye?" GULP! Art thou ready to answer the hag's personnal questions?
			echo.
			pause
			:hag
			echo.
			echo Yes or no:
			set /p choice19=
			echo.
			if "%choice19%"=="no" (
				cls
				echo.
				echo "Then I'll never know for sure," sniffs the hag. You Dungeonheartbreaker.
				echo.
				pause
				cls
				goto West2a
			)
			if "%choice19%"=="yes" (
				cls
				echo.
				echo "Very well. If ye answers questions three, then I'll know thou art me precious Cramforth."
				echo.
				pause
				cls
				echo Where didst we geaux on our honeymoon? A: Wensleydire, B: Rottenscab, C: Blood Area, or D: Zork?
				echo.
				set /p choice20=
				echo.
				if "%choice20%"=="b" (
					cls
					echo.
					echo What did ye get me for me 114th birthday? A: Toad, B: Eyeball, C: N64 Paddle, D: Commemorative mug?
					echo.
					set /p choice21=
					echo.
					if "%choice21%"=="d" (
						cls
						echo.
						echo How many hags didst we have together? 1, 2, 3, or 4?
						echo.
						set /p choice22=
						echo.
						if "%choice22%"=="3" (
							cls
							echo.
							echo "Cramforth! Tis really ye!" mistaking thee for her dead husband. Ye suffer several prickly kisses from the hag which leave the odor of rat liquer on thy face. "No wonder ye died of a bloody head, ye forgot thy FLASK GETTING GLOVE before ye set out to vanquish that rogue dungeon."
							echo.
							pause
							echo.
							echo "Perhaps ye can still defeat that dungeon, even in thy new hotter, spectal form." Um, eww. "Here!" she says and hands ye a wicked FLASK GETTING GLOVE!
							echo.
							pause
							set glove=1
							goto West2a
						) else (
							cls
							echo.
							echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
							echo.
							pause
							cls
							goto HD
						)
					) else (
						cls
						echo.
						echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
						echo.
						pause
						cls
						goto HD
					)
				) else (
					cls
					echo.
					echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
					echo.
					pause
					cls
					goto HD
				)
			)
			if not defined "%choice19%" (
				cls
				echo.
				echo Just answer the hag 'yes' or 'no'. Please?
				echo.
				goto hag
			)
		) else (
			echo.
			echo "Away with ye," she shrieks, "I speaketh only to me dead husband who died of a bloody head!" She becomes abruptly silent and goes back to her landbeforetimeing. A bloody head, eh?
			echo.
			pause
			cls
			goto West2a
		)
	) else (
		echo.
		echo "Ye go show that dungeon who's lord, Cramforth! You da lord!"
		echo.
		pause
		cls
		goto West2a
	)
)

The problem I am running into, is that the first user input variable set /p choice19= ignores the IF commands the first time around.

Example:

:hag
echo.
echo Yes or no:
set /p choice19=

User inputs: yes

Ignores: if "%choice19%"=="yes" (

Goes to: if not defined "%choice19%" ( cls echo. echo Just answer the hag 'yes' or 'no'. Please? echo. goto hag

Redirects back to:

:hag
echo.
echo Yes or no:
set /p choice19=

User again inputs: yes

And this time it follows the correct command continuing down the "yes" branch.

However, this poses a problem for outcomes that do not redirect back to the question (ie. if "%choice20%"=="b" () and end the game.

I reviewed a similar issue (Nested IF ( IF ( ... ) ELSE( .. ) ) statement in batch) however, I'm noticing that it is slightly different than my case, being that my variables are defined. So, I'm at a loss. Any ideas?

Note: I am aware that I could restructure the commands without deep nesting to solve the problem, and just GOTO to different :LABELS. I just want to see what's causing this issue.

3
  • 4
    Not the same question, but the same answer Commented May 13, 2015 at 20:24
  • Thank you, worked like a charm. I appreciate the detailed explanation with it, it explained a lot. Commented May 13, 2015 at 20:55
  • 1. goto breaks the if block context, so when jumping to :hag, the code after that is executed as if it were ouside of any () code block; 2. when using if [not] defined choice19, don't place %% around choice19; 3. you're using set /P for user prompts; you should also handle the case when the user inputs something other than asked for; 4. set /P choice19= keeps the former value of a variable, if the user just presses enter; perhaps place set choice19= before to clear choice19; 5. when checking user inputs, maybe use `if /I [not] "%choice19%"=="no", so it's case-insensitive; Commented Nov 27, 2015 at 0:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.