Page 1 of 1

Setting variable limits

Posted: 08 Aug 2013 20:49
by SEOS
Hello everyone, I am programming an rpg orientated game.
I know how to set variables and change them, I just don't know how to make them have limits.
for example:
if %health% == 0 goto death
This wont work most of the time because enemies do different damage to health that could make it become a negative number such as -1 or -4
and the if statement wont notice it.

So how could I make so if health equals 0 and under it goes to death?

Re: Setting variable limits

Posted: 08 Aug 2013 20:56
by Magialisk
GTR = Greater than
GEQ = greater or equal
LEQ = less or equal
LSS = less than
Use them as: 'IF %health% LEQ 0 GOTO:death'

For more help on IF logic type 'IF /?'. There are several other useful flags such as 'NOT' and '/I' that you'll probably eventually want to use :)

Re: Setting variable limits

Posted: 09 Aug 2013 11:22
by SEOS
I also thought it was the else command you use.
Maybe not, but thanks a bunch I can now fix alot of errors!

Re: Setting variable limits

Posted: 09 Aug 2013 11:36
by Magialisk
YOu can use ELSE in the following way:

Code: Select all

IF condition==true (
   do stuff
) ELSE (
   do other stuff
)

I don't know if I'm just cursed or what, but the cmd parser tends to go crazy sometimes when I use ELSE, complaining about parenthesis and/or the word ELSE being unexpected. It happens sporatically and it's never actually a mismatched paren or something I can "fix", I can never quite get to the bottom of it.

Sometimes I can take the exact code as written above and reformat it as "IF condition==true (do stuff) ELSE (do other stuff)" and that will make the errors go away, other times it's just the opposite I have to break up a one-liner into the format shown up top to make it work. In very rare cases it won't work at all with an ELSE statement so I have to use "IF NOT condition==true" instead. I know this sounds rediculous, but I swear it's been an issues for me. I never know what to expect from the ELSE command, so I just have to play it by ear every time I write one...

Re: Setting variable limits

Posted: 09 Aug 2013 12:24
by Squashman
You will get the ELSE error when you don't have a space between the right paren and the ELSE.
)else (
) else (

Re: Setting variable limits

Posted: 09 Aug 2013 15:02
by Magialisk
Thank you squashman but I know that's not the cause of my particular curse. Is it possible the error could be caused by too many spaces and/or a CRLF between the right paren and the ELSE?

I tend to get the error most often when trying to do something like this:
IF condition==true (do this stuff) ELSE (do something else)
or like this:
IF condition==true (do this stuff)
ELSE (do something else)

I like the formatting above because it's clean and concise for short 1-2 liner IF statements As I said before though I tend to sporatically have issues with the ELSE. DO you think it could be related to extra whitespace to the right of the ')' before the CRLF character? How about too many spaces due to indenting the ELSE?

99% of the time when one of the formats above gives me problems it ends up working perfectly if I spread it back out to the "normal" structure outlined in the earlier post, with ") ELSE (" on its own line... Beats me! :)

Re: Setting variable limits

Posted: 09 Aug 2013 19:32
by carlsomo
Sometimes if the else will bomb at the if statement. If a variable has spaces or potentially does you have to quote both sides:

Code: Select all

C:\>set "variable=My space"
C:\>set "whatever=My space"
C:\>If %variable%==%whatever% (echo equal) else (echo not equal)
C:\>space==My was unexpected at this time.

So use quotes:

Code: Select all

C:\>set "variable=My space"
C:\>set "whatever=my space"
C:\>If "%variable%"=="%whatever%" (echo equal) else (echo not equal)
C:\>not equal
C:\>if /i "%variable%" equ "%whatever%" (echo equal) else echo not equal
C:\>equal

Re: Setting variable limits

Posted: 09 Aug 2013 20:59
by Magialisk
Thanks carlsomo. Someone else actually PMd me a potential answer to my specific issue, though I still swear I've had more issues with ELSE than about any other command in the whole set. No matter how I try to clean up my code once it's working it's always the ELSE clauses throwing syntax errors when I move them around.

My primary issue seemed to come from trying to use a format like the following:

IF condition==true (do something)
ELSE (do something else)

As I should have realized, this gets interpreted by the parser as an IF statement by itself, and then on the next line an ELSE statement with no IF. I'd have been fine if I added %\n% to the end of the IF for line continuation (only if that variable is defined of course, but I usually have at least one macro around so it's always defined). I'd probably also be fine if I dropped the closing paren on the IF down to the start of the next line. Either way, if I wasn't trying to be clean and fancy and just wrote out the expanded code every time I wouldn't cause myself these problems :oops:

In any case