Game Development Community

Is there a WaitForGameTime() function?

by Tim Saunders · in Torque Game Builder · 11/27/2007 (7:57 am) · 7 replies

Hello me again,

I'm doing a game where I'd like a series of characters to move about a grid one by one. So each time one is moved then I need to wait until they have finished moving before calling the next one.

Something like this would be nice

$CharacterAtDestination = false;

MoveCharacter()

while($CharacterAtDestination == false)
{
WaitForGameTime(0.1);
}

MoveNextCharacter()

etc.

with the CharacterAtDestination flag being set from a callback when the character has reached his target square. Is this possible at all? Or is there another obvious way that I'm missing out on?

#1
11/27/2007 (9:32 am)
Hey Tim -

the problem w/ that is that the engine isn't executing at the same time as script; they have to take turns.
so a while loop like that will mean the engine would never get a turn.

so you have to design your code with that in mind.

why not have the destination-reached callback call moveNextCharacter() itself ?
#2
11/27/2007 (9:42 am)
Hey there,

The idea of the WaitForGameTime(0.1) idea is that the game will stop bothering to read the script for 0.1 seconds and then only come back to the script afterwards (whilst the engine is still running) and test the condition again.

The problem with calling the moveNextCharacter() from the callback is that then I'd need to make the arrays with all the data about the order of characters to move and the paths they are on global variables, since you cannot send that data straight to a callback function.
#3
11/27/2007 (9:52 am)
Generally callbacks yield better performance and cleaner code etc than polling for states,
but if you really can't pass the data through it, there is a way to poll periodically -

you can use a Schedule() to cause some script code to execute at a point in the future.
Altho that might necessitate globals as well.

in any event, something like

$gMyScheduleID = 0;
function myPollingFunction()
{
   cancel($gMyScheduleID);
   $gMyScheduleID = 0;
   if (checkCondition())
      doSomething();
   else
      $gMyScheduleID = schedule(100, 0, "myPollingFunction"); // 100 = 0.1 seconds
}

.. but again, this might not get around the problem of globals.
#4
11/27/2007 (11:06 am)
Tim, I am going to be a bit bold here and second what Orion has said. You are going about it completely wrong. Use the callback. That is the whole reason they exist. It is their sole purpose in life. And yes, you will need a stack/queue. That is their whole purpose in life. You are trying to reinvent the wheel.
#5
11/27/2007 (12:10 pm)
Hello thanks for your help.

I'm not really an experienced programmer so am no doubt going about it the wrong way. The scripting that I've done before has always involved waiting functions. When you say you should use a stack/queue do you mean you line up the functions you want to call in the stack/queue and then call them as necessary when the last one has finished?
#6
11/27/2007 (6:32 pm)
[quote]When you say you should use a stack/queue do you mean you line up the functions you want to call in the stack/queue and then call them as necessary when the last one has finished?[quote]

Yes. A queue in programming is just like what a Brit calls a queue and I, as an American, call a line. Things are lining/queueing up to be taken in turn. You may have seen them mentioned elsewhere as FIFO (First In First Out) or LIFO (Last In First Out). A simple way to do this in Torque Script is with an Array, if you need to preserve order. However, if you don't need to preserve order, you can use a SimSet.

One way to do this, would be to create a common behavior. Maybe call it MoveInOrder. When a character with that behavior moves, put it in a queue. When it is done moving, remove it from the queue and invoke the callback method. That callback method will inform other characters in the queue to see if it is their turn to move. Meanwhile, other characters will be triggered to move, will put themselves in the queue and see that their is another character in front of them. They will then wait in the queue until the get the callback to check to see if they are next in the queue.

Others may have different suggestions on how to do this. More than likely some of those will be better than mine. ;)
#7
11/30/2007 (6:27 am)
Hey thanks a lot for the explanation. I'm going to give it a try now.