Game Development Community

Can you have too many Timers firing?

by Scott Johnson · in Torque Game Builder · 08/18/2006 (9:04 pm) · 5 replies

I am beginning to experiment with using timers via setTimerOn to provide my data-state driven objects (sprites etc...) a "heartbeat" so to speak... where each one would fire say every 25 ms or so. with each timer event, the object will self-update it's internal state, position, etc...

Would this method be more efficient than using a single timer event, and then loop over all of these objects and individually direct call an object method...

I'm just wondering about scalability w/regards to how many of these objects I can have simultaneously. Would I run out of "timer resources" if I had say 100 - 500 timer driven objects? Less?

Is there a breakover point where maybe timer driven works better for smaller numbers, and then using a timed "loop" method begins to be better?

Any info on this would be appreciated.

Thanks.

P.S. This would save me some serious experimentation time. (which I'll probably do anyway)...

#1
08/18/2006 (9:24 pm)
If I was going to do that I would make a "task manager" class that had the timer callback with a simset of objects that needed to be updated. For example:

function TaskManager::initializeTaskManager( %update )
{
   %taskManager = new ScriptObject()
   {
       class = TaskManager;
   };
   
   %taskManager.taskSet = new SimSet();

   %taskManager.setTimerOn( %update );

   return %taskManager;
}

function TaskManager::addTask( %this, %object )
{
   %this.taskSet.add( %object );
}

function TaskManager::removeTask( %this, %object )
{
   %this.taskSet.remove( %object );
}

function TaskManager::onTimer( %this )
{
   %count = %this.taskList.getCount():
   for( %i = 0; %i < %count; %i++ )
   {
      %currTask = %this.taskList.getObject( %i );
      if( %currTask.isMethod( "updateTask" ) );
         %currTask.updateTask();
   }
}

Then your task update functions:

function TaskTypeA::updateTask( %this )
{
   // blah blah update
}

function TaskTypeB::updateTask( %this )
{
   // blah blah update
}

With that defined you can use it like so:

%someTaskManager = TaskManager::initializeTaskManager(  25 );

%someTask  = new ScriptObject()
{
   class = TaskTypeA;
};

%someTaskManager.add( %someTask );

That gives you the freedom to have several task managers running simultaneously at different time offsets. To my knowlege, there is no hard limit to the number of timers you can have running at a given time. The only restriction, regardless of how you implement it, will be your processing power. Since timers are called after a given offset I would say it's safer to have manager objects to avoid really wierd timing issues. As you load up the manager, if it takes it more than 25 ms to iterate through all of the tasks, it still won't start on iterating through them again until after it's done with one cycle. It really just comes down to a matter of personal preference though. I prefer things to be much more manageable.

Hope this was helpful.

Edit: I haven't actually tested this code, so there are probably typos/errors.
#2
08/18/2006 (9:40 pm)
Thomas,

Wow.. dude. This rocks, you hit the nail on the head. Multple "brains" as it were...

I just ran a basic test of timer driven objects, very minimal objects, single sprite.

Randomly place these objects (all bouncy...) into the scenegraph. assign random linearVelocities.

Works like a CHAMP... up to a point.. at 100 objects I'm still running over 200fps... now get silly.

500 objects still got about 80 fps... 1000... slows it down to about 30 fps... however this is where it gets bad.

I'm noticing the fps starting to continually drop... i think that it's stacking up these timer calls and the system can't process them fast enough.. eventual buffer overflow (i didn't wait that long)...

I'm now going to give the Taskmanager route a go.

Thanks, that was a fast and robust answer.

EDIT:
If anyone is interested this Nifty Sample is what I used for the above test...
#3
08/21/2006 (10:21 pm)
Is there an advantage of using timers over schedules? im working on a similar issue at the moment where i need to update joystick events to my player and was going to use schedule.
#4
08/23/2006 (10:26 am)
Im not certain.. but schedule does the event ONCE timer will continue to do the function each milliseconds set.

Not a bad idea at all!
#5
08/23/2006 (10:43 am)
@Azraelk - If you put the schedule in the function then it will continue to trigger the event. eg:
function myFunction(){
  echo("This will appear every 1 second");
  schedule(1000,0, "myFunction");
}