Game Development Community

Stalling a script?

by Chris "DiGi" Timberlake · in Torque Game Engine · 08/10/2005 (1:04 pm) · 6 replies

Is it possible to stall a script, like with HL you can do (wait;)? I want to wait a few seconds before doing the 2nd stage of something in an if statement.

#1
08/10/2005 (1:17 pm)
That will probably freeze the application for as long as you're waiting. Is that what you want to happen? If not, then see if it's feasible for you to write the 2nd stage code into a seperate function and call the schedule function there instead to schedule the new function to execute after your wait period.

Here's an example from starter.racing's game.cs script file that uses schedule() to count down from 3, 2, 1, then start the race:

function countThree()
{
   // Tell the client to count.
   for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
      %cl = ClientGroup.getObject( %clientIndex );
      commandToClient(%cl, 'SetCounter', 3);
   }

	schedule(1000, 0, "countTwo");
}

function countTwo()
{
   // Tell the client to count.
   for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
      %cl = ClientGroup.getObject( %clientIndex );
      commandToClient(%cl, 'SetCounter', 2);
   }

	schedule(1000, 0, "countOne");
}

function countOne()
{
   // Tell the client to count.
   for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
      %cl = ClientGroup.getObject( %clientIndex );
      commandToClient(%cl, 'SetCounter', 1);
   }

	schedule(1000, 0, "startRace");
}

function startRace()
{
   // Inform the client we're starting up
   for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
      %cl = ClientGroup.getObject( %clientIndex );
      commandToClient(%cl, 'GameStart');

      // Other client specific setup..
      %cl.lap = 0;
      %cl.nextCheck = 1;
      %cl.setControlObject(%cl.car);
      %cl.camera.setFlyMode();
   }

   // Start the game timer
//   if ($Game::Duration)
//      $Game::Schedule = schedule($Game::Duration * 1000, 0, "onGameDurationEnd" );
   $Game::Running = true;
   
   $Game::Running = true;
}
#2
08/10/2005 (10:04 pm)
I'm talking about mid statement, in an if statement.
#3
08/11/2005 (10:48 am)
I know what you meant.. Here's what I meant...

function YourFunctionNow()
{
	%pause = DoSomething();

	if ( %pause == true )
	{
		// I want to wait here for a time
		%waitTime = 1000.0f;
		Wait( %waitTime);	// ficticous function

		// do more stuff
		// ...

	}

	DoSomethingElse();
}


Your fuction split up how I was suggesting:

function YourFunctionNow()
{
	%pause = DoSomething();

	if ( %pause == true )
	{
		%waitTime = 1000.0f;	// milliseconds
		schedule( %waitTime, 0, "DoMoreStuff");
	}
}


function DoMoreStuff()
{
	// do more stuff
	// ...
	
	DoSomethingElse();
}



Or you can use threads.. see class Thread in platformThread.h
#4
08/11/2005 (10:54 am)
The only way I can think of is to either use schedules or write your own C++ script function which will let you do this.
#5
08/11/2005 (11:42 am)
Another way to do it, depending on what you're doing, is to use something like the AI manager where you're basically interpreting a script within a script. It's like giving your objects a list of things to do. More of an event driven approach, rather than the procedural approach of Torque Script.

For example (modified from a NWN example, but simplified a little)
function NPC::onStartScene()
{
   while(IsSceneActive())
   {
      oPatron = GetNearestCreature();
      if(!oPatron.isObject())
         return;
      ActionMoveToObject(oPatron);
      ActionSpeakString("What's your poison?");
      ActionWait(2.0);

      if (!DrinkOrder())
      {
         ActionSpeak("Suit yourself.");
         ActionWait(1.0);
         continue;
      }
      MoveToBar();
      ActionWait(2.0);
      ActionMoveToObject(oPatron);
      ActionSpeakString("That'll be 2 copper, please.");
      oPatron.schedule(oPatron, ActionGiveAnimation);
   }
}

It's actually very easy to do... except without modifying or creating your own scripting engine, you'd have to do conditionals like this:

BeginConditionalLoop();
   IsSceneActive();
   BeginLoop();
      BeginConditional();
      DrinkOrder();
      IfTrue();
         // Don't really do anything
      IfFalse();
         ActionSpeak("Suit yourself.");
         ActionWait(1.0);
         ContinueLoop();
      EndConditional();
   EndLoop();

But... I'd wager that you're not really looking for something like that.

Edit: Oh, and variables would be different too. It sounds like a fun task, but I really think it requires a new scripting language that coexists with the current one, and that's the language you'd expose to your end-user base that's interested in creating RPG style modules.
#6
08/11/2005 (12:15 pm)
Thanks guys! I think this should work!