I'm coding a simple game and needed help,
pcash = user cash
reshp1 = cost to restore hp
php = player health
How do I write these into 1 line?
If user press 1, check if cash is equal or greater to the cost of restore hp, if it is greater - subtract cash base on cost of restore hp, then finally add the hp by 20%.
This is what I have tried, but did not work as expected.
if "%restorer%"=="1" if %pcash% geq %reshp1c% && set /a pcash=%pcash% - %reshp1c% && set /a php=%php% * .20
if "%restorer%"=="1" if %pcash% geq %reshp1c% set /a pcash=%pcash% - %reshp1c%&set /a php=%php%*6/5
& is used to separate cascaded statement. Batch uses integer mathematics, so *6/5 will multiply by 6 then divide by 5, adding 20% to the prior value (your code, had it worked, would have set the value to 20% of its prior magnitude)
Related
Here is my sample batch file code and I really don't know what it does.
set TEMPRPSW=%RPSW_VERSION%
set RELVER=%TEMPRPSW:~0,4%
set RELVER=%RELVER:.=%
if %RELVER% GEQ 30 goto :eof
Please give me a working sample.
That takes a 4 character long substring of TEMPRPSW, starting from character 0.
Meaning, it takes the first 4 characters of TEMPRPSW and puts them in RELVER.
set TEMPRPSW=abcdef
set RELVER=%TEMPRPSW:~0,4%
echo %RELVER% -> will print abcd
%VAR:str=% removes str
set RELVER=123.456
set RELVER=%RELVER:.=%
echo %RELVER% -> will print 123456 with no .
here is a nice article: https://www.dostips.com/DtTipsStringManipulation.php
This question already has an answer here:
Windows batch file IF failure - How can 30000000000000 equal 40000000000?
(1 answer)
Closed 5 years ago.
I have a very silly issue but unable to figure out what is wrong
I'm doing a basic level number comparison in if statement. script is pasted below. geq-greater than or equal to is working fine but gtr-greater than has a problem. Is there any limitation for numbers in gtr comparison?
if 3 gtr 2 (#echo "greater") else (#echo "lesser")
greater
if 3 geq 2 (#echo "greater") else (#echo "lesser")
greater
if 135149772801 gtr 111110000000 (#echo "greater") else (#echo "lesser")
lesser
if 135149772801 geq 111110000000 (#echo "greater") else (#echo "lesser")
greater
Is there any limitation for numbers in gtr comparison?
Yes, there really is.
You're limited to 32-bit signed integers and, if there's overflow, it's set to the maximum value.
Since both those numbers, 135149772801 and 111110000000, are too big, they're both converted to 231 - 1.
That's why it says the first is not greater (which is subtly different to lesser, by the way) but it is greater than or equal to.
More details can be found at the excellent dostips site.
A way around the 32-bit limit is to prefix the numeric-string with a goodly number of 0 and then use an alphabetic comparison on the last n characters.
Hence
set "num1=135149772801"
set "num2=111110000000"
set "zeroes=00000000000000000000000000000000000000000000000000000"
set "comp1=%zeroes%%num1%"
set "comp2=%zeroes%%num2%"
if "%comp1:~-20%" gtr "%comp2:~-20%" (echo greater) else (echo not greater)
where "-20" above means "take the last 20 characters"
A batch-file natively supports 32-bit signed integer arithmetic only. If the number goes over 2147483648, it overflows and warp back to negative number. However in if statements, the number gets clutter back to 2^31-1.
Code | Code Result | Actual Result
-----------------------+-------------------+-----------------------
2147483647 + 1 2147483648 2147483648
2147483648 + 1 -2147483648 2147483649
Here is some possible workaround:
Chopping the number(making the number smaller before comparing)
Other scripting languages(use powershell or vbs to compare)
Embedded Powershell solution:
for /f %p in ('powershell -command if (1 -gt 0^) {write-host greater} else {write-host lesser}') do echo %p
This command calls powershell to compare, and retrieve the results using a for loop.
Mine is a very simple code - I'm looking to find out if the difference between a couple of dates gives negative or not. Problem is not in getting the value, but in checking if they're negative or not. Here's my code:
if !val! LSS 0 (
echo value is !val!
echo value is less than zero - SLA breached!
) else (
echo value is !val!
echo value is greater than zero - SLA not breached.
)
The values of !val! are:
-0.9513888888888888888888888888888888888889
63.06736111111111111111111111111111111111
-1.09722222222222222222222222222222222222
-1.19236111111111111111111111111111111111
First two results are fine - no issues in detecting they're positive and negative. Next two - batch says that they're greater than one! Here's the result:
value is -.95138889
value is less than zero - SLA breached
value is 63.0673611
value is greater than zero - SLA not breached.
value is -1.1923611
value is greater than zero - SLA not breached.
value is -1.0972222
value is greater than zero - SLA not breached.
Where am I going wrong?
I found a different way to approach this -
if "!val:~0,1!"=="-" (
echo value is !val!
echo value is less than zero - SLA breached!
) else (
echo value is !val!
echo value is greater than zero - SLA not breached.
)
Here's the result:
value is 63.0673611
value is greater than zero - SLA not breached.
value is -.95138889
value is less than zero - SLA breached
value is -1.0972222
value is less than zero - SLA breached
value is -1.1923611
value is less than zero - SLA breached
Hope this helps someone!
I am making a batch rpg game, and am working on a shop. You are supposed to be able to upgrade your weapons/armor, and i would like to know how to make a way so the price increases when you buy. Heres what i have:
if %money% GEQ (%WeaponLevel% * 4) / 2 then set money=(%WeaponLevel% * 4) / 2 & set WeaponLevel=%WeaponLevel% + 1
Thanks! Hope i'm not too far off :)
set /a $=money - (weaponlevel *2)
if %$% geq 0 set /a money=$&set /a weaponlevel +=1
Using a temporary variable $ (its name is not relevant - but don't use the reserved names temp or tmp),
set the tempval to money-(weaponlevel*2)
Note that in set/a, the variablenames do not need to be enclosed in %. Also that ((%weaponlevel% * 4) / 2) is exactly the same as (weaponlevel*2)
if the resultant temporary value is greater than or equal to zero, then change the values of money and weaponlevel using set /a; the new value of money is in the temporary variable and +=1 increments a value.
You could try:
rem My weapon upgrade script
set price=weaponlevel * 4
set /a money-= %price%
Run that script whenever the player goes to upgrade the weapon and the price will be equal to the weapon level * 4 everytime and will reduce the money by that set price. I hope this help : D
I have a batch file which must be able to display a percentage. Unfortunately I have no idea how to accomplish this.
The file takes a range of individual points from 0 to 29 and adds or subtracts points from this range in a background process the user never sees. I want the current percent of how full that range is to be displayed. IE if there are 29 points the file displays "100 %", if there are 22 points it lists "75 %", ectra.
Mathematically the operation should be (x/29)*100. I have coded this operation as:
set /a math="%shields%" / "%scap%"
set /a sm="%math%" * 100
but my code dose not function. sm is the variable which will be the percent, shields is the current 0 - 29 point value and scap is the maximum value shields can be (normally 29 but some conditions can adjust this.)
Can I get a hand with this please? Its confusing.
Matematic operation in bat don't accept floating point value if you make :
22/29 in bat you'll get 0 and 0 *100 = 0.
so you have to do (x*100)/29
#echo off
set $val=22
set /a $percent=(%$val%*100)/29
echo %$percent% %%