Multiple timers on a single object
by Aditya Kulkarni · in Torque Game Builder · 11/18/2009 (5:46 am) · 7 replies
I need to set 2 timers when an object is created, but as far as i understand, you can only schedule a timer once the previous one has been triggered...
#2
here, i can only specify onTimer event on the object 'obj', right?
11/18/2009 (6:28 am)
i do this...obj.setTimerOn( 2000 );
function obj::onTimer(%this) {
echo "Now!!"
%this.setTimerOff();
}here, i can only specify onTimer event on the object 'obj', right?
#3
You can do alternative way thou. For eg. if one timer should trigger every 2000ms and the second one every 100ms, then you can set just one timer that triggers every 100ms and then add some counter that you increment onTimer call to check the other (longer) counter.
or
You set sub-object to that main-object and set counters to them.
mainObject.counterObject1 = new t2dSceneObject(){};
mainObject.counterObject2 = new t2dSceneObject(){};
...
(i think that only t2dSceneObjects can have timers on them)
11/18/2009 (7:29 am)
As far as i know you can't set multiple timers at the same time on a single object.You can do alternative way thou. For eg. if one timer should trigger every 2000ms and the second one every 100ms, then you can set just one timer that triggers every 100ms and then add some counter that you increment onTimer call to check the other (longer) counter.
or
You set sub-object to that main-object and set counters to them.
mainObject.counterObject1 = new t2dSceneObject(){};
mainObject.counterObject2 = new t2dSceneObject(){};
...
(i think that only t2dSceneObjects can have timers on them)
#4
If you use the schedule function, you can have as many as you want, each with its own callback.
I create the "handles" so that if I need to cancel the tick before the object is deleted, I have access to the timer and can just call "cancel(Player.happyTickHandle);"
11/18/2009 (2:11 pm)
If you use the setTimerOn, then yes, you can only have one.If you use the schedule function, you can have as many as you want, each with its own callback.
new t2dSceneObject(Player) { ...setup class here... };
Player.happyTick();
Player.sadTick();
function Player::happyTick( %this )
{
... Do something here ...
Player.happyTickHandle = %this.schedule( 500, happyTick );
}
function Player::sadTick( %this )
{
... Do something here ...
Player.sadTickHandle = %this.schedule( 357, sadTick );
}I create the "handles" so that if I need to cancel the tick before the object is deleted, I have access to the timer and can just call "cancel(Player.happyTickHandle);"
#5
i am creating multiple object having the same class...in that case, even this method goes haywire...
suppose i create 2 objects (having the same class), then the 2nd object's timer finishes with the first...
11/19/2009 (6:22 am)
awesome!! love u guys...:)i am creating multiple object having the same class...in that case, even this method goes haywire...
suppose i create 2 objects (having the same class), then the 2nd object's timer finishes with the first...
#6
That will keep each individual object separate.
11/19/2009 (7:22 am)
If you are using the same class, make sure to always use the "%this" variable. Looking at my example, I realize I mixed by accesses.%s = new t2dSceneObject() { class = Ogre; ...setup class here... };
%s.happyTick();
%s.sadTick();
function Ogre::happyTick( %this )
{
... Do something here ...
%this.happyTickHandle = %this.schedule( 500, happyTick );
}
function Ogre::sadTick( %this )
{
... Do something here ...
%this.sadTickHandle = %this.schedule( 357, sadTick );
}That will keep each individual object separate.
#7
i finally ended up using schedule without the handle...
schedule(15000, 0, "upgrade");
11/21/2009 (12:18 am)
awright cool...over the head fer me....i finally ended up using schedule without the handle...
schedule(15000, 0, "upgrade");
Associate William Lee Sims
Machine Code Games