Game Development Community

Issues with scheduling

by USC - IMD student 2 · in Torque Game Builder · 06/11/2008 (7:44 pm) · 2 replies

I got tired of banging my head on the keyboard and am asking for help. Sorry in advance if I've gone and done something stupid.

I have a boomerang-like object mounted on the player.

It needs to return to the player and remount if a certain amount of time (3 seconds, let's say) passes to prevent it from getting lost. If it collides with the player all on its own, it remounts and is ready to launch again. The timer should re-set and the next time it launches, 3 seconds again.

Here's what I did:

I have a function to launch the boomerang, sort of like this

function Boomerang ();
{
$boomerang.dismount();
$boomerang.SetLinearVelocity(%xcoord, %ycoord);
$returnEvent = schedule(3000, 0, "returnToMe");
}

and then the return function

function returnToMe();
{
echo ("return to me!");
%pos = $body.getPosition();
$boomerang.moveTo(%pos, 140, false, false, false, 1);

}

and in the OnCollision for the player object

if (isEventPending( $returnEvent ))
{
cancel( $returnEvent );
echo ("I'm canceling the event");
}



Here's the problem:

It's not canceling the scheduled return. If I get it back in 2 seconds and launch again, it immediately zips back after 1 second, not 3.

For some reason, it's doing two things I can't explain.

1) I see the "return to me!" debug printed between 4 and 10 times on a single throw in rapid succession.
2) It prints the cancel debug, but it doesn't seem to be actually canceling the event.

help? The documentation on schedules isn't helping. It just makes me want to burn everything down around me.

#1
06/11/2008 (9:18 pm)
I'm not entirely sure if I understand what's going wrong, but I see some potential problems in the code. First, if you're getting lots of "return to me!"'s on a single throw, something is wrong. Shouldn't that only happen once...after the 3 seconds? I'd add some checks into the functions so that a throw can only happen if the boomerang is returned. You could use the schedule for this, just skip the throwing code if the event is pending. Having multiple schedules is probably your problem, because $returnEvent is getting set many times to the most recent schedule, so only one of them gets canceled. The other ones cause the behavior you want.

Hope that made sense and I understood the problem ;)
#2
06/12/2008 (3:32 pm)
That did make sense. Thanks.

I did like you suggested (my next step was going to be canceling the fire function if it had already fired anyway but this was a far more efficient solution.)

The ten or so extra echoes dried up right when I did it, and I was posting a bug that I couldn't get locked down but when checking the post for typos I found a stupid idiot mistake I made instead - now it works. I've edited the post to reflect that I'm happy -- hooray.

I have a feeling I may have another question later, but thanks!