Saved Bookmarks
| 1. |
Solve : Why won't this add up right???? |
|
Answer» HI guys, Can anyone tell me why I can't get this to add up right?? It should be a simple set /a answer=%num1% - %num2% I've posted the whole batch file n case I've carried somthing over the ball'sed it up.I've also put ***** next to the line to make it clearer. Code: [Select]@echo off title Frozen h**l color 4f cls cd "C:\dos game\missions" set name=%1 :Mines set errm=Make your selection; :Mmenu cls echo. echo. ==== Frozen h**l - The Mine ==== echo. echo. As you desend into the darkness of the well, on the ladder, one of echo. the rungs break so you move a bit quicker. echo. You reach the floor of the well you find a gas lantern. You pull a echo. lighter from inside in your jacket and light the lantern. echo. echo. As the light fills the well you can see two tunnells leading away into echo. the DISTANCE, one to your right and one straight ahead echo. echo. The tunnell to your right has a track on the floor that looks like it was echo. used for a mine train, but there are no carriages that you can see. echo. echo. The tunnell straight ahead is very narrow, as you take a closer look you echo. can see light at the end of it. echo. %errm% echo. echo. a. Return back to the Ice base echo. echo. b. Take the tunnel to the right echo. echo. c. Take the tunnel in front of you echo. set /p ansm="Make your selection: " if /i "%ansm%" == "a" CALL level1.bat %name% if /i "%ansm%" == "b" goto mb if /i "%ansm%" == "c" goto mc if "%ansm%" == "" set errm=Invaild selection. Please try again && goto Mmenu :mb echo. %ammo% - ammo echo. %health% - health echo. %weapon% - weapon echo. %name% - Player name echo. pause rem Set /a Number=(%Random% %%10)+1 set number=6 set /a loss=15 if /i "%number%" GEQ "5" ( set mess=You have tripped on a loose rail track. You have lost 15%% health!! ***** set /a nhealth=%health% - %loss% ***** echo nhealth=%health% - %loss% // should be 100 - 15 echo newh - %newh% // should be 85 set mess2=You currently have %newh%%% left set health=%nhealth% del "C:\dos game\save data\%name%\Playerinfo.ini" echo Frozen h**l - Player Data >>"C:\dos game\save data\%name%\Playerinfo.ini" echo [Player Information] >>"C:\dos game\save data\%name%\Playerinfo.ini" echo %name% >>"C:\dos game\save data\%name%\Playerinfo.ini" echo. >>"C:\dos game\save data\%name%\Playerinfo.ini" echo [Level] >>"C:\dos game\save data\%name%\Playerinfo.ini" echo 1 >>"C:\dos game\save data\%name%\Playerinfo.ini" echo. >>"C:\dos game\save data\%name%\Playerinfo.ini" echo [Health] >>"C:\dos game\save data\%name%\Playerinfo.ini" echo %health% >>"C:\dos game\save data\%name%\Playerinfo.ini" echo. >>"C:\dos game\save data\%name%\Playerinfo.ini" echo [Ammo] >>"C:\dos game\save data\%name%\Playerinfo.ini" echo %ammo% >>"C:\dos game\save data\%name%\Playerinfo.ini" echo. >>"C:\dos game\save data\%name%\Playerinfo.ini" echo [Current Weapon] >>"C:\dos game\save data\%name%\Playerinfo.ini" echo %weapon% >>"C:\dos game\save data\%name%\Playerinfo.ini" echo. >>"C:\dos game\save data\%name%\Playerinfo.ini" ) set newhm=%health:~0,1% if "%newhm%" == "-" call game_over.bat %name% health %health% echo %newhm% - newhm pause set errm=Make your selection :menu2 cls echo. echo. ==== Frozen h**l - The Mine ==== echo. echo. echo. ** turning right, down tunnell with no light ** echo. echo. echo. echo.%number% GEQ 5 = trip echo. echo. echo. echo. %mess% echo. echo. %mess2% echo. echo. %errm% echo. echo. a. Return back to the base of the ladder echo. echo. b. continue down the dark tunnell echo. echo. echo. set /p ansm="Make your selection: " if /i "%ansm%" == "a" goto mines if /i "%ansm%" == "b" goto 2b :mc echo mc pause exit :2b echo 2b pause exit When I run it, it does everything it should but not the caluclation. the nre varaiable newh is blank. cheers in advance!!Quote Code: [Select]set /a nhealth=%health% - %loss% ***** Well, if you are setting the variable %nhealth% to be %health% - %loss%, and %health% equals 100, and %loss% equals 15, I should expect the variable %nhealth% to equal 85, and I would expect the variable %newh%, which seems to have appeared from nowhere, to be equal to nothing at all (blank). Moral: read your code carefully! hi, I see the mistake, I've corrected it but it hasn't helped. I'm stilling getting nothing in the varaible %nhealth% code now in batch file.. Code: [Select] set /a nhealth=%health% - %loss% echo nhealth=%health% - %loss% // should be 100 - 15 echo nhealth - %nhealth% // should be 85 Cheers for your help.Quote from: blastman on June 29, 2008, 06:07:08 AM hi, echo nhealth - %nhealth% // should be 85 that just leaves nothing. This code Code: [Select] @echo off set /a health=100 set /a loss=15 set /a nhealth=%health% - %loss% echo nhealth=%health% - %loss% // should be 100 - 15 echo nhealth - %nhealth% // should be 85 produces this output Code: [Select]nhealth=100 - 15 // should be 100 - 15 nhealth - 85 // should be 85 Are %nhealth% and %loss% both getting values? yeah, in my code, %loss% is set just before the if statement and %health% has been set from a .ini file in the pervious bacth file (this one was "called" so the varaibles carries over) I reakon the problem might be that when I pull the health ammount in I just "set" it and not "set /a" I'll mod my code now and have a look. Back in bit..... [EDIT;] Na, that hasn't made any difference. I'm gonna post the batch file again, see if anyone can spot the problem.... (like a varaible that means nothing!!!)Right, I've taken the sum code out of the if statement and it works!! I'm gonna reorganise this batch file and post back. hopefully with a working solution!!! Code: [Select]if /i "%number%" GEQ "5" ( set /a nhealth=%health% - %loss% echo nhealth=%health% - %loss% // should be 100 - 15 echo newh - %newh% // should be 85 set mess2=You currently have %newh%%% left set health=%nhealth% ) This is your problem. Just like in a FOR loop, inside the brackets of this IF statement you need to use DELAYED expansion, as %nhealth%, %mess2%, %health%, cannot be used inside the brackets otherwise when you say "delayed expansion" what do you mean?? (so i can understand whats happening) I've now moved everything that happens to a label instead of inside the IF's () cheersI was going to suggest ditching the if with brackets structure and using a jump to a label, but you have beat me to it! You can Google for a more complete explanation of delayed expansion but I will attempt a quick run through here. Basically, when you run a batch file, at run time, cmd.exe expands all variables into their values. You can see this happening if you don't use @echo off at the beginning. So if you had Code: [Select]set var=cat echo %var% you would see Code: [Select]set var=cat echo cat cat OK so far? Now, at runtime, cmd.exe does not expand variables that are in Code: [Select]( brackets like this ) which you find in FOR loops or IF multiline structures like the one in your batch file. Until Windows 2000, the NT batch language was thus crippled. Windows 2000 introduced delayed expansion. You enable it with this statement Code: [Select]setlocal enabledelayedexpansion and you use exclamation signs instead of percent signs for variables created in the brackets. You can use percent signs for them again after the brackets. This won't work the way you want. the variable %cry% will be blank. Code: [Select]set animal=cat if "%animal%"=="cat" ( set cry=meow echo The cry of the %animal% is a %cry% ) The result will be Code: [Select]The cry of the cat is a The variable %animal% is expanded OK because it was set before the brackets, but %cry% is blank. However, this will work. Code: [Select]setlocal enabledelayedexpansion set animal=cat if "%animal%"=="cat" ( set cry=meow echo The cry of the %animal% is a !cry! ) Result will be Code: [Select]The cry of the cat is a meow perfect, I understand completely. Thanks for that. you da man!! |
|