Home > Mobile >  Making variables have letters in CMD Batch
Making variables have letters in CMD Batch

Time:09-18

So I wanna make a language system for my game, and so I want to use variables for that. But I can't, because I can't make variables have letters. Can anyone help me with that? Instead of showing what's in the variable it just shows "0". Here's the code.

:selectlanguage
cls
echo SELECT LANGUAGE
echo 1.English
echo 2.Espanol
echo 3.Francais
echo 4.Italiano
echo 5.Deutsch
set /p language=
if %language%==1 goto setenglish
if %language%==2 goto setspanish
if %language%==3 goto setfrench
if %language%==4 goto setitalian
if %language%==5 goto setgerman

:setenglish
cls
set /a loadingtitle=Loading
set /a welcometitle=Welcome to the Whitefield Street
set /a pressanykeytoplay=Press any key to play
echo Offset Selected Language: English >>lang.c
goto loading

:loading
cls
echo %loadingtitle% %percent%...
ping localhost -n 2 >null
set /a percent =9
goto loading0

:loading0
cls
echo %loadingtitle% %percent%...
ping localhost -n 3 >null
set /a percent =17
goto loading1

:loading1
cls
echo %loadingtitle% %percent%...
ping localhost -n 3 >null
set /a percent =34
goto loading2

:loading2
cls
echo %loadingtitle% %percent%...
ping localhost -n 2 >null
set /a percent =40
goto entry

:entry
cls
echo %welcometitle%
echo.
set /pause=%pressanykeytoplay%
goto entryintro

:entryintro
cls
pause

CodePudding user response:

As already mentioned in the comments by @Stephan, you cannot use set /a to set a string as variable. /a is arithmetic, like you're doing with setting the percentage. However, I would not use set /p as a choice menu, instead use choice.exe. Here is an example:

@echo off & set cnt=
:selectlanguage
cls
set "langs=English Espanol Francais Italiano Deutsch"
for %%i in (%langs%) do (
    set /a cnt =1
    call echo %%cnt%% %%i
)

choice /c 1235 /m "Select Language:"
goto lang%errorlevel%

:lang1 English
set "loadingtitle=Loading
set "welcometitle=Welcome to the Whitefield Street"
set "pressanykeytoplay=Press any key to play"
(echo Offset Selected Language: English)>>lang.c
rem other Enlich items here
goto :EOF

:lang2 Spanish
rem Spanish variables are set here.. etc.
goto :EOF

:lang3 French
rem French variables are set here.. etc.
goto :EOF

:lang4 Italian
rem Italian variables are set here.. etc.
goto :EOF

:lang5 German
rem German variables are set here.. etc.
goto :EOF

CodePudding user response:

A slight expansion to @Gerhard's answer (which adapts the menu automatically to the number of languages, but does not also adjust the choice command). On top, to keep the code more readable, the labels are the actual languages, instead of lang<n>

@echo off
setlocal 
set "choices=123456789"
set "langs=English Espanol Francais Italiano Deutsch"
for %%i in (%langs%) do (
    set /a cnt =1
    call set "lang[%%cnt%%]=%%%%i"
    call echo %%cnt%% %%i
)
call set choices=%%choices:~0,%cnt%%%
choice /c %choices% /m "Select Language:"
call goto :%%lang[%errorlevel%]%%
goto :eof

:Deutsch
echo Deutsche Variablen
goto :eof

: ... etc

I personally would have worked with delayed expansion, but for this, I kept Gerhard's decision to do without.

Note: if you need more than 9 languages, just use letters instead of numbers (set "choices=ABCDEFGHIJKLMNOPQRSTUVWXYZ") or combination (set "choices=123456789ABCD...").
Anything left to do for another language is adding it to set /langs and add the subroutine.

  • Related