Game Development Community

HOWTO - Simulate a WAIT(10) command

by Chok Wei Tat · in Torque Game Builder · 10/17/2006 (5:29 pm) · 12 replies

Hi, i wonder howto simulate a WAIT() command like other language, and using schedule mayb hard for me to get the idea

And moreover, how to stop a schedule if i dont want the schedule anymore


Thanks

#1
10/17/2006 (8:39 pm)
$someschedule = 10;

schedule($someschedule, 0, bleh);

cancel($someschedule);

See game.cs functions onMissionEnded() and StartGame() for more details.
#2
10/17/2006 (9:10 pm)
Wouldn't it be?
$someschedule = schedule(10, 0, bleh);

cancel($someschedule);
Honestly I don't totally remember coz' I really never use the non-object version of schedule, but that's how it works with that...
#3
10/17/2006 (9:30 pm)
Well, yeah. The syntax might be a bit off. A clearer version can be found in game.cs
#4
10/18/2006 (6:19 am)
Sorry, i cant find any
Quote:
$someschedule = schedule(10, 0, bleh);
cancel($someschedule);
in my game.cs, are you guys mention in tourquegamebuilder/games//gamescripts/game.cs

I am using tgb indie standard
#5
10/18/2006 (6:21 am)
No, that was just an example. In TGE's game.cs in starter.fps they have a working schedule command using global variables.
#7
10/18/2006 (6:35 am)
Hey Matt, Paul, thanks for help, i master it :p

function testwait() {
	echo ("tw: start");
	$someschedule = schedule(4000, 0, "bleh");
        echo ("tw: end");
}

function bleh() {
	echo ("blur blur");
	cancel($someschedule);
}

result:
tw: start
tw: end

(after 4 seconds)
blur blur
#8
10/18/2006 (8:46 pm)
There is no need for a cancel() there. Cancel is used if you want to stop a schedule from being called. Just doing:
function testwait() {
	echo ("tw: start");
	schedule(4000, 0, "bleh");
        echo ("tw: end");
}

function bleh() {
	echo ("blur blur");	
}
Would work.
#9
10/18/2006 (10:16 pm)
Just remember a schedule will actually do that.. schedule a function to execute at a certain time. So in the above example it would print "tw: end" and proceed to run any other code immediately after the schedule command. Calling schedule() doesn't halt the programs execution...
#10
10/19/2006 (4:26 am)
Faced problem again.....

function gBoard::onRespawn(%this, %tileX, %tileY) {
	gBoard.setAnimatedTile(%tileX, %tileY, tmAnim);
	$someschedule = schedule(2000, gBoard.getId(), "spawn",%tileX, %tileY);     // wht should put here
}
function gBoard::spawn(%this, %tileX, %tileY) {
	echo ("spawing");
	gBoard.setStaticTile(0, 0, b @ getRandom(1,3) @ Imagemap, getRandom(0,7));
	cancel($someschedule);
}
Wht should be the code in the 3rd line of code in order to make the spawn function called after 2 seconds?

Once again thanks for the community, i really learn from it
#11
10/19/2006 (3:36 pm)
What you want is the following:

function gBoard::onRespawn( %this, %tileX, %tileY )
{
	gBoard.setAnimatedTile( %tileX, %tileY, tmAnim );
	%this.schedule( 2000, getId, "spawn",%tileX, %tileY );
}

function gBoard::spawn( %this, %tileX, %tileY )
{
	...
}

As paul said, you don't need the cancel. In fact, it isn't actually doing anything since the scheduled function has already been called (you know that because you're in it).
#12
10/19/2006 (9:58 pm)
Actually, there are some cut/paste/edit errors in the above, which trust me I know happens when you try to patch edit a previous post, especially one that comes from some minor misunderstandings of TorqueScript!

First, some minor notes:

since your function gBoard::onRespawn(...) is a namespace method, it is somewhat redundant and slightly misleading in usage to continue to use gBoard as an obect identifer. It works, and won't cause any serious issues, but isn't particularly portable or resuable. Instead, once within the function, you should be using the %this variable, like so:

function gBoard::onRespawn( %this, %tileX, %tileY)
{
  %this.setAnimatedTile( %tileX, %tileY, tmAnim );
  // note that the use of the named object tmAnim [i]also[/i] makes the code not 
  // particularly re-usable, but again is ok, just not best practice. Instead, you 
  // should be passing in the animation to be set as an argument, and using the 
  //argument instead of a named object

  %this.schedule(2000,"spawn", %tileX, %tileY);
  // note that the above statement is a namespace scope version of schedule, and 
  // therefore by definition does [i]not[/i] require the second parameter to be 
  // the objectID of the dependency object. Again by definition, the dependency 
  // object is in this case, %this, and this form of the schedule method is aware 
  // of the dependency already.
}

Some general theory points to keep in mind:

--In a namespace method, the first argument to the method, in common style known as the "%this" argument, contains the objectID of the object who's method is being called.

--the schedule capability comes in two flavors:
----namespace scope: it will be called as a namespace method, and by definition two things will be true:
------the method to be called must be a namespace method on the object being scheduled.
------the dependency object for the schedule is the object being scheduled.

example:

%myObjectID.schedule(5000, "someNamespaceMethod", %optionalMethodParams);


----global scope: it will be called as a global function, which also has two important items:
------by definition is not automatically dependent on a specific object. This form of the schedule capability requires an argument to indicate a dependency object, although it may have a value of zero (no dependency object in fact).
------again by definition, the function to be called must also be of global scope (not a namespace method).

example:

schedule(5000, %DependencyObjectID, "someGlobalFunction", %optionalFunctionParams);

As Thomas commented, the only reason to ever cancel a schedule is when you decide before the event happens that you don't want the scheduled event to happen after all. Schedule does not repeat automatically--it schedules a single call of the indicated method or function.