Forum

Regenerating Float Values?

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Regenerating Float Values?

Postby chris4268 » Sat Feb 19, 2011 3:40 am

The title is pretty self-explanatory, how do I make a float value increase in a specified amount of time?

This is the code I made:

Code: Select all
void() Energy_Regen =
{   
   if (energy == 20)
      centerprint (self, "Max energy");
      return;
   
   if (energy != 20)
      {
         centerprint (self, "Used some energy");
         self.count = self.count + 1;
         if (self.count == 20)
         {
            energy = 20;
         }
      }
};


As you can see "energy",which is stored in defs.qc, is the float I want to regenerate.

I also tried using this:

Code: Select all
void() Energy_Regen =
{   
   if (energy == 20)
      centerprint (self, "Max energy");
      return;
   
   if (energy < 20)
      {
         centerprint (self, "Used some energy");
         self.nextthink = time + 1;
         self.think = energy + 1;
      }
};


I made a function that when it's activated, it subtracts 10 from the "energy" float with:

Code: Select all
energy = energy - 10;


Also, I added this to the "PutClientInServer" function:

Code: Select all
energy = 20;


And even with all of that, it just won't work!! :x But I've only been coding for about a week now so... halp? :lol:
chris4268
 
Posts: 3
Joined: Tue Sep 29, 2009 2:59 am

Postby Mexicouger » Sat Feb 19, 2011 3:52 am

self.think && self.nextthink
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm

Postby Jukki » Sat Feb 19, 2011 6:06 am

Mexicouger wrote:self.think && self.nextthink


but thouse are affected by frames...
Jukki
 
Posts: 214
Joined: Wed Apr 07, 2010 4:59 am

Postby Mexicouger » Sat Feb 19, 2011 7:39 am

Create a new float:
float recharge_time;

Create a new function to regenerate energy:

Code: Select all
void() Regenerate =
{
if (energy < 20)
energy += 0.01;

recharge_time = time + 5; //energy can increase in 5 seconds
};


Goto PlayerPreThink

And add somewhere:

Code: Select all
if (recharge_time < time)
Regenerate();


code not tested, but you get the feeling. While recharge_time is less than time and energy is less than 20, add 0.01 to energy. Then after 5 seconds, add more energy.
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm

Postby ceriux » Sat Feb 19, 2011 7:44 am

what does += mean again?
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Postby Arkage » Sat Feb 19, 2011 10:40 am

its the same as writeing:
this = this + some_really_cool_number;
User avatar
Arkage
 
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Postby frag.machine » Sun Feb 20, 2011 1:39 am

Beware, "+=" is not supported by all QuakeC compilers.
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 chris4268 » Sun Feb 20, 2011 4:58 am

Thanks a ton everyone, I managed to get it to work now :D
chris4268
 
Posts: 3
Joined: Tue Sep 29, 2009 2:59 am

Postby GiffE » Sun Feb 20, 2011 5:12 pm

Alternatively you can use the frametime (seconds per frame) variable and for example:
Code: Select all
self.energy + 5*frametime

This would make it regenerate at a steady pace compared to the previous "wait 5 seconds... regen a chunk"
I'd also like to point out something in your initial code, although I know you got it working.
Code: Select all
void() Energy_Regen =
{   
   if (energy == 20)
      centerprint (self, "Max energy");
      return;
   
...
};


This would return EVERY time right after the first if statement. It will never get to if(energy != 20). centerprint is nested under the if when there is no curly braces.
GiffE
 
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT

Postby Mexicouger » Sun Feb 20, 2011 5:55 pm

self.energy + 5*frametime

Thanks for the new method of messing with time based functions
User avatar
Mexicouger
 
Posts: 514
Joined: Sat May 01, 2010 10:12 pm

Postby Spike » Sun Feb 20, 2011 7:14 pm

frametime should generally not be used unless you can guarentee a tick every frame.

which you can't always do.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby Downsider » Mon Feb 21, 2011 1:41 am

Spike wrote:frametime should generally not be used unless you can guarentee a tick every frame.

which you can't always do.


I thought it helped make things framerate independent?
User avatar
Downsider
 
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Postby Spike » Mon Feb 21, 2011 2:54 am

its only framerate independant if the value is correct for the total duration. if you're using a nextthink of 0.1, and the server is ticking 72 times per second, your frametime is 1/72 instead of 0.1, and thus wrong.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby Downsider » Mon Feb 21, 2011 3:45 am

Spike wrote:its only framerate independant if the value is correct for the total duration. if you're using a nextthink of 0.1, and the server is ticking 72 times per second, your frametime is 1/72 instead of 0.1, and thus wrong.


Oh, I wasn't talking about using Mexicouger's method. I was talking about the normal method that GiffE refers to.
User avatar
Downsider
 
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Postby GiffE » Mon Feb 21, 2011 6:41 am

Spike wrote:frametime should generally not be used unless you can guarentee a tick every frame.

which you can't always do.

Sorry Spike, but could you elaborate? I've really only had experience using this in SP so...
For example: Say I'd want to increase energy each frame proportional to the time it took between them. This is why using frametime made sense.
If the game was run on a faster cpu the frametime would be less but the proportion would remain the same, so where's the issue?

Also I don't know if I made this clear but I'd do the increase with in the PlayerPreThink or in Startframe if I had to do it for something in the world. I would not be using nextthink at all.
GiffE
 
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT

Next

Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest