Search code examples
goto

Goto was unexpected at this time - batch


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?


Solution

  • Your startchoice isn't being set correctly. Use the alternate syntax for set /p where you supply the prompt there (and remove the space between startchoice 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 the set /p <variable>=<Prompt> syntax).

    I added two targets for the goto, and echo statements so you could see they were reached:

    :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
    set /p startchoice=Enter your choice:
    
    if %startchoice%==1 goto changes
    if %startchoice%==2 goto startgame
    :changes
    echo Changes
    goto end
    :startgame
    echo StartGame
    :end