I am trying to make a batch text-based game. But i encountered a problem just starting to write it what I have never encountered before.
:menu
:: the game menu - opens when the game starts
cls
echo This game is still being made -- expermintal
echo Start Screen:
echo [1] View Changes
echo [2] Start Game
echo enter your choice:
set /p startchoice =
if %startchoice%==1 goto changes
if %startchoice%==2 goto startgame
When i type in either 1 or 2, it shows the error "goto was unexpected at this time" How do I fix it?
Instead of using environment variables, try this:
Choice is a program that allows you to enter a number or y/n and returns an error code. %ERRORLEVEL% is a variable that holds the program's last error code.
You can do other comparisons, too.
Your
startchoice
isn't being set correctly. Use the alternate syntax forset /p
where you supply the prompt there (and remove the space betweenstartchoice
and the assignment (=
) operator - I think it's actually the cause of the problem, but you can reduce your batch file by a line if you use theset /p <variable>=<Prompt>
syntax).I added two targets for the
goto
, andecho
statements so you could see they were reached:You need quotes around the if comparison and it didn't like you using set / p without a prompt. The following works:
Another Reason it could be malfuntioning is that you have not included the SET /P M= Variable Correctly... There should not be a space between the m and the equals sign.