Game Development Community

How do I insert a pause in my script?

by Chris Vallance · in Torque Game Builder · 04/11/2006 (2:34 pm) · 12 replies

I want to introduce a timed delay in my code, but I'm not sure how to do it. I thought of using "schedule" but, if I'm not mistaken, this will set a delay before calling a new function. I want to place a delay within a function. Any ideas?

#1
04/11/2006 (2:50 pm)
You can use 2 functions:

function firstPart()
{
   //do stuff before delay

   schedule( %delayInMilliseconds, 0, "secondPart" );
}

function secondPart()
{
   // stuff after delay
}
#2
04/11/2006 (3:04 pm)
The trouble I've found with this method is that I'm trying set up behaviors in my onCollision function, so I'm using %srcObj and %dstObj as my variables. Once I jump over to a new function, these variables no longer work. Is there some way around this?
#3
04/11/2006 (3:26 pm)
You can pass values in schedule like this

function firstPart()
{
   //do stuff before delay

   schedule( %delayInMilliseconds, 0, "secondPart", 10);
}

function secondPart(%val)
{
   // stuff after delay
   // %val will be 10
}
#4
04/11/2006 (3:27 pm)
You can also call schedule on an object

function t2dSceneobject::firstPart(%this, %this)
{
   //do stuff before delay

   %this.schedule( %delayInMilliseconds, "secondPart", 10);
}

function t2dSceneobject::secondPart(%this, %val)
{
   // stuff after delay
   // %val will be 10
}
#5
04/11/2006 (3:35 pm)
Okay, this may sound like a really dumb question, but what does %this mean? I see it all over the place, but I'm not sure what it's for.
#6
04/11/2006 (4:37 pm)
This might give you some hints:

http://cplus.about.com/od/beginnerctutorial/l/aa081902a.htm

It's about c++ but the concept is the same.
#7
04/19/2006 (7:02 pm)
How would you handle a pause if you needed it in the middle of a 'for' loop?

I've been tried doing it with the two function, but it seems like schedule just queues up the function calls and so the loop just blasts through with no pauses.
#8
04/19/2006 (9:19 pm)
function t2dSceneobject::firstPart(%this, %this)
{
  // Do stuff before "for-loop"
  %this.schedule( <delayInMilliseconds>, "theForLoop", 0);
}
function t2dSceneobject::theForLoop(%this, %val)
{
  // %val will be 0
  // Do stuff inside "for-loop"
  if( %val < <maxLoopIterations> )
    %this.schedule( <delayInMilliseconds>, "theForLoop", %val + 1);
}
#9
04/20/2006 (1:47 am)
Thats somehow a quite complicate way if the scripting does faked multithreading anyway and thus should know of pauses anyway ...
No simpler way like Pause(duration) to put the actual function to sleep for duration milliseconds?
#10
04/20/2006 (7:03 am)
Scripting does no multithreading of any kind. If you put a sleep-like command into a script, it will pause your whole game, and any effect you expect to accomplish with a delay in a for-loop will never be seen.

Really, the code isn't that hard to get used to. In fact, it makes all kinds of effects a whole lot easier to code. For example, I'll often have a score text field that runs up by steps of 5 to the current score. I can set up a global variable with the new score and then use schedule to add or subtract 5 if the current score is different from the new score. The delay in my "for-loop" is necessary so the GUI can update.
#11
04/20/2006 (7:15 am)
If you wanted to do have a single function, similar to the second one William posted, you could do:

$loops = 0;
$maxLoops = 10;

function loopAround(){
  if($loops < $maxLoops){
    // Do some stuff
    // 2500 -> milliseconds

    $loops++;
    schedule(2500,0,loopAround);    
  }
}

loopAround();
loopAround() would be called ten times in total, and take 25000 milliseconds to do ten loops.
#12
04/20/2006 (10:12 am)
William: In that case it would be a quite strange script engine ... didn't know that ... I assumed that faked multithreading is common in scripting engines to prevent locks of the whole system ...
And if I think of schedule, TGBs scripting engine must be fake multithreaded (through event queues or how it might be done) as well as it otherwise could not execute the script at a later date, as a different script is already running.

So if you can put a schedule script in an event ... where is the problem with pause(milliseconds) to put it in the same event queue? The only thing needed in addition to schedule would be a "where to jump back in" handling ...