Okay this is from my Knight Class. I am trying to figure out how I can bring back the health at a much slower rate. When I compile it, it takes me all the way to 100 instantly. I understand why is is doing that , but I am not sure of a way to make it do what I want it to. If someone could post an example of a way I could accomplish this..I would be forever grateful.
1 2 3 4 5 6 7 8 9 10 11
void Knight::knightRegenerate()
{
int gainHealth = 1;
while(gainHealth <= 99)
{
gainHealth++;
}
std::cout << "I have gathered Health!\n"<< gainHealth;
}
What exactly do you want to happen instead? Should the user be allowed to do other things whilst the health is coming back at a 'much slower rate'. Do you want the health to regenerate over a certain time period, or should certain 'events' trigger an increase in health?
If you have some sort of 'iteration' happening in your program (like if the days advance after each joust competition or whatever), you could increment the health a certain amount every time you advance in your program.
One could also regenerate a knight's health by a certain amount every time input is taken from the console, for example.
I'm not sure if that's of any help, but I hope it gives you something to think about...
Yes, thank you for responding. I am sorry for not being more clear..sometimes it eludes me how specific c++ is heh.
What I am wanting to happen is I would like his health to return at a rate of 1 point per second. I thought maybe some sort of timer would be used..but I have not done anything with a timer before. So I wouldn't even know where to begin or the syntax of it.
Do you know of a example of a timer or maybe a tutorial that covers them?
Also note that Windows has Sleep with a capital 'S'.
Also note that sleeping for 1 second is a bad idea because:
- it won't be exactly 1 second (it will be "at least" 1 second, but might be longer)
- your program can't do anything else during that 1 second.
You're better off checking the current time (like with GetTickCount or something) and only incrementing your health if 1 second has passed since the last time you incremented.
But all of this depends on what kind of program you're making. I assume this is some kind of game, right? Are you using some kind of game lib or are you doing console nonsense?
Well I will be using allegro for the GUI. I have never heard of getTickCount before what is that? That may help me cause I don't want my program to stop during that. IS there a cretin format you have to use with getTickCount?
It is a project I am working on more so than a game, it is a learning method that has worked well for me. I have done all the tutorials on this site and a few others..so now I am just using code..trying to see what I have understood and what I haven't. The things I catch myself not knowing I go back to those sections and review.
To wait (put this in a while loop) (clock()-timeofstart)*CLOCKS_PER_SEC() < 1
Where timeofstart is the value returned by clock() when you start your timer.
You can start the timer and check it's elapsed time in your event loop. You could probably wrap that functionality in a little class that will reset the timer and set a flag for your event loop to monitor the elapsed time. You could even add a virtual method for each interval tic and another virtual method that gets called when the time's up. That way you could inherit from the class to create timers for all sorts of things!
@moorecm,
Alternatively, you could do something with static variables and the ctime library:
1 2 3 4 5 6 7 8 9 10 11 12
void get_health(int& health)
{
staticdouble start = clock() / CLOCKS_PER_SEC, /* This is only assigned the first time the function is called */
end = 0;
if (end = (start + 1)) {
health += 1;
}
end = clock() / CLOCKS_PER_SEC;
start = end;
}
Note: I wrote that kind of rushedly, so it probably doesn't work. But something like that would maybe work.