Regenerating Float Values?
Moderator: InsideQC Admins
16 posts
• Page 1 of 2 • 1, 2
Regenerating Float Values?
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:
As you can see "energy",which is stored in defs.qc, is the float I want to regenerate.
I also tried using this:
I made a function that when it's activated, it subtracts 10 from the "energy" float with:
Also, I added this to the "PutClientInServer" function:
And even with all of that, it just won't work!!
But I've only been coding for about a week now so... halp? 
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!!
- chris4268
- Posts: 3
- Joined: Tue Sep 29, 2009 2:59 am
Create a new float:
float recharge_time;
Create a new function to regenerate energy:
Goto PlayerPreThink
And add somewhere:
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.
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.
-

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

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 pm
Alternatively you can use the frametime (seconds per frame) variable and for example:
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.
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.
- 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
frametime should generally not be used unless you can guarentee a tick every frame.
which you can't always do.
which you can't always do.
- Spike
- Posts: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
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
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.
-

Downsider - Posts: 621
- Joined: Tue Sep 16, 2008 1:35 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
16 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 1 guest

