Game Development Community

Timer

by Jeremy Easoz · in Torque Game Builder · 06/29/2008 (9:08 pm) · 8 replies

The game im working on has 2 phases.
Phase One lasts 20 seconds.
Phase Two does some processing, ends, and starts Phase One again.

The only way I could think to do this was with setTimerOn and onTimer.
I setTimerOn(20000); //20 Seconds
The onTimer callback starts Phase Two.
Phase Two does it's thing and starts Phase One again.
The only problem with this is that it's a callback and I have no way to show a visual timer.

Are there other timing fuctions that im missing?
Or can someone suggest a better way?

#1
06/29/2008 (9:22 pm)
Just off the top of my head, you could do something like:

function myCountdownTimer::updateTime(%this, %time, %callback)
{
    %this.text = %time;
    
    if (%time > 0)
        %this.schedule(1000, %this, "updateTime", %time - 1000, %callback);
    else if (%this.isMethod(%callback))
        eval(%this @ "." %callback @ "();");
}

This is untested, but it should countdown from the initial time to 0 and then call an optional callback function.
#2
06/30/2008 (11:33 am)
Torsion throws a parse error on the eval statment.
But I don't see anything wrong with it.

I tried,
eval(%this @ "." @ %callback @ "();");

It compiles ok but doesn't seem to work.

I used it like this.
phaseone.updatetime(20000, updateTime);
When the game loads up it shows 20000 for the time. (I can fix that)
But after the initial run it never updates again which makes me think
it's not hitting the function again.

Mis-use by me?
#3
06/30/2008 (12:01 pm)
Philip gave an example of a generic/reusable way to call two functions in sequence (well after a time ms delay). But in your case, the following (less reusable) code might be easier for you to understand.

function startGame()
{
   phaseOne();
}

function phaseOne()
{
   echo( "phaseOne" );
   schedule( 20000, 0, phaseTwo );
}

function phaseTwo()
{
   echo( "phaseTwo" );
   // do processing...

   // then...
   phaseOne();
}
#4
06/30/2008 (12:17 pm)
Yeah that's what I was doing before.
Only problem was counting down per second and updating a visual on the screen.
#5
06/30/2008 (3:28 pm)
Do you have a license with source code access? I don't recall ... there might be a timer ctrl in TGB already, or you should be able to find a resource around.

Alternatively you could do it all in script a couple different ways. If you are going the script route I could provide you some ideas to get started.
#6
06/30/2008 (4:09 pm)
Yeah i'm trying to do everything in script for now.
Yes I have source code access to all products except TorqueX 2.0 Pro.
#7
07/01/2008 (12:22 pm)
The easiest way might be to use to t2dTextObject to visually show the time left, since that is a sceneObject you can use the setTimerOn/Off methods that all sceneObjects have. You could also use a guiTextCtrl but you would need a repeating schedule or a behavior with the onUpdate enabled to keep the time up-to-date.

Let us assume that you have a t2dTextObject that is named Timer.

function phaseOne()
{
   Timer.text = 20;
   Timer.setTimerOn(1000);
}

function Timer::onTimer(%this)
{
   %this.text -= 1;
   if ( %this.text <= 0 )
   {
      %this.setTimerOff();
      phaseTwo();
   }
}

function phaseTwo()
{
   // do processing...
   // then...
   phaseOne();
}
#8
07/03/2008 (2:58 pm)
That will do nicely thanks.

That's pretty much what I was doing before except I called setOnTimer(20000) with the full
20 seconds.