Game Development Community

noob question: Timer?

by sqldbatx · in TGB Platformer Kit · 07/29/2009 (6:59 pm) · 4 replies

I'm trying to find a good place to put my timer code. I can't seem to find a function that's called over and over. Anyone have a recommendation?

Thanks!

#1
07/29/2009 (7:47 pm)
You can always use a schedule:

// Call a method on an object in 500ms (1/2 a second).
%myObject.schedule( 500, "myMethod" );

// Call a function in 500ms.
schedule( 500, 0, "myFunction" );
#2
07/30/2009 (6:27 am)
Is there a way to see how long until the schedule fires? If not this won't work unless I string a host of schedules. I'm looking to make a countdown. Would calling a schedule from another schedule from another schedule be poor in performance?

Example:

... onaddtoscene ... ...
{
...
schedule(25000, 0, "executeschedule1");
}

Function executeschedule1
{
schedule(1000,0,"executeschedule2");
guitxt.text = "5";
}

Function executeschedule2
{
schedule(1000,0,"executeschedule3");
guitxt.text = "4";
}
...
#3
08/06/2009 (1:48 pm)
Just adding thoughts to this, could you do something like.
schedule( 500, 0, "myFunction" );

function myFunction
{
 if variable "timer" isn't set
 {
  timer = 5
 }
 guitxt.text=timer;
 timer= timer - 1;
 
 if timer=0
 {
 anotherFunction();
 cancel schedule();
 delete timer
 }
}
I've not done torquescript for a good few years so maybe that's not possible, I hope its something to spark some thought.

Is there a parameter that is passed via schedule() into your function for how many times it has run? That could help.
#4
05/18/2010 (11:07 am)
Here's a recursive way to handle a countdown:



schedule(1000,"myFunction",5); //5 second countdown
//...
//...
function myFunction(%timer)
{
if{%timer==0)
{
//finished, do something!
echo("KABOOOM");
}
else
{
echo("Explosion in... " @ %timer);
schedule(1000,"myFunction",(%timer-1));
}
}

The output would be:

Explosion in... 5
Explosion in... 4
Explosion in... 3
Explosion in... 2
Explosion in... 1
KABOOOM

(my syntax might be off a lot, but the idea is still there)