Can somebody help me how to make some function to do every 10 seconds or so?? I tried with if (clock() > 10) but after ten seconds it always call it. I know that and I tryed Sleep() but it didn t work as I thought it would so any suggestions on this matter??? thanx.
clock() keeps counting up, it doesn't just magically reset when you want it to.
Sleep() is Windows only, and it also takes a value in milliseconds (so 1 second is 1000 milliseconds)
well not like that.. It doesn t work.. I ll try to explain one more time.. for example you have
while loop with switch statement an you set energy on for example 100 and what is my probelm is that it will be still less and less in i don t know maybe 30 seconds... i ll post you part of a code here.. maybe it will be better to understand what I want.. sorry for my english btw. i am not native english speaker.
=>
while (fEnd)
{
rex.growUp();
if (sec >= death)
{
cout << "I am really sorry but " << name << " died. He was too old. Would you like another pet?";
}
cout<< name <<" is " <<rex.getAge()<<" years old and "<< rex.getHappines()<< "% happy and "<< rex.getHunger()<<"% full and has "<<rex.getEnergy()<<"% energy."<<endl ;
int choice;
cout <<"(1) Feed it (2) Pet it (3) Let it sleep (4) Play with it (5) Load or restart game (0) End\nChoose number: ";
cin >> choice;
switch (choice)
{
case 0:
char progres;
cout << "Do you want to save the progres?? y/n\n";
cin>> progres;
if (progres == 'y')
{
fEnd = false;
break;
}
else
{
cout << "Thank you for playing. Please press enter to quit.";
cin.ignore();
cin.get();
return 0;
}
case 1:
system("cls");
back3:
if (rex.getHunger()<=100)
cout <<rex.getHunger()<<"% full"<<endl;
if (rex.getHunger()>100)
{
rex.setHunger(100);
cout <<rex.getHunger()<<"% full"<<endl;
}
.............. and so on........
//Timer.
#include <Windows.h>
#include <iostream>
#include <conio.h>
void updateThingsHere()
{
std::cout << "You just updated energy.\n";
std::cout << "You just updated health.\n\n";
}
int main()
{
double startTime = GetTickCount();
while( true )
{
double currentTime = GetTickCount() - startTime;
if( currentTime >= 1500 ) //1 and a half seconds.
{
updateThingsHere();
//Reset the timer.
startTime = GetTickCount();
}
//Run other code here while not updating.
char key = ' ';
if( _kbhit() )
key = _getch();
if( key == 13 ) //The ENTER key.
break; //Quit the while loop
elseif( key != ' ' )
{
std::cout << "You pressed: " << key << '\n';
key = ' ';
}
}
return 0;
}
This will run the update function every 1 and a half seconds, but still run the other code while it's waiting for the current time to go greater/equal to 1500 milliseconds.
> will it work even if I do something inside of while loop?
Do you mean do something at the same time that you are waiting? No, the above code will not work; std::this_thread::sleep_for() would block for the duration of the sleep. Is that what you want to do?
A second question. The 'every 10 seconds' that we are talking about - is that elapsed time (as in 4:52:35 PM to 4:52:45 PM ) or processor time (10 seconds of CPU time used in the interim)?
well i want elapsed time while playing game it should count to 10 or 20 or so and after this time it should change energy by itself for example every 10 sec aka 10 000 milisec energy go down by 10 percent..
and i have never worked with chrono library so I probably should look this up for my project..