Help with object names and setTimerOn ()
by Shawn C. · in Torque Game Builder · 01/19/2006 (12:03 pm) · 9 replies
I'm trying to figure out the proper way to use setTimerOn and its callback onTimer. I have a few sprites and I want them to pulsate in size, as if they are breathing. So I set a periodic timer for each.
If I use the following, I get the desired effect, but this can't be the best way to call onTimer (), because assume I will have other different sprites or objects, each with their own timers and code to execute.
What should my function declaration look like? Certainly it won't be t2dSceneObject. How do I reference my sprite object specifically?
Thanks.
function CreateMySprite ()
{
%sp = new t2dStaticSprite() { scenegraph = t2dScene; };
%sp.setImageMap(imMySprite);
%sp.setTimerOn (1000);
}If I use the following, I get the desired effect, but this can't be the best way to call onTimer (), because assume I will have other different sprites or objects, each with their own timers and code to execute.
function t2dSceneObject::onTimer (%this)
{
...do something with my sprite
}What should my function declaration look like? Certainly it won't be t2dSceneObject. How do I reference my sprite object specifically?
Thanks.
#2
so when you do your comparison, you will have to do
if(%this.Name=="myPlayer")
because %this=="myPlayer" may resolve to be false... i dont reemmber if it is ALWAYS that way, or just under certain conditions.
-Jason
01/19/2006 (12:56 pm)
PS: important note: in torquescript, what %this is is actually just a string variable containing the ID (number) of the object. for example %this=="1284";so when you do your comparison, you will have to do
if(%this.Name=="myPlayer")
because %this=="myPlayer" may resolve to be false... i dont reemmber if it is ALWAYS that way, or just under certain conditions.
-Jason
#3
01/19/2006 (1:11 pm)
Jason, I appreciate the reply. I am curious though how %obj.setTimerOn () differs from schedule () in its functionality?
#4
Minor correction: it would be
to do a string comparison.
01/19/2006 (1:27 pm)
Quote:
PS: important note: in torquescript, what %this is is actually just a string variable containing the ID (number) of the object. for example %this=="1284";
so when you do your comparison, you will have to do
if(%this.Name=="myPlayer")
because %this=="myPlayer" may resolve to be false... i dont reemmber if it is ALWAYS that way, or just under certain conditions.
Minor correction: it would be
if(%this.Name $= "myPlayer")
to do a string comparison.
#5
use $= for string comparisons in torquescript!
01/19/2006 (2:10 pm)
@Stephen: oh yeah, i forgot about that.. that is a subtle but very very important correction :)use $= for string comparisons in torquescript!
#6
logically, i think it would be better to implement your own timing mechinism and have that checked during the OnWorldUpdate() callback, but again.. i'm not a torquescript expert so I dont know how much faster an implementation such as this would be (if faster at all)
and obviously, implementing this yourself is more work.. .setTimer() is a quick/easy callback eh.
01/19/2006 (2:13 pm)
@Shawn: i'm not sure the exact reason behind .setTimer() but i think it's just added for usability purposes (meaning it is not any better to do that)logically, i think it would be better to implement your own timing mechinism and have that checked during the OnWorldUpdate() callback, but again.. i'm not a torquescript expert so I dont know how much faster an implementation such as this would be (if faster at all)
and obviously, implementing this yourself is more work.. .setTimer() is a quick/easy callback eh.
#7
- Melv.
01/20/2006 (12:50 am)
The advantage of "setTimer()" (apart from a simpler syntax) is that it's a periodic timer unlike using "schedule" which is a one-shot timer, unless of course you reschedule in the callback.- Melv.
#8
01/22/2006 (2:32 am)
I'd add that the advantage of schedule() is that you can set a particular function for a particular object, so that function doesn't need to test which object is being referenced, as you must do in the onTimer() callback. So in part it's a matter of style, but I'd guess there's a little more overhead for comparing the name of the object multiple times versus rescheduling the callback.
#9
01/24/2006 (10:02 am)
Scott, I agree and that is what I chose to do (use schedule ()). Thanks
Torque Owner Jason Swearingen
to call different code for different objects, you need to hardcode the torquescript equivlant of a switch statement.
Sorry i am not a torquescript user, so i cant give you exact syntax (I wrote and use a c# wrapper of t2d)
but the psudocode for what you need to do:
function t2dSceneObject::onTimer (%this) { switch(%this.Name) { case "myPlayer": playerTimerCallback(%this); break; case "enemy": enemyTimerCallback(%this); break; default: otherTimerCallback(%this); break; } }the switch statement is in C# syntax, though if torquescript doesnt have switches, you can easily do the same thing with a bunch of IF statements.
Hope this helps you out,
good luck!
-Jason