Sleep function?
by Daniel Magnan · in Torque Game Builder · 01/25/2007 (11:32 am) · 3 replies
I'm looking for a function similar to sleep in Java. I want the script to pause for a set time before continuing. I have found schedule, but it does not serve my purposes. I do not want to set the action to be called for a later time and then continue on with the current code. Is there a common way to do such a thing in TS? I tried to create my own, but the engine doesn't like it and locks up.
//one of my attempts to create a sleep function
function utilitySleep(%time)
{
%sleepy = schedule(%time,0, wakeUp);
echo("asleep");
while(isEventPending(%sleepy))
{
//busy looping waiting for scheduled event to fire
}
//event has fired, should finish function...
}
function wakeUp()
{
echo("awake");
}
////////
I am assuming it locks up because this is not multi threaded and is spending the entire time in the loop and never has processing time to fire the event. If I put a safety counter on the loop, it will fire the event after the safety counter has stopped the loop.
Perhaps I am going about this the wrong way, and there is a better way to do this.
In this particular case I am moving a piece tile by tile on a tile map in a turn based game. However each move happens so fast that all you see is the final position. I want to see each individual move. Thus I want to place a short pause before the next move is done.
//one of my attempts to create a sleep function
function utilitySleep(%time)
{
%sleepy = schedule(%time,0, wakeUp);
echo("asleep");
while(isEventPending(%sleepy))
{
//busy looping waiting for scheduled event to fire
}
//event has fired, should finish function...
}
function wakeUp()
{
echo("awake");
}
////////
I am assuming it locks up because this is not multi threaded and is spending the entire time in the loop and never has processing time to fire the event. If I put a safety counter on the loop, it will fire the event after the safety counter has stopped the loop.
Perhaps I am going about this the wrong way, and there is a better way to do this.
In this particular case I am moving a piece tile by tile on a tile map in a turn based game. However each move happens so fast that all you see is the final position. I want to see each individual move. Thus I want to place a short pause before the next move is done.
About the author
#2
%time = 0;
%finish = 4000;
while(%time < %finish)
{
%time = %time + 1000;
schedule(%time, 0 , moveFunction);
}
After the loop finished and the moveFunctions were set to go off 1 sec apart, wouldn't the rest of the script after the loop run before all the schedules went off? I can redesign the code to work with this, but I'm just trying to understand the way that the schedule function works.
01/25/2007 (1:29 pm)
If I used schedule something like this:%time = 0;
%finish = 4000;
while(%time < %finish)
{
%time = %time + 1000;
schedule(%time, 0 , moveFunction);
}
After the loop finished and the moveFunctions were set to go off 1 sec apart, wouldn't the rest of the script after the loop run before all the schedules went off? I can redesign the code to work with this, but I'm just trying to understand the way that the schedule function works.
#3
01/25/2007 (4:13 pm)
Yeah, the "rest" of the script after the loop does run but you decide what the "rest" is. So if you don't want anything else to run after the loop, don't run anything after the loop. Once you finish your move, you can run more code as appropriate.
Torque Owner Ben R Vesco