Page 1 of 1

Numbers in batch game code not making sense

Posted: 22 Jan 2019 16:08
by EliJamesMorey
I am having trouble with this line in a game that I am making. It is pretty simple, but for some reason the numbers aren't adding up.

if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo This chest is locked, and you do not have the key.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo 1) Leave.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo 2) Try to pick the lock.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" set /p c2=
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "1" goto monstore2
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" GTR "99" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" GTR "99" echo Success. You have picked the lock.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" GTR "99" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" GTR "99" pause
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" GTR "99" set /a storeval9=1
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" LEQ "99" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" LEQ "99" echo Failure. You have cannot pick the lock.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" LEQ "99" echo.
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" LEQ "99" pause
if "%storval9%" == "0" if "%c%" == "1" if "%keyval2%" == "0" if "%c2%" == "2" if "%plloc%" LEQ "99" goto chestopen1

So I think that the problem has to be with the numbers %plloc% and "99." Plloc is set to 145, and when I leave the value that it needs to be greater than at 99,
it always sends me to failure, but when I put it to 100, it always sends me to success. Do values of three digits or more require some other kind of notation? It seems like it's only reading the first two digits when numbers are greater than 99...

Forgive me if I'm not using any of the right terminology. I am entirely new to coding, and only know a tiny bit about batch and about coding in general.

Thank you so much in advance!

Re: Numbers in batch game code not making sense

Posted: 22 Jan 2019 17:31
by Ed Dyreen

Code: Select all

if +%storval9% EQU 0 if +%c% EQU 1 if +%keyval2% EQU 0 (

	echo.
	echo.This chest is locked, and you do not have the key.
	echo.
	echo.1) Leave.
	echo.2) Try to pick the lock.
	echo.
	set /P "c2="

	setlocal enableDelayedExpansion
	:: (
		if !c2! EQU 1 call :monostore2
		if !c2! EQU 2 if !plloc! GTR 99 (
			echo.
			echo.Success. You have picked the lock.
			echo.
			pause
			set /A storeval9 = 1
		) else (
			echo.
			echo Failure. You have cannot pick the lock.
			echo.
			pause
			call :chestopen1
		)
	:: )
	endlocal
)
echo.The End.
pause

:monostore2
:chestopen1
exit /B

Re: Numbers in batch game code not making sense

Posted: 22 Jan 2019 17:34
by ShadowThief
Using quotes with GTR (and LSS, GEQ, LEQ, and so forth) forces a string comparison instead of a numeric comparision. 9 is greater than 1, so any number starting with 9 will be considered larger than any number starting with 1. Remove the quotes for the last comparison.

Re: Numbers in batch game code not making sense

Posted: 22 Jan 2019 17:56
by EliJamesMorey
Thank you so much shadow thief! It's great to know that, that is going to save me a lot of pain in the future.