Problem with if statements

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cadab321
Posts: 5
Joined: 23 May 2015 17:59

Problem with if statements

#1 Post by cadab321 » 23 May 2015 18:02

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%
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Problem with if statements

#2 Post by Ed Dyreen » 23 May 2015 18:29

i see no problem in the code posted, the if does exactly what you state.

if "%hp%" > "%maxhp%" then set hp=%maxhp%

cadab321
Posts: 5
Joined: 23 May 2015 17:59

Re: Problem with if statements

#3 Post by cadab321 » 23 May 2015 19:12

It still restores them to full health for no reason. I still require assistance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with if statements

#4 Post by foxidrive » 23 May 2015 19:20

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

cadab321
Posts: 5
Joined: 23 May 2015 17:59

Re: Problem with if statements

#5 Post by cadab321 » 23 May 2015 20:13

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Problem with if statements

#6 Post by Ed Dyreen » 23 May 2015 20:43

cadab321 wrote:, I added that.
We can all see that you did not.

cadab321
Posts: 5
Joined: 23 May 2015 17:59

Re: Problem with if statements

#7 Post by cadab321 » 23 May 2015 20:45

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Problem with if statements

#8 Post by Ed Dyreen » 23 May 2015 20:53

I wonder why you ask a solution for a problem we can't see. That's just trolling.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with if statements

#9 Post by foxidrive » 23 May 2015 22:19

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.

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: Problem with if statements

#10 Post by mcnd » 24 May 2015 02:29

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%
)

cadab321
Posts: 5
Joined: 23 May 2015 17:59

Re: Problem with if statements

#11 Post by cadab321 » 24 May 2015 16:16

Sorry if I have been a bit of a dick, thanks for the support :)

Post Reply