Reputation: 17
I've been making a text-adventure, but towards the end of my coding it came up with this error. Every time I try to continue from scene 5 to scene 5.1 it goes to a different scene and skips through it. Help?
Here is my code:
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
cls
echo You walk in through the doorframe, and into the dark room. You can't see a thing in the gloom, but when you put your hand on the wall you recognise the shape of a lightswitch.
echo.
pause
goto scene5.1
:scene5.1
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
echo 1) Table
echo 2) Sofa
echo 3) Fireplace
echo 4) Go back
set /p type
if %type%==1 goto scene5.1.1
if %type%==2 goto scene5.1.2
if %type%==3 goto scene5.1.3
if %type%==4 goto scene5.1.4
Upvotes: 0
Views: 1425
Reputation: 80211
@ECHO OFF
SETLOCAL
:scene4.6.1
:scene5.1
SET "scene=5.1"
cls
echo You flip the lightswitch down, and slowly the glowing bulbs switch on; illuminating the living room. In the gradually increasing light you see a table in the corner, and a large sofa that streches across the back wall. A few metres in front of the couch is a fireplace.
echo.
CALL :addchoice Table Sofa Fireplace "Go back"
GOTO makechoice
:scene5.1.1
ECHO AT table
goto :eof
:scene5.1.2
ECHO AT sofa
goto :eof
:scene5.1.3
ECHO AT fireplace
goto :eof
:scene5.1.4
:scene4.6
SET "scene=4.6"
echo You are IN a dank corridor dimly lit by sputtering torches
echo.
CALL :addchoice Doorway Corridor
GOTO makechoice
:makechoice
CHOICE /C %choices%
SET "scene=%scene%.%errorlevel%"
SET /a choicecount=0
SET "choices="
GOTO scene%scene%
:addchoice
SET /a choicecount+=1
ECHO %choicecount%) %~1
SET "choices=%choices%%choicecount%"
SHIFT
IF "%~1" neq "" GOTO addchoice
GOTO :EOF
This may save you a heap of programming. Please do not accept it as an answer since bgalea's
answer actually answers your question. This is just an approach to make the construct-an-adventure exercise faster and easier.
For each scene, describe the scene and call :addchoice
with parameters of the available choices. If a choice is multiple words, "enclose them in quotes".
The routine assigns 1
to the first choice in the list, 2
to the next and so on. The available choices are then recorded in choices
.
go to makechoice
. This prompts for an entry using the list in choices
. Then append dot+the errorlevel
of the response to the current scene number, clear the choices and goto scene
+the calculated scene number.
Note that you can use an empy command-sequence to move between scenes by using the structure like in scene5.1.4 to scene4.6 (iow, scene 5.1+choice4 moves to 4.6)
This way, your response-sets become one line after your scene description and you never need to use a series of if-commands to move between scenes.
Upvotes: 0
Reputation: 67
C:\Windows\system32>echo 4) Go back
4) Go back
C:\Windows\system32>set /p type
The syntax of the command is incorrect.
goto was unexpected at this time.
It's probably the lack of an equals sign in set /p
(well I know it is).
PS: Set /p var = PromptText
is not the best command. It doesn't prevent wrong entry, and you don't check for it. Use Choice
command instead. It only takes allowed entry.
The reason most examples use set /p is because choice was dropped from one windows version 15 years ago. That was along time ago.
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
As help says use errorlevels in descending order
if errorlevel 4 dosomething
if errorlevel 3 dosomething
if errorlevel 2 dosomething
if errorlevel 1 dosomething
if errorlevel 0 UserPressedCtrl+C
or in any order
if errorlevel 1 if not errorlevel 2 dosomething for 1
Do not use %errorlevel%
as it can be overwritten by other programs.
Upvotes: 1