Timing

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Timing

Post by Cleves »

A quick question about timing:

Code: Select all

u32 lastTime = device->getTimer()->getTime();

if(device->getTimer()->getTime() > lastTime + 2000) 
 DoSomething();
If I get it right, after 2 seconds DoSomething will happen. But it doen't work. Any ideas?

Thanks in advance. :D
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

:lol: No, it is not! It just checks whether between the two statements two seconds have elapsed (by some miracle or time warp :wink: ) If not (and this will be true in about 101% of all tries) the statement is just not executed. Use sleep/Sleep to stop your app for the specified amount of time.
Maybe a sleep method would be a good extension for the Timer interface?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Timing

Post by Acki »

Or use this:

Code: Select all

static u32 lastTime = device->getTimer()->getTime();

if(device->getTimer()->getTime() > lastTime + 2000) 
 DoSomething();
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CuteAlien
Admin
Posts: 9939
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Making it only static won't make a difference. Additionally you need to call the if statement regularly (for example in main loop). And in most cases it's better to use member variables than static's.

Using sleep is the best way to go if you don't want to do anything in your application within that time.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Thing is that I do want to do something in this 2 seconds. The game will continue going, but after 2 seconds my energy will rise a little bit.
Andreas
Posts: 166
Joined: Sun Oct 31, 2004 7:15 am
Location: Münster / Germany
Contact:

Post by Andreas »

You could try something like this:

First define a variable in the constructor of your class:

Code: Select all

u32 energyTime = 0;
Then check the time elapsed in the game's main loop;

Code: Select all

u32 curretTime = device->getTimer()->getTime();

if (currentTime - energyTime > 2000) {
    energyTime = device->getTimer()->getTimer();
    // raise the energy bar
    .....
}
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post by Cleves »

Yep, that did the trick. Thanks :P
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I think the usual way is to set a destination time (i.e. now+2000) and check the current time for being larger or equal to the destination time. This removes the additional expression in each comparison. But otherwise yes, do it within the main loop and you'll come to something like that.
tjuhzj
Posts: 44
Joined: Mon Mar 27, 2006 7:00 am

Yes ! that is Ok~

Post by tjuhzj »

You have to check if now is destination time in Main Loop,
because irrlicht doen't provide OnTimer() event!

Andreas wrote:You could try something like this:

First define a variable in the constructor of your class:

Code: Select all

u32 energyTime = 0;
Then check the time elapsed in the game's main loop;

Code: Select all

u32 curretTime = device->getTimer()->getTime();

if (currentTime - energyTime > 2000) {
    energyTime = device->getTimer()->getTimer();
    // raise the energy bar
    .....
}
________
JUSTIN BIEBER FAN
Post Reply