I am making a text based rpg and I'm struggling with a specific kind of if statement, I'm not inclined to share the exact code as It's got a lot of content in it I have made over the past 5 years that I hope to get a copyright on. (to clarify, I'm regarding the artwork, music and storyline I created myself)
Example of the Code:
set choice=00
:top
cls
echo What would you like to do?
echo 1) gain 10 points
echo 2) do something else
set /p choice=Your Choice:
if %choice%==1 goto :options
if %choice%==2 goto :something-else
if %choice%==00 goto :end
:options
set /a opt1=10
set /a opt2=10
if %opt1%==%opt2%-5 goto :nxt
set /a opt1 =10
goto :top
:nxt
cls
echo You can't do that!
echo/
echo Press Enter to Continue
pause >nul
goto :top
:something-else
exit
:end
exit
The problem is, my process is ignoring this if statement completely. From my perspective opt2-5 should = 5 therefore forbidding the action being attempted, which is to add 10 to opt1, instead it skips the if and adds 10 anyways. This type of equation has worked for me in other areas thus far, I don't understand why it isn't working now. The only difference is the other code had the variables defined completely, ex: opt1=10 opt2=5, if opt2=5 then echo you can't do that.
CodePudding user response:
To perform a comparison of an expression in batch, you can use Set /A
with an expression designed to evaluate as 0 if false in conjunction with &&
or ||
conditional operators to implement if else logic.
IE:
@Echo off
:l
Set /A A=5,B=2
Set /P "C=Value for C: "
2> nul Set /A "D=B C","1/(A-D)" && (
Cmd /V:On /C "Echo(!A! ^!= !D!"
) || (
Cmd /V:On /C "Echo(!A! == !D!"
)
Goto:l