Passing values to scheduled functioncalls?
by James Ford · in Torque Game Builder · 06/04/2006 (10:29 am) · 10 replies
As the topis says . . . Is it possible to pass a value to a schudeled functioncall? I have a generic function that should operate on any object that is passed to it, but i need to schedule the function calls. What i am doing now is testing to see what object it is, then scheduling a call to a helper function which really calls the function and passes the correct value. If it is not possible to pass values, that functionality should definitely be added.
About the author
http://jamesdev.info
#2
06/04/2006 (10:41 am)
Thanks, what is the "obj" parameter for? Its always 0 . . .
#3
06/04/2006 (10:53 am)
Try having a look at: www.garagegames.com/mg/forums/result.thread.php?qt=28423 :)
#4
I use schedule like this "schedule(200,0,"functionName", %arg1);
and understand that,
But maybe someone could explain the other way/ways to use schedule more clearly.
06/05/2006 (11:29 am)
I read that and still am not sure ....I use schedule like this "schedule(200,0,"functionName", %arg1);
and understand that,
But maybe someone could explain the other way/ways to use schedule more clearly.
#5
examples:
schedule(2000,0,"functionA",%arg);
%obj.schedule(2000,"functionA",%arg);
schedule(2000,%obj,"functionA",%arg);
At least I think they are all right. The first one calls the function "functionA". The last 2 tell the object %obj to call the function "functionA". In effect the last 2 are the same.
06/05/2006 (11:49 am)
There are 2 ways to use a schedule. 1 way is the way you have just used it. The obj parameter is when you want that object to call the function. The other way is when you use it like an object method. This way doesn't have a obj parameter.examples:
schedule(2000,0,"functionA",%arg);
%obj.schedule(2000,"functionA",%arg);
schedule(2000,%obj,"functionA",%arg);
At least I think they are all right. The first one calls the function "functionA". The last 2 tell the object %obj to call the function "functionA". In effect the last 2 are the same.
#6
function obj_class::(%arg)
{
}
06/06/2006 (8:19 am)
Ok thats cool. And for that you would need to define this ahead of time right?function obj_class::(%arg)
{
}
#7
for this both are for the most part equal, where the playerShip is calling the schedule.
for normal Game Events to schedule that are not owned by another object, just use the normal method with 0 for the object id.
HTH...
06/06/2006 (9:54 am)
No the object if valid should have a schedule function already. you call it just like Tom's Exampleschedule(1000,$playerShip,"moveTo","0 0",10); or $playerShip.schedule(1000,"moveTo","0 0",10);
for this both are for the most part equal, where the playerShip is calling the schedule.
for normal Game Events to schedule that are not owned by another object, just use the normal method with 0 for the object id.
schedule(1000,0,"updateTimer");
function updateTimer()
{
$globalTimer++;
schedule(1000,0,"updateTimer");
}HTH...
#8
06/06/2006 (1:18 pm)
By the way- everything I was using schedule() for, in a lot of cases now I am using t2dSceneObject::onTimer() which seems to be a new addition to TGB.
#9
06/06/2006 (1:26 pm)
Not so new, I see onTimer() in the reference doc from alpha 1. :) Might even have been in T2D 1.0 but I don't feel like reinstalling that to check. ;)
#10
the original thread is: www.garagegames.com/mg/forums/result.thread.php?qt=22272
@ Anthony - I could be wrong, but I think you made a mistake on the actual usage of the schedule. You said...
Schedule(milliseconds, simgroup, functionname, [parameter1, param2 . . . ])
and
simgroup is used to assign the schedualing to a certain simgroup
I believe the correct usage is...
Schedule(millisecs, eventDependsOnThis, functionname, [arg1, arg2 . . . ])
So say you were hitting someone with an instant fireball spell using...
spellDamageEnemy(%enemy, %damType, %amount);
and you wanted to schedule it for 5 seconds, your method is to use...
%player.schedule = schedule(5000, 0, "spellDamageEnemy", %enemy, %damType, %amount);
And if the enemy was deleted you would cancel it using...
if(isEventPending(%player.schedule)){ cancel(%player.schedule); }
My understanding was you could do that all at once using....
schedule(5000, %enemy, "spellDamageEnemy", %enemy, %damType, %amount);
And if the %enemy was deleted before the 5 seconds was up, the schedule would not execute at all because it was dependant upon the %enemy to exist . A better example would be if you had a projectile that could completely traverse the distance of your map in 2 seconds and you built a delete function to schedule a delete for 3 seconds after firing, then you would have something like....
Normally this function would be asking for errors and console spam
because it lacks proper checks
function deleteBullet(%proj)
{
%proj.delete();
}
but scheduled in this fashion.......
%proj = newBullet(){}
fire(%proj);
schedule(3000, %proj, "deleteBullet", %proj);
if the projectile collides and deletes 1 second after firing, then 2 seconds later the
schedule will find that %proj has already been deleted and the schedule will self cancel. If you use....
schedule(3000, 0, "deleteBullet", %proj);
Then you are going to get console spam from the .delete() unless you manually cancel the schedule.
Here's some examples of proper builds based on normal methods for Derk...
If you had a Bot that you wanted to follow a path after a 10 second delay instead of constantly following it, then instead of using..
%bot.followPath(%bot.path, -1);
you would use....
%bot.schedule(10000, "followPath", %bot.path, -1);
This of course could be a self canceling schedule as well. If the bot is deleted before the 10 seconds is up then the schedule will self cancel. Because essentially the engine see's...
%bot.schedule(10000, "followPath", %bot.path, -1);
and
schedule(10000, %bot, "followPath", %bot.path, -1);
as the same thing, the only difference is how they execute when the schedule is completed.
If you wanted to set a dead players shapeName to blank 5 seconds after he dies instead of instantly, you change...
%player.setShapeName(""); // instant
to either
%player.schedule(5000, "setShapeName", "");
or use a manual function setup like
function ClearName(%player)
{
%player.setShapeName("");
// some stuff here
// more stuff here
}
schedule(5000, %player, "ClearName", %player);
06/08/2006 (10:54 am)
I found a lengthy description of schedule() so here it is:the original thread is: www.garagegames.com/mg/forums/result.thread.php?qt=22272
@ Anthony - I could be wrong, but I think you made a mistake on the actual usage of the schedule. You said...
Schedule(milliseconds, simgroup, functionname, [parameter1, param2 . . . ])
and
simgroup is used to assign the schedualing to a certain simgroup
I believe the correct usage is...
Schedule(millisecs, eventDependsOnThis, functionname, [arg1, arg2 . . . ])
So say you were hitting someone with an instant fireball spell using...
spellDamageEnemy(%enemy, %damType, %amount);
and you wanted to schedule it for 5 seconds, your method is to use...
%player.schedule = schedule(5000, 0, "spellDamageEnemy", %enemy, %damType, %amount);
And if the enemy was deleted you would cancel it using...
if(isEventPending(%player.schedule)){ cancel(%player.schedule); }
My understanding was you could do that all at once using....
schedule(5000, %enemy, "spellDamageEnemy", %enemy, %damType, %amount);
And if the %enemy was deleted before the 5 seconds was up, the schedule would not execute at all because it was dependant upon the %enemy to exist . A better example would be if you had a projectile that could completely traverse the distance of your map in 2 seconds and you built a delete function to schedule a delete for 3 seconds after firing, then you would have something like....
Normally this function would be asking for errors and console spam
because it lacks proper checks
function deleteBullet(%proj)
{
%proj.delete();
}
but scheduled in this fashion.......
%proj = newBullet(){}
fire(%proj);
schedule(3000, %proj, "deleteBullet", %proj);
if the projectile collides and deletes 1 second after firing, then 2 seconds later the
schedule will find that %proj has already been deleted and the schedule will self cancel. If you use....
schedule(3000, 0, "deleteBullet", %proj);
Then you are going to get console spam from the .delete() unless you manually cancel the schedule.
Here's some examples of proper builds based on normal methods for Derk...
If you had a Bot that you wanted to follow a path after a 10 second delay instead of constantly following it, then instead of using..
%bot.followPath(%bot.path, -1);
you would use....
%bot.schedule(10000, "followPath", %bot.path, -1);
This of course could be a self canceling schedule as well. If the bot is deleted before the 10 seconds is up then the schedule will self cancel. Because essentially the engine see's...
%bot.schedule(10000, "followPath", %bot.path, -1);
and
schedule(10000, %bot, "followPath", %bot.path, -1);
as the same thing, the only difference is how they execute when the schedule is completed.
If you wanted to set a dead players shapeName to blank 5 seconds after he dies instead of instantly, you change...
%player.setShapeName(""); // instant
to either
%player.schedule(5000, "setShapeName", "");
or use a manual function setup like
function ClearName(%player)
{
%player.setShapeName("");
// some stuff here
// more stuff here
}
schedule(5000, %player, "ClearName", %player);
Torque Owner Tom Bushby
Example:
schedule(time, obj, function, arg1, arg2, ...);
Hope that helps,
- Tom.