Game Development Community

Schedule

by Toby Mckenzie · in Torque Game Builder · 08/10/2005 (12:49 am) · 10 replies

Hi,

I am probably being very dumb here, but I have a Class called Game

new ScriptObject (GameClass)
{
class = Game ;
gameState = "AttractMode" ;
level = 1 ;
player = null;
scoreManager = null;
levelData = null ;
enemyManager = null ;


};

and a function

function Game::setGameState(%this,%state)
{

}

In here I want to set up a schedule to call itself after a second, but if I do

schedule(1000,0,"setGameState",%this,"Ready") ;

I get an unknown method exception, how to I get schedule to call a method on the class?

Any help much appreciated.

#1
08/10/2005 (1:45 am)
I tried namespacing it to

"Game::setSchedule" but it still failed in the same way.

I don't like moving the code out to a function that does not belong to the Game class as it feels incredibly nasty and breaks encapsulation. Can't help but think if schedule does not support the ability to namespace calls it is a bit lacking in functionality.
#2
08/10/2005 (3:20 am)
Try this:
GameClass.schedule( 1000, setGameState, "Ready" ) ;

setGameState() will be invoked and supplied with the %this parameter automatically, no need to do this explicitly.

Alternatively you could use the onAdd() callback to invoke the scheduled call, in which case you could use something like this:
function Game::onAdd( %this )
{
   %this.schedule( 1000, setGameState, "Ready" ) ;
}
#3
08/10/2005 (5:41 am)
I tried

GameClass.schedule(1000,setGame,"Ready")

which sort of worked, except a new instance of the GameClass was being created.

Hmm, going to have to have try a few more approaches. Thanks for the help so far, anymore would be much appreciated.
#4
08/10/2005 (6:05 am)
Got it, as always it is the simple and obvious things that stump:

%this.schedule(1000,setGameState,"Playing");
#5
08/10/2005 (6:13 am)
Quote:
function Game::setGameState(%this,%state)
{

}

In here I want to set up a schedule to call itself after a second, but if I do
Hmm, I may have misread this the first time.
Do you intend setting up a recurring call to setGameState() returning it to the "Ready" state every second?
If so then adding this to the setGameState() method should work
%this.schedule( 1000, setGameState, "Ready" ) ;

As I read it, I thought you just wanted a one-time call to setGameState().

I can't see how any of the code suggestions would create another instance of the GameClass though :-(
#6
08/10/2005 (6:13 am)
Oh, okay I see you've solved it, ignore that last post then :-)
#7
08/10/2005 (10:29 am)
Yes, as you've noticed, schedule has multiple overloaded implementations. For the most part, when you want to accomplish a scheduled event on an object, you should always call schedule from within the namespace of that particular object, using the:

%this.schedule(...);

format.

schedule by itself is basically the global namespace, and while it does allow for an additional parameter (the second one), which is an objectID on which the scheduled event should depend (don't execute the schedule of the objectID is no longer valid), it can be more difficult to force it to be performed in the specific instantiation of a normal object. schedule in this syntax is normally reserved for functions (non-namespace defined commands), or other, more generic tasks.
#8
08/10/2005 (10:36 am)
98% of the time I use the format of
%this.schedule()

like Stephen explained better than I could usually you will use this, most of the time it simply makes more sense that way. Though here is an example of it used in the global format from the spacescroller demo included with T2D... since there is only one player it isn't required to attach it to an object, though it could have been.

this is in the "playerFire" function that activates when you press the key, it ends with

// Reschedule another fire.
	$playerFireSchedule = schedule( $playerFireRate, 0, "playerFire" );

then on the "PlayerFireStop" which activates when you release the key it runs this

// Stop Firing event if active.
	if ( isEventPending($playerFireSchedule) )
		cancel( $playerFireSchedule );

just an example of a global one used in the spacescroller
#9
08/10/2005 (12:03 pm)
Thanks for all the help, things are starting to come together quite nicely now
#10
08/10/2005 (12:59 pm)
Thanks for the tip on using global schedule Stephen. I create a group of objects that each add a schedule event when they're created. It's nice to attach the schedule events to the objects so that the schedule is cancelled if the object is removed.