Calling schedules freezes game?
by Jacob Wagner · in Torque Game Builder · 09/22/2005 (3:01 pm) · 5 replies
I have a bit of code in which I call a schedule to function updateDot from within updateDot. I set the time to 20 ms, so my animation runs smoothly, but it also slows down the framerate at 50 ms. What happens is the framerate gets progressively worse until the game slows down to a crawl.
I seem to have this same problem in another game I am working on.
Here is the code of the test I was working on to make a pendulum (right now it just goes around in a circle, havent modified it enough so its an actual pendulum)
right under "add your custom code here" is this:
and here is the function updateDot:
What I don't understand is the game goes very smoothly and it seems like its performing all the calculations and updating the dot correctly, but then about 20 seconds later the game slows to a crawl.
I seem to have this same problem in another game I am working on.
Here is the code of the test I was working on to make a pendulum (right now it just goes around in a circle, havent modified it enough so its an actual pendulum)
right under "add your custom code here" is this:
$dot = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
$dot.setPosition("5 0");
$dot.setSize( "2 2" );
$dot.setImageMap( dotImageMap );
$centerx = 0;
$centery = 0;
$gravity = 0.01;
$distance = 5;
$velocity = 0;
$angle = 0;
updateDot();and here is the function updateDot:
function updateDot() {
echo("Function updateDot()");
%circumference = $distance * 2 * 3.14;
%travelled = $velocity + $gravity;
%percentCircumference = (%travelled / %circumference);
%angleTravelled = 360 * %percentCircumference;
%newPositiony = $distance * mSin(-($angle - mDegToRad(%angleTravelled)));
%newPositionx = $distance * mCos(-($angle - mDegToRad(%angleTravelled)));
$dot.setPosition(%newPositionx SPC %newPositiony);
$angle = $angle - %angleTravelled;
schedule(20, 0, "updateDot");
}What I don't understand is the game goes very smoothly and it seems like its performing all the calculations and updating the dot correctly, but then about 20 seconds later the game slows to a crawl.
#2
I'm not using torque 2d's built in movement functions because I'm not moving the object in a straight line - I'm simulating a pendulum and it has to move around a circle.
I was hoping to have a game in which many pendulums are tied together, but if I can't even get one to work I guess I'm in trouble :)
Oh, and I'm not checking to see if the object is still valid as I'm just testing this stuff out before I make an actual game out of it.
09/22/2005 (5:09 pm)
The thing is, even if I set the schedule time to 200, the game will still lower fps until its barely crawling along. And at 200 ms the motion is not very smooth at all. I'm not using torque 2d's built in movement functions because I'm not moving the object in a straight line - I'm simulating a pendulum and it has to move around a circle.
I was hoping to have a game in which many pendulums are tied together, but if I can't even get one to work I guess I'm in trouble :)
Oh, and I'm not checking to see if the object is still valid as I'm just testing this stuff out before I make an actual game out of it.
#3
Place an fxSceneObject2D at the pivot point of the pendulum. Then, mount the bob rigidly to it at whatever offset you need. Then you can move the bob by simply controlling the rotation of the pivot object.
09/22/2005 (6:57 pm)
I have no idea why the engine is slowing down from the schedule calls. As far as I know, calling schedules the way you have should work just fine. You might want to rethink how you're doing the pendulum, though. Here's what I would recommend.Place an fxSceneObject2D at the pivot point of the pendulum. Then, mount the bob rigidly to it at whatever offset you need. Then you can move the bob by simply controlling the rotation of the pivot object.
#4
I wish I could figure out this schedules problem though, because it also affects a game I have in which I call schedules to make a radar.
The funny thing is tho in the game with the radar, even if I dont use schedules, and instead call the updateRadar() function based on a variable I increment every onupdatescene, it will still slow the game down until it freezes.
its like doing certain actions too often will crash the game, I'll have to check line by line to see exactly what the offending function is and maybe submit it as a bug report.
09/23/2005 (3:29 pm)
Hmm thanks for the tip, for some reason I never thought of that. :)I wish I could figure out this schedules problem though, because it also affects a game I have in which I call schedules to make a radar.
The funny thing is tho in the game with the radar, even if I dont use schedules, and instead call the updateRadar() function based on a variable I increment every onupdatescene, it will still slow the game down until it freezes.
its like doing certain actions too often will crash the game, I'll have to check line by line to see exactly what the offending function is and maybe submit it as a bug report.
#5
09/23/2005 (3:46 pm)
The only times I've ever seen my schedules grind dramatically to a halt are when I accidentally loop a schedule in addition to calling it in onSceneUpdate.
Torque Owner Josh Moore
Also, why aren't you using Torque 2D's built in movement functionality?
Ya know:
$dot.setLinearVelocityX(10);
$dot.setLinearVelocityY(10);
$dot.setConstantForce("X Y", true);