Forum

Working on a loop.

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Working on a loop.

Postby Mexicouger » Thu Oct 07, 2010 4:20 am

I started working on a loop, and I am very new to loops. I was trying to figure this out alone, But I keep coming to a couple problems. FIrst of all, here is my code:
Code: Select all
h = cl.stats[STAT_HEALTH] * 2.22;
   while (cl.stats[STAT_HEALTH] == cl.stats[STAT_HEALTH])
   {
   h++;
   if (h > 422 || h < 222)
   return;
   life100 = Draw_CachePic ("gfx/energyb.lmp");
   M_DrawPic ( (h-life100->width)/2, 20, life100 );
   }


So basically, this is an energy bar. The minimum distance it should go is 222(x), and the maximum it can go is 422(x).

Then I want the health to be incorporated into the calculation, so it makes the graphics based on a health bar of 0-130. I don't want 0-30 to show, only 31-130. The health bar overlaps though, and It doesn't go all the way.

Code: Select all
h = cl.stats[STAT_HEALTH] * 4.22;


I got the number 4.22 by taking 422(maximum x value) and dividing it by 100. The health bar also scrolls from right to left, and it is supposed to go left to right. Any help on these would help. If not, Then it is all good.
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm

Postby frag.machine » Thu Oct 07, 2010 12:13 pm

The general format for a while loop is:

Code: Select all
while (condition is true) {do something}


You must assure that at some point your condition change from true to false, otherwise your code won't never scape the loop and the program freezes:

Code: Select all
while (cl.stats[STAT_HEALTH] == cl.stats[STAT_HEALTH])


As we can see, cl.stats[STAT_HEALTH] will always be equals to itself, so your loop won't never end.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Mexicouger » Thu Oct 07, 2010 1:14 pm

Alright. I made it end earlier, But It wasn't giving me the right results.

EDIT:

And In my loop, I had
Code: Select all
if (h > 422) return;


That made the while loop not be able to go any further down the bottom. But I suppose ++h could keep going
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest