Comparing two variables to a number?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Comparing two variables to a number?

#1 Post by ProjectConquest » 14 Jan 2018 23:01

Hello everyone thank you for taking the time to read this! Anyways I've been working on a game in batch for awhile now and within this game you can craft things from mob drops. For example a bandage is crafted from 2x flesh. This is the code used to get that to work:

Code: Select all

:ItemBandage
cls
Echo.
echo Are you sure you would like to craft a bandage?
echo.
echo 1) Yes
echo.
echo 2) No
set /p input=
if %input% == 1 goto ItemBandageCheck
if %input% == 2 goto Craft

:ItemBandageCheck
cls
echo.
if %flesh% GEQ 2 goto ItemBandageCraft
if %flesh% LSS 2 goto ItemBandageError

:ItemBandageCraft
cls
echo.
set /a flesh-= 2
set /a bandages+= 1
echo You have succesfully crafted a bandage!
pause >nul
goto Craft

:ItemBandageError
cls
echo.
echo You do not have the required materials to craft this item
pause >nul
goto Craft
This code checks to make sure the player has 2x flesh then crafts the bandage and deletes 2 flesh.

My problem arises when I want something crafted from 1x bandage and 1x teeth. These values are stored in two different variables so I'm unsure how to compare it to a number. I tried to do:

Code: Select all

:CraftHealth5
cls
echo.
echo Are you sure you would like to craft a health potion(+5)?
echo -----------------------------------------------------------
echo 1) Yes
echo.
echo 2) No
set /p input=
if %input% == 1 goto CraftHealth5Check
if %input% == 2 goto Craft

:CraftHealth5Check
cls
if %bandages% && %flesh% GEQ 1 goto CraftHealth5Complete
if %bandages% && %flesh% LSS 1 goto CraftHealth5Error

:CraftHealth5Complete
cls
set /a potion1= HealthPotion(+5)
echo You have succesfully crafted a health potion (+5)
pause >nul
goto Craft

:CraftHealth5Error
cls
echo.
echo Error in crafting item, check you have the right amount of materials!
pause >nul
goto craft
That didn't work. Then I tried:

Code: Select all

:CraftHealth5
cls
echo.
echo Are you sure you would like to craft a health potion(+5)?
echo -----------------------------------------------------------
echo 1) Yes
echo.
echo 2) No
set /p input=
if %input% == 1 goto CraftHealth5Check
if %input% == 2 goto Craft

:CraftHealth5Check
cls
if %bandages% GEQ 1 goto CraftHealth5Complete.1
if %bandages% LSS 1 goto CraftHealth5Error

:CraftHealthComplete.1
cls
echo.
if %teeth% GEQ 1 goto CraftHealth5Complete

:CraftHealth5Complete
cls
set /a potion1= HealthPotion(+5)
echo You have succesfully crafted a health potion (+5)
pause >nul
goto Craft

:CraftHealth5Error
cls
echo.
echo Error in crafting item, check you have the right amount of materials!
pause >nul
goto craft
So can you work around this somehow? And also what if there was a recipe for comparing two different variables to do different numbers? Any help is greatly appreciated!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Comparing two variables to a number?

#2 Post by aGerman » 16 Jan 2018 05:24

There is no logical AND or OR linkage for comparisons in batch.
Working around AND is quite simple though:

Code: Select all

if %bandages% GEQ 1 if %flesh% GEQ 1 goto CraftHealth5Complete
OR is a little more complicated. Usually a helper variable is in place to perform it:

Code: Select all

set "helper="
if %bandages% GEQ 1 set "helper=1"
if %flesh% GEQ 1 set "helper=1"
if defined helper goto CraftHealth5Complete
Another possibility is the errorlevel workaroud described here:
viewtopic.php?f=3&t=4308

Steffen

ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Re: Comparing two variables to a number?

#3 Post by ProjectConquest » 16 Jan 2018 07:52

Thank you, I posted this question to a few other forums and a mix of their answers along with yours helped me solve my problem with this code:

Code: Select all

if %bandages% GEQ 1 (
if %teeth% GEQ 1 (
goto :CraftHealth5Complete
)
)
So, thank you!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Comparing two variables to a number?

#4 Post by aGerman » 16 Jan 2018 08:47

That's exactly the same method as in my first suggestion :wink:

Steffen

Post Reply