Why don't these schedule() functions work?
by Juan Aramburu · in Torque Game Engine · 05/22/2006 (12:35 am) · 7 replies
This is the main function:
Console Output:
The goal is to spawn a bot and every second it keeps checking for nearby players & shoots them if in view, but the schedules are not registered.
shootHimIfYouSeeHim() is declared as:
function spawnBot(%botName, %botSpawnPoint)
{
%player = AIPlayer::spawn(%botName, %botSpawnPoint);
%player.incInventory(crossbow, 1);
%player.mountImage(CrossbowImage,0);
%player.setInventory(CrossbowAmmo,10);
%player.setImageLoaded(0, true);
[b]schedule(1000, 0, "aiplayer::shootHimIfYouSeeHim", %botname);
schedule(1000, 0, "aiplayer.shootHimIfYouSeeHim", %botname);
schedule(1000, 0, "%player.shootHimIfYouSeeHim");
schedule(1000, 0, %botname @ ".shootHimIfYouSeeHim", %botname);[/b]
}Console Output:
[b]aiplayer::shootHimIfYouSeeHim: Unknown command. aiplayer.shootHimIfYouSeeHim: Unknown command. %player.shootHimIfYouSeeHim: Unknown command. bot1.shootHimIfYouSeeHim: Unknown command.[/b]
The goal is to spawn a bot and every second it keeps checking for nearby players & shoots them if in view, but the schedules are not registered.
shootHimIfYouSeeHim() is declared as:
function AIPlayer::shootHimIfYouSeeHim(%this)
{
//code here (it does work when called manually in the console
// -> spawnBot("bot1", LocalClientConnection.player.getPosition());
// -> bot1.shootHimIfYouSeeHim();
}
#2
05/22/2006 (7:14 am)
There are two versions of schedule: one for global functions, and one for namespace methods. Jaun, you were trying to use the global function version for namespace methods, and Stefan pointed out the correct way to do so.
#3
On the TDN Console Functions Part 10, I see:
It seemed to me that these were equivalent, guess not.
Also, regarding schedule(), it is unclear whether it sets up the event as recurring or a one time-only occurrence.
I was trying out %botName.shooHimIfYouSeeHim() because that seemed to work in the console. My AIPlayer::spawn() does a:
So if I was to call it with "bot1" as param1, bot1.shootHimIfYouSeeHim() worked.
05/22/2006 (9:18 am)
Thanks Stefan & Stephen.On the TDN Console Functions Part 10, I see:
objID.schedule( t , methodName, arg0, ... , argN ) and schedule( t , objID or 0, functionName, arg0, ... , argN )
It seemed to me that these were equivalent, guess not.
Also, regarding schedule(), it is unclear whether it sets up the event as recurring or a one time-only occurrence.
I was trying out %botName.shooHimIfYouSeeHim() because that seemed to work in the console. My AIPlayer::spawn() does a:
new AIPlayer(%botName) {
//initialization
}So if I was to call it with "bot1" as param1, bot1.shootHimIfYouSeeHim() worked.
#4
That definatly will not work in the console, as the argument is local.
One time only. If you place the schedule inside the function which is scheduled, you will get a looping one, in your example - every 1000 ms.
05/22/2006 (10:00 am)
Quote:
I was trying out %botName.shooHimIfYouSeeHim() because that seemed to work in the console.
That definatly will not work in the console, as the argument is local.
Quote:
Also, regarding schedule(), it is unclear whether it sets up the event as recurring or a one time-only occurrence.
One time only. If you place the schedule inside the function which is scheduled, you will get a looping one, in your example - every 1000 ms.
#5
05/22/2006 (3:46 pm)
Thank you, your advise is appreciated.
#6
05/22/2006 (5:02 pm)
Schedules are not recurring, but you can get it to be. Look at the following code as an example:// create a script object, as an example
%myObject = new ScriptObject() {};
// schedule a call to 'doSomething' 1 second later, the second arguement
// tells schedule to automatically terminate if %myObject gets deleted, the
// last arguement tells schedule that when we call 'doSomething' it needs
// to be passed %myObject as an arguement to it
$someThread = schedule( 1000, %myObject, doSomething, %myObject );
function doSomething( %anObject )
{
// if there is another event pending, cancel it.. just in case
if( isEventPending( $someThread ) )
cancel( $someThread );
echo( %anObject.getID() );
// schedule this event to happen again
$someThread = schedule( 1000, %anObject, doSomething, %anObject );
}
#7
It's there to allow you to decide to only execute your global script function if your dependent object still exists (wasn't deleted while waiting for the scheduled event to occur). For usage, if the global version has a non-null ObjectID, when the event matures (is ready to be processed), the handler will search the simulation for a valid object of that ID, and only execute if that object is found.
05/22/2006 (8:46 pm)
And for clarification, the "objID or 0" argument within the global function version of schedule is a dependency, not a namespace clarifier.It's there to allow you to decide to only execute your global script function if your dependent object still exists (wasn't deleted while waiting for the scheduled event to occur). For usage, if the global version has a non-null ObjectID, when the event matures (is ready to be processed), the handler will search the simulation for a valid object of that ID, and only execute if that object is found.
Torque Owner Stefan Lundmark
This is how your example should look:
function spawnBot(%botName, %botSpawnPoint) { %player = AIPlayer::spawn(%botName, %botSpawnPoint); %player.incInventory(crossbow, 1); %player.mountImage(CrossbowImage,0); %player.setInventory(CrossbowAmmo,10); %player.setImageLoaded(0, true); [b]%player.schedule(1000, "shootHimIfYouSeeHim");[/b] }We remove the second argument in schedule because it simply is not needed, and won't work with the above example. It is normally used to set a object.
Also, your %botname argument has no use as far as I can see and you did not even set it as a argument to AIPlayer::shootHimIfYouSeeHim so I removed it too. In any case, the above is how it is written.
Hope it helps! :)