Game Development Community

[Solved] Help with timer

by Ghonium · in Torque Game Builder · 08/23/2011 (11:44 pm) · 5 replies

Hi
I been week trying to make a timer that
Whenever level start a timer start and count up like this
00min: 00s : 000 mill
00:00:00
This timer can be used in games like car racing labs or football soccer

Can someone help me please ? Im new in tgb and trying to learn

#1
08/24/2011 (2:40 pm)
Hmmm, well for starters, you could create a named scriptObject of some class, lets say "timerClass", having a custom field "time". So something like this:

new ScriptObject(Timer){
   canSaveDynamicFields = "1";
   class = "timerClass";
   time = "0"; 
};

Then right after creating it, or whenever you need it to start timing, call the "setTimerOn" function on it, like so:

Timer.setTimerOn( somemilliseconds );

Every however many milliseconds you define, the "onTimer" callback will be called on that object. Now that you have a recurring callback, you can write all your timer logic in there easily, something like this:

function timerClass::onTimer( %this ){
   //Do stuff like increase "%this.time" and manipulate strings
}

Perhaps you could use something besides a scriptobject (like a t2dTextObject or guitextctrl, which have text fields), but I'll leave that up to you. I hope this helps and that you have fun learning more about tgb!
#2
08/24/2011 (5:31 pm)
Thank you
that helped alot :)
#3
08/25/2011 (4:37 pm)
I made a slight mistake that I hope hasn't caused substantial problems. Scriptobjects do not have timer functions, you'd have to use a t2dsceneobject or any objects under that class.
#4
08/25/2011 (5:00 pm)
I used TextObject and worked perfect
now when level Load the timer start like this
00:00:000
00min:00sec:000milSec
and that what I want :)
thanks :)
#5
08/25/2011 (5:10 pm)
this is the code to show 00:00:000
function tick()
{
   LevelGuiTimer.text = 0;
   LevelGuiTimer.setTimerOn(1);
   LevelGuiTimer.visible = 0;
}
function LevelGuiTimer::onTimer(%this)
{
   %this.text += 1;
   
   // convert SceneTime to hh:mm:ss
   %msec_hh = 1000 * 60 * 60; // millisecs per hour
   %msec_mm = 1000 * 60; // millisecs per minute
   %msec_ss = 1000; // millisecs per second
   %cTime = %this.text;
   %tHou = mFloor(%cTime / %msec_hh);
   %r = %cTime % %msec_hh;
   %tMin = mFloor(%r/%msec_mm);
   %r = %r % %msec_mm;
   %tSec = mFloor(%r/%msec_ss);
   %r = %r % %msec_ss;
   %milli = %this.text;
   %result = %tMin SPC ":" SPC %tSec SPC ":" SPC %r;
   //%result is your formated time you can place it where ever you want that accepts a string object.
   TimeboardGuiTime.setValue(%result);
       
   if ( %this.text > 5000 )
   {
      %this.setTimerOff();
   }
   // Time-check
   if( $pick == 1 )
   {
      //Once Pickup then timer off
      %this.setTimerOff();

      %this.setScenePause( true );
      $pick = 0;
   }    
}



function MyLevel::onLevelLoaded(%this)
{   
         tick();
}