0

I just want to ask, where is problem in this script. Every time i try to open mns fight, it just crashes somewhere on if %lokace%==2 if %vyber2%==mns set lokace=3 set monstrum=1 (btw, it's Czech game, so just ignore names)

Whole code:

:hra
cls
if %lokace%==0 echo Vesnice-domov
if %lokace%==1 echo Vesnice-namesti
if %lokace%==2 echo DabluvLes
if %lokace%==3 goto boj
echo mas %zivoty% hp
echo -----------------------------------
echo Co chces udelat?
if %lokace%==0 echo Jit na namesti [1]
if %lokace%==1 echo Jit domu [0]
if %lokace%==1 echo Jit do lesa [2]
if %lokace%==2 echo Najit monstrum [mns]
if %lokace%==2 echo Jit do vesnice [1]
if %lokace%==0 echo Vyspat se [spt]
set /p vyber2=Vyber si:
if %lokace%==1 if %vyber2%==0 set lokace=0
if %lokace%==0 if %vyber2%==1 set lokace=1
if %lokace%==1 if %vyber2%==2 set lokace=2
if %lokace%==0 if %vyber2%==spt if %zivoty% LSS %maxzivoty% set /a zivoty=%zivoty%+1
if %lokace%==2 if %vyber2%==mns set lokace=3 set monstrum=1
if %lokace%==2 if %vyber2%==1 set lokace=1
goto hra
:boj
if %monstrum%==1 set monstrumdmg=1  set monstrumhp=10
if %monstrumhp%==0 set lokace=2 set monstrum=0 goto hra
if %zivoty%==0 set lokace=0 goto hra
if %monstrumhp% GTR 0 if %monstrum%==1 echo Pavouk
echo Monstrum ma %monstrumhp% hp
echo Mas %zivoty% hp
echo Boj. Mas utok %damage% [boj]
echo Utek [utk]
set /p fight=Vyber si:
if %fight%==boj if %monstrum%==1 set /a monstrumhp=%monstrumhp%-%damage% set /a zivoty=%zivoty%-%monstrumdmg%
if %fight%==utk set lokace=2 set monstrum=0 goto hra
goto boj

3 Answers 3

1
set lokace=3 set monstrum=1

should be

set lokace=3&set monstrum=1

otherwise, lokace is set to 3 set monstrum=1

(there seems to be a number of such errors in the code)

Sign up to request clarification or add additional context in comments.

1 Comment

I found my own way of repairing this, but this is easier, thanks.
1

Your problem: set lokace=3 set monstrum=1 sets one variable named lokace with the value 3 set monstrum=1

Possible solutions:

a) concatenate commands with a &:

set lokace=3&set monstrum=1

(a better (safer) syntay (to avoid stray spaces) would be set "lokace=3" & set "monstrum=1")

b) use code blocks:

if %lokace%==2 if %vyber2%==mns (
  set "lokace=3"
  set "monstrum=1"
)

c) set /a can set several variables at the same time (pure set can't), but all values have to be integers:

if %lokace%==2 if %vyber2%==mns set /a lokace=3, monstrum=1

Same problem with for example set lokace=0 goto hra

Speaking about safety: you should enclose both sides of an if comparison in quotes to avoid syntax error with empty/undefined variables:

if "%lokace%"=="2" if "%vyber2%"=="mns" ...

1 Comment

One advantage of option c), Setting multiple numeric values with a single SET /A is significantly faster then multiple SET statements. Not an issue here, but it can be important if dealing with many SETs in a tight loop.
0

Example for study:

:hra
ClS
If "%lokace%"=="3" GoTo boj
If "%lokace%"=="0" Echo Vesnice-domov
If "%lokace%"=="1" Echo Vesnice-namesti
If "%lokace%"=="2" Echo DabluvLes
Echo mas %zivoty% hp
Echo -----------------------------------
Echo Co chces udelat?
If "%lokace%"=="0" (Echo Jit na namesti [1]& Echo Vyspat se [spt])
If "%lokace%"=="1" (Echo Jit domu [0]& Echo Jit do lesa [2])
If "%lokace%"=="2" (Echo Najit monstrum [mns]& Echo Jit do vesnice [1])
Set /P "vyber2=Vyber si: "
If "%lokace%%vyber2%"=="10" Set "lokace=0"
If "%lokace%%vyber2%"=="01" Set "lokace=1"
If "%lokace%%vyber2%"=="12" Set "lokace=2"
If /I "%lokace%%vyber2%"=="0spt" If %zivoty% Lss %maxzivoty% Set /A zivoty+=1
If /I "%lokace%%vyber2%"=="2mns" Set /A lokace=3,monstrum=1
If "%lokace%%vyber2%"=="21" Set "lokace=1"
GoTo hra

:boj
If "%monstrum%"=="1" Set /A monstrumdmg=1,monstrumhp=10
If "%monstrumhp%"=="0" (Set /A lokace=2,monstrum=0 & GoTo hra)
If "%zivoty%"=="0" (Set "lokace=0" & GoTo hra)
If %monstrumhp% Gtr 0 If "%monstrum%"=="1" Echo Pavouk
Echo Monstrum ma %monstrumhp% hp
Echo Mas %zivoty% hp
Echo Boj. Mas utok %damage% [boj]
Echo Utek [utk]
Set /P "fight=Vyber si: "
If /I "%fight%%monstrum%"=="boj1" Set /A monstrumhp-=damage,zivoty-=monstrumdmg
If /I "%fight%"=="utk" (Set /A lokace=2,monstrum=0 & GoTo hra)
GoTo boj

1 Comment

Thanks, you gave me another way how to make this. I already found some myself. But thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.