Mail Archives: djgpp/1999/02/15/21:41:00
> well i am writing a game, a tomagotchi-like game.... and i need to
> test when it's time to burp... i was planing to do this by getting
> the time when he eats... and store it in a varable... then i was go
> to check the time at the begaining of the main loop... if > then a
> certain value then it would run the burp routine... i hope that this
> will help u in asisting me. thanks
This is easy, then. At the time the trigger event happens (eating, in
this case), do this:
eat_time = time(0);
Note that you could also do "time(&eat_time)" or "eat_time = clock()"
(clock has greater precision than time, but seconds should be enough
for you).
Later, do this:
if (time(0) - eat_time > 60)
burp();
The "60" means it will happen 60 seconds later. Don't forget
to note that you've done that event, something like this:
if (time(0) - eat_time > 60 && burp_time - eat_time < 0)
{
burp_time = time(0);
do_something();
}
Which means "If we haven't eaten in 60 seconds, and the last time we
burped was before we last ate, burp, and remember that we burped so we
don't keep doing it over and over again."
Note I've used time differences, rather than comparing absolute times,
so that if your game is ever running when the time value exceeds 31
bits (it could happen), the times wouldn't compare right but the
differences would (as long as the differences were less than, say,
20-30 years :)
- Raw text -