0

I'm making a batch file game (I know it's not really for games). I need to run set /p to ask the user to choose which option to do while it adds a certain amount to their in game money every second, but I can't figure out how to run it at the same time.

I've tried using it in a for loop, but I can't get it to work.

Here is an example:

:do
::echo choices and ask user which option to do 
echo choice 1
echo choice 2
echo choice 3
set /p choice= what to do: 
if "%choice%" == "1" goto c1
if "%choice%" == "2" goto c2
if "%choice%" == "3" goto c3
::add money
set /a "money=%money%+10"
::wait one second
ping localhost -n 2 >NUL
::do it all again
goto do

I want it to give them money even if they don't choose an option, so they get their money every second.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
beta23
  • 31
  • 7
  • is calculating the time after `set /p` an option or do you need to show it in realtime? – Stephan May 10 '19 at 17:53
  • real-time would be better – beta23 May 10 '19 at 17:57
  • hm - that's not easy. `cmd` doesn't support that. But there was a trick with `xcopy`. Let's see, if I can find it again... – Stephan May 10 '19 at 18:06
  • Aside from your issue, you should use `Choice /C 123 /M "What to do"` instead of `set /p choice= what to do:`, _then you could replace your `if` lines with `GoTo c%ErrorLevel%` or similar_. For a `1` second break, you should also use `Timeout /T 1 /NoBreak >Nul` instead of `ping localhost -n 2 >NUL` too! – Compo May 10 '19 at 18:06
  • [Found it](https://stackoverflow.com/questions/18942356/batch-file-choice-syntax) – Stephan May 10 '19 at 18:15
  • @Stephan ok i,ll try it – beta23 May 10 '19 at 18:25
  • I agree with everything written by Compo. See [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) It explains very detailed why usage of `choice` is better than `set /P` for simple choice menus. Run also in a cmd window `set /?` and read the output help, especially the section about usage of `set /A` for evaluation of an __arithmetic expression__. `set /A money=money+10` is better and best is `set /A money+=10` because of working also always inside a command block. – Mofi May 11 '19 at 08:22
  • Run in a cmd window also `choice /?` to get help on this executable. It can be read that `choice` can automatically select an option after a specified time like one second. So you can add a fourth option which the batch file user does not know and which is chosen by `choice` after a second to increment the money and show the menu once again. An alternate method is getting current time before prompting the user and then wait for user decision. After user has made decision, get once again current time, calculate the difference in seconds and increment money value according to seconds difference. – Mofi May 11 '19 at 08:33
  • Well, there is no `for` loop in your code as you claim. Anyway, you simply can't execute other code as long as the batch file awaits user input, unless you establish a parallel process (with [`start`](https://ss64.com/nt/start.html)). Another and easier option is to check the time difference after and before the user prompt... – aschipfl May 11 '19 at 16:22

1 Answers1

1

You may have to use CHOICE because SET /P will read user input until you gives it a new line.

CHOICE has a parameter /T, which can tell you after n seconds will select default choice (use /D <default key> to set default choice). For example, choice /c 123y /t 1 /d y will wait input for 1 second, and only select the last choice (which is y) if user does not pressing anything for 1 second. You can also display custom prompt message by adding /M "<message>", so your complete command is:

choice /c 123y /t 1 /d y /m "what to do:"

%ERRORLEVEL% is where CHOICE command gives output. It's starts from 1 to the last character index (in this case, y will be 4):

if "%errorlevel%"=="1" goto c1
if "%errorlevel%"=="2" goto c2
if "%errorlevel%"=="3" goto c3
rem The last one, which is 4, but because it's the only last option, so you don't need another if statement

And in the end of the loop, you may want to add money:

rem Instead of "set /a money=%money% + 10", why you don't use += instead?
set /a money+=10
goto theBeginOfTheLoop

Combine those statements together, we'll have this simple goto loop:

:theBeginOfTheLoop
echo money: %money%
echo choice 1 [1]
echo choice 2 [2]
echo choice 3 [3]
choice /c 123y /t 1 /d y /m "what to do: "
if "%errorlevel%"=="1" goto c1
if "%errorlevel%"=="2" goto c2
if "%errorlevel%"=="3" goto c3

set /a money+=10
goto theBeginOfTheLoop

Have a nice day with Batch :)

an tran huu
  • 93
  • 2
  • 7