Using schedule() and cancel()
by Gabe Allen · 07/17/2002 (9:10 am) · 16 comments
At some point, you're going to need to continually run a function based upon a certain amount of time. That's where schedule() comes in.
I've made a few changes based on the comments I got. One thing (basically the repetition) I knew, but I was still thinking in that mindset when I wrote this. So I got that cleared up (thanks Brett). I also added in how to pass variables (thanks Matt).
In my case, I was trying to alter the turning speed of a player based upon their current velocity. This meant I needed to re-evaluate the player velocity constantly while the turn key was depressed. I had virtually no idea how to do this. When I explained what I was trying to do, Ed Gardner mentioned schedule(), so I went off in search of information on how to useit. Unfortunately, I didn't find much. But between the original schedule() forum post and Panama Jack's speed HUD, I managed to figure out how to do what I needed to do.
I'll say right now that I don't know how to use this with objects. If anyone does, please post comments so I can update this.
Your basic usage : schedule(%time, %object, %command)
%time is a value in milliseconds that specifies when it will run %command.
For the purposes of this HowTo, %object should be entered as 0
%command is the name of a function that you want to run
Example : schedule(1000,0,bustAction);
This will execute the bustAction() function after one second. By placing your schedule inside the bustAction() function, it will execute every second. Using schedule(1000,0,bustAction(somethingHere)); is a quick way to get back to your desktop. Instead use schedule(1000,0,bustAction,%variable,"string",#);
If you're going to have schedule called repeatedly, you're going to need a way to stop it from exectuing. This is where cancel() comes in. But in order to use it, we have to change the way that we call schedule().
Example : $someVariableName = schedule(1000,0,bustAction);
This effectively gives your scheduled event a name. In order to stop your scheduled event, use cancel($someVariableName);
Hopefully someone will find this useful. If anyone has any questions or comments, please post them (as several of you have!) and I'll get back to you.
I've made a few changes based on the comments I got. One thing (basically the repetition) I knew, but I was still thinking in that mindset when I wrote this. So I got that cleared up (thanks Brett). I also added in how to pass variables (thanks Matt).
In my case, I was trying to alter the turning speed of a player based upon their current velocity. This meant I needed to re-evaluate the player velocity constantly while the turn key was depressed. I had virtually no idea how to do this. When I explained what I was trying to do, Ed Gardner mentioned schedule(), so I went off in search of information on how to useit. Unfortunately, I didn't find much. But between the original schedule() forum post and Panama Jack's speed HUD, I managed to figure out how to do what I needed to do.
I'll say right now that I don't know how to use this with objects. If anyone does, please post comments so I can update this.
Your basic usage : schedule(%time, %object, %command)
%time is a value in milliseconds that specifies when it will run %command.
For the purposes of this HowTo, %object should be entered as 0
%command is the name of a function that you want to run
Example : schedule(1000,0,bustAction);
This will execute the bustAction() function after one second. By placing your schedule inside the bustAction() function, it will execute every second. Using schedule(1000,0,bustAction(somethingHere)); is a quick way to get back to your desktop. Instead use schedule(1000,0,bustAction,%variable,"string",#);
If you're going to have schedule called repeatedly, you're going to need a way to stop it from exectuing. This is where cancel() comes in. But in order to use it, we have to change the way that we call schedule().
Example : $someVariableName = schedule(1000,0,bustAction);
This effectively gives your scheduled event a name. In order to stop your scheduled event, use cancel($someVariableName);
Hopefully someone will find this useful. If anyone has any questions or comments, please post them (as several of you have!) and I'll get back to you.
#2
07/18/2002 (4:57 am)
Thanks in advance! You probably saved my team and I a few days/weeks of searching.
#3
ie. schedule(1000,,) will fire in one seconds time.
if you wanted it to fire every second you would require it to call a function which then rescheduled iteself.
I'm not sure if you knew this, sorry if you did, but you talk about it as if it runs continually.
Regards,
Brett
07/18/2002 (5:47 am)
Schedule() only runs once.ie. schedule(1000,
if you wanted it to fire every second you would require it to call a function which then rescheduled iteself.
I'm not sure if you knew this, sorry if you did, but you talk about it as if it runs continually.
Regards,
Brett
#4
Matt : Yep. You can pass stuff just fine like that. Thanks.
Brett: like I say in the update, I just got kinda stuck on that one use because that's what I was doing at the time. I even used it as a one shot deal in my code, which makes me feel really dumb :)
07/18/2002 (8:01 am)
Made a couple of changes there .. thanks guys. Matt : Yep. You can pass stuff just fine like that. Thanks.
Brett: like I say in the update, I just got kinda stuck on that one use because that's what I was doing at the time. I even used it as a one shot deal in my code, which makes me feel really dumb :)
#5
I used it heavily in T1, even to add ground vehicles (though ofcourse the code lagged the server to all hell).
07/21/2002 (12:05 am)
An old trick from Tribes 1 :)I used it heavily in T1, even to add ground vehicles (though ofcourse the code lagged the server to all hell).
#6
www.liquid.nq.net/tutorial/
07/22/2002 (3:36 pm)
For a tutorial on using schedule in both its native and object oriented form go herewww.liquid.nq.net/tutorial/
#7
function PlayGui::onMouseDown(%this, %aKeyModifier, %clickCount)
{
if (%clickCount == 1) {
$clickEvent = schedule(500, 0, PlayGui.onMouseDown, %this, %aKeyModifier, %clickCount);
}
Any ideas?
10/28/2004 (7:56 pm)
I keep getting an error that says the method I'm passing is not found. I'm spelling it correctly and passing the all the arguements it needs, so I don't understand where I'm going wrong. Here is what I have:function PlayGui::onMouseDown(%this, %aKeyModifier, %clickCount)
{
if (%clickCount == 1) {
$clickEvent = schedule(500, 0, PlayGui.onMouseDown, %this, %aKeyModifier, %clickCount);
}
Any ideas?
#8
Thanks for putting this together. I spent the better part of a day combing through the forums looking for this info.
04/03/2005 (6:39 pm)
Gabe,Thanks for putting this together. I spent the better part of a day combing through the forums looking for this info.
#9
08/08/2005 (4:05 am)
ok great i need these kind of documentation heheh.
#10
06/04/2006 (10:40 am)
what is the "object" position for--the paramenter that is always 0?
#13
function rotateShape(%shape)
{
%transformInfo = %shape.getTransform();
%pos_x = getword(%transformInfo, 0);
%pos_y = getword(%transformInfo, 1);
%pos_z = getword(%transformInfo, 2);
%rot_x = getword(%transformInfo, 3);
%rot_y = getword(%transformInfo, 4);
%rot_z = getword(%transformInfo, 5);
%rot_z = (%rot_z + 0.1) % 1.5;
%shape.setTransform(%pos_x SPC %pos_y SPC %pos_z SPC %rot_x SPC %rot_y SPC %rot_z SPC "1");
schedule(200, 0, rotateShape, %shape);
}
any help at all would be really great. thanks in advance
12/22/2006 (11:28 am)
Do you guys have any idea how to cancel schedule() in this situation:function rotateShape(%shape)
{
%transformInfo = %shape.getTransform();
%pos_x = getword(%transformInfo, 0);
%pos_y = getword(%transformInfo, 1);
%pos_z = getword(%transformInfo, 2);
%rot_x = getword(%transformInfo, 3);
%rot_y = getword(%transformInfo, 4);
%rot_z = getword(%transformInfo, 5);
%rot_z = (%rot_z + 0.1) % 1.5;
%shape.setTransform(%pos_x SPC %pos_y SPC %pos_z SPC %rot_x SPC %rot_y SPC %rot_z SPC "1");
schedule(200, 0, rotateShape, %shape);
}
any help at all would be really great. thanks in advance
#14
(That's untested but should work nearly as-is.)
01/17/2007 (8:13 pm)
@Nick: Try this...$rotateShapeSchedule = 0;
function cancelRotateShape()
{
if ( $rotateShapeSchedule )
cancel( $rotateShapeSchedule );
$rotateShapeSchedule = 0;
}
function rotateShape(%shape)
{
if ( $rotateShapeSchedule )
cancel( $rotateShapeSchedule );
%transformInfo = %shape.getTransform();
%pos_x = getword(%transformInfo, 0);
%pos_y = getword(%transformInfo, 1);
%pos_z = getword(%transformInfo, 2);
%rot_x = getword(%transformInfo, 3);
%rot_y = getword(%transformInfo, 4);
%rot_z = getword(%transformInfo, 5);
%rot_z = (%rot_z + 0.1) % 1.5;
%shape.setTransform(%pos_x SPC %pos_y SPC %pos_z SPC %rot_x SPC %rot_y SPC %rot_z SPC "1");
$rotateShapeSchedule = schedule(200, 0, rotateShape, %shape);
}(That's untested but should work nearly as-is.)
#15
So I was thinking because the array is dynamic and the schedules are actually doing something similar, I thought that I could just use 1 function for the schedule. But what I don't really know is how to store the instances of each schedules so that I can cancel each one of them in onMissionEnded().
Thanks in advance.
EDIT: Nevermind. Got it working by using eval. Thanks though.
03/03/2008 (8:42 pm)
Hi, I've got a question about scheduling. How do I make an array of global variables that can store the instance of a recursive schedule? E.g I have these schedules that basically do a similar task:function schedule1()
{
scheduledTask();
$sched1 = schedule(1000, 0, schedule1);
}
function schedule2()
{
scheduledTask();
$sched2 = schedule(1000, 0, schedule2);
}
function schedule3()
{
scheduledTask();
$sched3 = schedule(1000, 0, schedule3);
}
...
function scheduleN()
{
scheduledTask();
$schedN = schedule(1000, 0, scheduleN);
}
onMissionLoaded()
{
$sched1 = schedule(1000, 0, schedule1);
$sched2 = schedule(1000, 0, schedule2);
$sched3 = schedule(1000, 0, schedule3);
...
$schedN = schedule(1000, 0, scheduleN);
}
onMissionEnded()
{
cancel( $sched1 );
cancel( $sched2 );
cancel( $sched3 );
...
cancel( $schedN );
}So I was thinking because the array is dynamic and the schedules are actually doing something similar, I thought that I could just use 1 function for the schedule. But what I don't really know is how to store the instances of each schedules so that I can cancel each one of them in onMissionEnded().
Thanks in advance.
EDIT: Nevermind. Got it working by using eval. Thanks though.
#16
12/13/2009 (10:56 am)
Darn trying to do the same thing what is eval?
Torque Owner Matt Webster
Just put variables after the function, and it should work. I haven't worked with it in a while, but the radar used to work like that before it was hard-coded.