Game Development Community

Creating a timer (with ms)

by Adam Beaumont · in Torque Game Builder · 11/10/2007 (11:13 pm) · 3 replies

I've just picked up TGB after doing quite a lot of work with TGE over the past few years. Things have started out pretty much ok, in that I've got my scrolling levels and controllable character set up with only a few hours work. Great results!

Now I want to add some other stuff that is actually going to make this into a playable game - first problem I have is setting up a timer. I want to have a control on the HUD that ticks up in milliseconds - the basic tutorial suggests setting a 1 second timer and every time it triggers update the time. I think that might be a bit time consuming if i make it every hundreth or thousandth of a second, so is there a better way?

I've done this before in TGE just by writing stuff in the C code but obviously in TGB this isn't as straight forward - suggestions welcome!

#1
11/11/2007 (9:01 am)
I think the only thing in TGB with that level of accuracy is the schedule function. OnUpdate, which is called by the system, is called every 32 ms I believe. The schedule function lets you set a call per millisecond.

%this.theSchedule = schedule( 1, 0,  updateHud, %this);

By setting a variable to the returned value, you can later stop the schedule from happening with the following code:

if(isEventPending(%this.theSchedule ))
{
    cancel(%this.theSchedule);
}

I don't know what type of overhead you get by calling this every millisecond. If 30 times a second is ok, you may want to look into using the OnUpdate callback.
#2
11/11/2007 (4:55 pm)
May I make a suggestion that you do not need to update the HUD every millisecond. No human can read that but 30 times per second. I suggest you update the HUD every second and then when necessary, for example on a pause or some even that requires the player to know the exact time, you can update the HUD with the exact time via getTimeSinceStart( eventID ).
#3
11/12/2007 (4:32 am)
I agree that you can't really see the numbers if its in MS but i think its a nicer effect. i also think that the timer function may be too much of an overhead so maybe onupdate is the way to go - i will check it out. Thanks.