Problem with if statements
Moderator: DosItHelp
Problem with if statements
Hello. I don't know much batch, and I am just starting code. I am making a small rpg, and a feature that it has is it gives you a randomly generated amount of health from 1-20. I didn't want people to have their health go over their maxhealth, so I made the code you see below. It doesn't seem to be working. I was able to make it so it doesn't go over the limit, but now it automatically resets your health to full, even if you didn't get that much health. Help?
if "%hp%" gtr "%maxhp%" (
set hp=%maxhp%
)
if "%hp%" gtr "%maxhp%" (
set hp=%maxhp%
)
Re: Problem with if statements
i see no problem in the code posted, the if does exactly what you state.
if "%hp%" > "%maxhp%" then set hp=%maxhp%
if "%hp%" > "%maxhp%" then set hp=%maxhp%
Re: Problem with if statements
It still restores them to full health for no reason. I still require assistance.
Re: Problem with if statements
This can help to give you a random number - run it and press the spacebar several times.
Code: Select all
@echo off
set /a hp=%random% %% 20 + 1
echo %hp%
pause >nul
%0
Re: Problem with if statements
I am aware of that, and already have a health randomizer. The thing is, it adds a random number of health to your current health. To prevent it from going over max health, I added that. Now it automatically resets your health to max whenever you get a kill, no matter what.
Re: Problem with if statements
We can all see that you did not.cadab321 wrote:, I added that.
Re: Problem with if statements
No... you can't. The current code that I have prevents a variables value from going over anothers. The problem is it automatically sets it to that variable.
Re: Problem with if statements
I wonder why you ask a solution for a problem we can't see. That's just trolling.
Re: Problem with if statements
cadab321 wrote:I am aware of that
cadab321 - See here: viewtopic.php?f=3&t=6108
Ed replied to you and I replied - and your problem can be a range of things that we can't help you with because we can't see what you have done in your code.
Re: Problem with if statements
cadab321 wrote:... it automatically resets your health to full, even if you didn't get that much health.
Maybe your problem are the quotes. With them the gtr operator compares strings. Try with
Code: Select all
if %hp% gtr %maxhp% (
set hp=%maxhp%
)
Re: Problem with if statements
Sorry if I have been a bit of a dick, thanks for the support