Schedules and timers

Torque has a couple of built-in functions to help you with timing events. Schedules are a way to defer an event to happen at some point in the future, and timers let you measure the time between known events.

Schedule

You can create a schedule using either the global schedule function, or by calling the schedule method on an object. Both of them number of milliseconds the engine should wait before calling your callback, and the name of the function to call when the timer is up. Here's a simple example where we write a function to do the thing, and then schedule for it to be called one second later:

After 1000 milliseconds, "I'm doing the thing with the data" will be printed to the console. What's that 0 in there for? It lets you specify an object to call the method on, and makes the schedule function behave identically to the schedule method. What does that mean? Here's an example of both:

Both of these ways of calling schedule give the same result.

Cancel

Once you've started a schedule, you often want to stop it. For example, maybe the object it was relevant to has been deleted, or maybe new user input has overridden a previous event that was started. schedule returns an ID when you call it, which you can then cancel with the cancel function:

In the example above, nothing will be printed, because the scheduled function was immediately cancelled.

For a couple of other helpful schedule-related functions, have a look at this page.

Timers

If you need to measure the amount of time between two known events, you have two options. Torque has a low-precision game clock, as well as dedicated high-resolution timers.

Sim and real time

The engine exposes two functions, getRealTime and getSimTime, which return values in milliseconds. getRealTime returns the current Unix time of the computer, whereas getSimTime returns the number of milliseconds since the game was started.

The simple function in the example above binds a timer to the t key, and echoes the amount of milliseconds the key was held down for.

Precision timers

The precision timer works a little differently. startPrecisionTimer returns a timer ID. Calling stopPrecisionTimer with a timer ID stops the timer and returns the number of milliseconds that elapsed since the timer started. Here's the above example again, using precision timers:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License