Regenerating Float Values?

Discuss programming in the QuakeC language.
chris4268
Posts: 3
Joined: Tue Sep 29, 2009 2:59 am

Regenerating Float Values?

Post by chris4268 »

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:
Mexicouger
Posts: 514
Joined: Sat May 01, 2010 10:12 pm
Contact:

Post by Mexicouger »

self.think && self.nextthink
Jukki
Posts: 214
Joined: Wed Apr 07, 2010 4:59 am

Post by Jukki »

Mexicouger wrote:self.think && self.nextthink
but thouse are affected by frames...
Mexicouger
Posts: 514
Joined: Sat May 01, 2010 10:12 pm
Contact:

Post by Mexicouger »

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.
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Post by ceriux »

what does += mean again?
Arkage
Posts: 66
Joined: Thu Nov 19, 2009 4:17 pm

Post by Arkage »

its the same as writeing:
this = this + some_really_cool_number;
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Post by frag.machine »

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)
chris4268
Posts: 3
Joined: Tue Sep 29, 2009 2:59 am

Post by chris4268 »

Thanks a ton everyone, I managed to get it to work now :D
GiffE
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT
Contact:

Post by GiffE »

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.
Mexicouger
Posts: 514
Joined: Sat May 01, 2010 10:12 pm
Contact:

Post by Mexicouger »

self.energy + 5*frametime
Thanks for the new method of messing with time based functions
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

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

which you can't always do.
Downsider
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Post by Downsider »

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?
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

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.
Downsider
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Post by Downsider »

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.
GiffE
Posts: 170
Joined: Sun Oct 08, 2006 3:39 pm
Location: USA, CT
Contact:

Post by GiffE »

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.
Post Reply