1.

Solve : Cant find what's going wrong with IF statements?

Answer»

Hey guys I'm back a second time with a new and exciting error ^^ *cry's deeply*. Anyway I think the IF statements are running even if the conditions are not met, but cant seem to track down exactly why or where I'm going wrong. The code is a little test for a combat phase in a small RPG game in the making in case anyone is interested . Now I know the code isn't all that efficient as it could be, but that's because I'm still  newish to batch files, so if there is any parts that could be made better just say . There is also a small problem when I target the second monster (%MonsterName2% goblin) it seems to take health off %MonsterName% instead of the second target like I wanted.
As I'm not sure what part is causing this and you will probably need the variables to re-create the error ill post all the  code I have so far.
Thanks in advanced!

The error:
after line Code: [Select]if /i %Target%==%MonsterName1% (
if %MonsterHealth1% LEQ 0 (
set MonsterName1=Dead
set MonsterHealth1=0
)
)I get:
        0 was unexpected at this time
        if       LEQ 0 (

Code: [Select]REM variables: -----------------------
set Agility=80
set Health=150
set Intellect=0
set Level=1
set MyVarName=Risen91
set Strength=150
set Xp=0
set Bot1Agility=30
set Bot1Health=130
set Bot1Intellect=250
set Bot1Level=1
set Bot1Name=Jessica
set Bot1Strength=50
set Bot1Xp=0
set Lvl2=100
set Lvl3=300
set Lvl4=600
set MonsterAgility=80
set MonsterHealth=150
set MonsterHealth1=150
set MonsterIntellect=0
set MonsterName=Rat
set MonsterName1=Goblin
set MonsterName2=-
set MonsterName3=-
set MonsterName4=-
set MonsterStrength=150
set /a MonsterLevel=%level%-1
set /a MonsterLevel1=%random%*3/32767+%MonsterLevel%
if %MonsterLevel1% Lss 1 (
set MonsterLevel1=1
)
cls
:MainMenu
REM Menu: --------------------
echo.
echo Fight
echo Spells
echo Inventory
echo Team
echo Run
echo.
echo.
Set /p menu=Choose a option:
if /i %menu%==Fight (
goto Fight
)
cls
echo Please choose a vailid option!
pause
cls
goto MainMenu
REM attack sub menu: -----------------------
:Fight
cls
echo.
echo Attack
echo Power Attack
echo.
set /p menu=Choose a option:
if /i %menu%==Attack (
goto Attack
)
REM Targeter: ---------------------
:Attack
cls
echo.
echo %MonsterName%
echo %MonsterHealth%
echo ------------------
echo %MonsterName1%
echo %MonsterHealth1%
echo ------------------
echo %MonsterName2%
echo %MonsterHealth2%
echo ------------------
echo %MonsterName3%
echo %MonsterHealth3%
echo ------------------
echo %MonsterName4%
echo %MonsterHealth4%
echo.
set /p Target=Choose a target to attack:
if /i %Target%==%MonsterName% (
goto Attack2
)
if /i %Target%==%MonsterName1% (
goto Attack2
)
if /i %Target%==%MonsterName2% (
goto Attack2
)
if /i %Target%==%MonsterName3% (
goto Attack2
)
if /i %Target%==%MonsterName4% (
goto Attack2
)
cls
echo Please choose a valid target!
pause
goto Attack
:loop
REM Damage: ------------------------
:Attack2
set /a hit=%random%%%10
set /a modifier=%random%%%2
if %modifier%==0 (
set mod=-
)
if %modifier%==1 (
set mod=+
)
set /a strnerf=%strength%/6
set /a dmg=%strnerf%%mod%%hit%
set /a critchance=%random%%%15
if %critchance%==0 (
set /a dmg=%dmg%+%dmg%
set critsay=1
) else (
set crit=0
set critsay=0
)
set /a miss=%random%%%15
if %miss%==0 (
set dmg=Attack Missed!
)
echo %dmg%
if %critsay%==1 (
echo Critical Hit!
)
if /i %Target%==%MonsterName% (
set /a MonsterHealth=%MonsterHealth%-%dmg%
echo %Target%s health=%MonsterHealth%
)
if /i %Target%==%MonsterName1% (
set /a MonsterHealth=%MonsterHealth1%-%dmg%
echo %Target%s health=%MonsterHealth1%
)
if /i %Target%==%MonsterName2% (
set /a MonsterHealth=%MonsterHealth2%-%dmg%
echo %Target%s health=%MonsterHealth2%
)
if /i %Target%==%MonsterName3% (
set /a MonsterHealth=%MonsterHealth3%-%dmg%
echo %Target%s health=%MonsterHealth3%
)
if /i %Target%==%MonsterName4% (
set /a MonsterHealth=%MonsterHealth4%-%dmg%
echo %Target%s health=%MonsterHealth4%
)

if /i %Target%==%MonsterName% (
if %MonsterHealth% LEQ 0 (
set MonsterName=Dead
set MonsterHealth=0
)
)
if /i %Target%==%MonsterName1% (
if %MonsterHealth1% LEQ 0 (
set MonsterName1=Dead
set MonsterHealth1=0
)
)
if /i %Target%==%MonsterName2% (
if %MonsterHealth2% LEQ 0 (
set MonsterName2=Dead
set MonsterHealth2=0
)
)
if /i %Target%==%MonsterName3% (
if %MonsterHealth3% LEQ 0 (
set MonsterName3=Dead
set MonsterHealth3=0
)
)
if /i %Target%==%MonsterName4% (
if %MonsterHealth4% LEQ 0 (
set MonsterName4=Dead
set MonsterHealth4=0
)
)

pause
PING 1.1.1.1 -n 1 -w 1000 >NUL
goto Attack
pause
exit
0 was unexpected at this time
        if       LEQ 0 (


See the blank? %MonsterHealth1% is expanding to nothing because it has no value, (investigate e.g. using echo) which breaks the IF line. The usual way to avoid this is to surround the comparison items with characters such as quotes.

if "%MonsterHealth1%" LEQ "0"


Hmm %MonsterHealth1% was defined in the variables listed at the top though? I cant see anything overwriting that value to blank :S

I cant remember how the error reporting works, but im pretty sure with echo off it only echo's the command if the line works. If that's the case then the next line %MonsterName2% has only been defined as - would this be whats causing it?Ah! *facepalm* your right I didn't define MonsterHealth2, 3 and 4 the quotations worked perfect thanks. Salmon Trout my Hero <3

All fixed now, turns out it was just me having a stupid moment and not adding in the right numbers (1,2,3,4) to the /set MonsterName in lines:
set /a MonsterHealth=%MonsterHealth3%-%dmg%
Thanks again!You have SPENT a lot of time on this. That is commendable. Surely you must also be good at some other programming language. Do you use Vb script?  Do you know Java?

The batch tool of DOS is very old and out of date. Microsoft has, for some time, been urging users to try something else.
What I am saying is this: that your energy and motivation would be more fruitful using anything other than pure batch. Compromise. Don't be a fanatic.

You cam incorporate a better language tool into simple batch files. The CScript.exe tool is a way of having Vb script power in a batch program.

Using the command-based script host (CScript.exe)

The link above provides very complete documentation of how to use the IF statement in the CScript language. With examples.

End of my rant. THANK you.
Thanks, I have done a small amount of Action Script in Flash but not much, and no this is the only language I know. This is only a fraction of the coding I have for the game, why this is just the basic attack sequence for combat, I haven't even got into Spells, advanced attacks, item drops, levels and statistics yet . I have got too far into this to change from batch files now, about 5-6 pages full of code like above hehe.
Although I see lots of people mentioning that batch is out of date I have not yet come across anything that I haven't been able to code, even if it is a less direct route.I am really interested in seeing the final product once completed. This really intrigues my interest Quote from: zeroburn on March 18, 2012, 01:56:37 AM

I am really interested in seeing the final product once completed. This really intrigues my interest

I'm afraid unless I have a team for this PROJECT, that it would take a very LONG time as at the moment I'm doing this in between university work. Appreciate your interest though and if I do ever get around to completing it ill be sure to send you a copy. I'm aiming for this to be a sort of Final fantasy/Diablo 2 game, text based of course .Have you tried a top-down approach?

Like this: Whenever you find some detail you can not handle, you just crate a 'stub' that fakes it. Later, you or someone else can fill in the details.

Kinda like an artist doing a sketch before he brings out the paint. Quote from: Geek-9pm on March 18, 2012, 02:06:52 PM
Have you tried a top-down approach?

Like this: Whenever you find some detail you can not handle, you just crate a 'stub' that fakes it. Later, you or someone else can fill in the details.

Kinda like an artist doing a sketch before he brings out the paint.

Hmm I'm not sure if I would like doing things that way, first off I'm a bit of a perfectionist when it comes to stuff like this and secondly I would have to fix the code at 1 time or another so it seems like I would be just giving myself more work to do, by not only having to scan through the code for 'stub' parts but writing them as well as the correct code later. I can see the reasoning behind that way of doing it though and I'm sure it would have its benefits at some point. Quote from: Risen91 on March 18, 2012, 02:29:16 PM
I'm a bit of a perfectionist when it comes to stuff like this

If you are a perfectionist, why are you writing a batch game? Unless it's an obsessive hobby thing, like making a model of the Taj Mahal out of matchsticks.

Beware. Somebody asked the Duke Of Wellington how he kept beating Napoleon's army, supposedly the best trained and most technically advanced force in Europe at the time. He compared his method of planning and executing a campaign with Napoleon's. He answered something like this: "Compare a miltary plan to a horses harness. Napoleon typical plan is like a very fine hand tooled leather harness, all the pieces very beautifully and precisely made, all fitting together perfectly. It all works well until something snaps and then it is useless. My typical plan is like one made out of assorted bits of string. If a bit breaks I just tie a knot and carry on".
Salmon Trout, great come back! 
Candidate for post of the month.mind = blown
And I started writing this batch game ages back to learn, since then its really just about I already have the code, know it well enough and am having fun with it and I think that's the most important thing, don't want to be learning a language I don't like much (E.g. Actionscript) even if batch is very outdated.The general idea now in computer programming is incremental refinement and crated packages.They call it by various names.

The concept is no avoid looking at a print out that covers the walls of two rooms. The main part of the code describes the logic of the program. Various packages do the hard work and are hidden from view. Until you want to see them.

Just an OBSERVATION.
For further reading:
http://c2.com/cgi/wiki?StructuredProgramming

Quote
"Structured programming" is programming that links control flow blocks (a function, an if statement's block, etc.) to the scopes of variables. A variable declared inside such a block is invisible outside it.
Structured Programming is a foundation of ModularProgramming and ObjectOrientedProgramming, as it's assumed that individual methods are structured (i.e., coded with only #1 to #3 above). (Of course, plenty of people write garbage in ObjectOrientedProgrammingLanguages.) (Yes, but it's structured garbage!) (Not necessarily, but it's encapsulated within the object.)

Three cheers for structured garbage!  you lost me at incremental refinement sorry Just been working on my script and come across something I'm struggling to get working right. basically I need a monster to have a random attack sequence, the variable names are %MonsterName% %Monstername1% and so forth up to 4 (giving a total of 5 in the group)  ill show you what I have been tinkering around with at the moment to see if I can spur any ideas (not that you need them )

Code: [Select]set /a modifier=%random%%%5
if %modifier%==0 (
set MonSeq=""
)
set MonSeq1=%MonsterName%%modifier%
)
:resetmon1
set /a modifier=%random%%%5
set MonSeq2=%MonsterName%%modifier%
if MonSeq2=%MonSeq% (
goto resetmon1
)
if MonSeq2=%MonSeq1% (
goto resetmon1
)
I was going to have it basically "Re-randomising" every time that %MonsterName% number was taken. This seems a really slow and process sapping way of doing it though, I need all 5 in a set order (don't matter if its pre-set or randomises it after the combat script where last monster has taken its hit) any ideas?

Edit: quick change to that script to make it more understandable


Discussion

No Comment Found