Game Development Community

Question on the schedule function

by Zachary Woodard · in Technical Issues · 10/21/2011 (1:15 pm) · 2 replies

I have working code, but I'm trying to UNDERSTAND the code (go figure :)

I have an updateMovement function that is a part of a class (playerShip)

function playerShip::updateMovement()
{
echo($pShip.linearThrust);
if ($pShip.keyThrust == true)
{
// Add the linearThrust value to the linearSpeed
$pShip.linearSpeed = $pShip.linearSpeed + $pShip.linearThrust;
/*if ($pShip.linearSpeed > $pShip.maxVelocity)
{
$pShip.linearSpeed = $pShip.maxVelocity;
}*/
$pShip.setImpulseForcePolar($pShip.getRotation(), $pShip.linearSpeed);
//echo("Blaggo:" @ $pShip.linearSpeed);
schedule(50, 0, StupidProxyCauseIDontKnowHowToResolveTheFunction);
}

}

I have validated through dump() that it IS a part of the class. If you look at my schedule command, I had to use a proxy function:

function StupidProxyCauseIDontKnowHowToResolveTheFunction()
{
playerShip::updateMovement();
}

for the schedule command to work properly. Without the proxy function, the script would not work. If I used the line

schedule(50, 0, playerShip::updateMovement);

I got a parse error, if I used

schedule(50, 0, updateMovement);

I got an unknown command error. Can anyone explain to me WHY I couldn't resolve the function through the schedule command?

Thanks in advance,

-Snackin 'N00B' Zack

#1
10/21/2011 (7:56 pm)
schedule(50, 0, playerShip::updateMovement) throws a parse error because you can't pass in a class method like that.

schedule(50, 0, updateMovement) throws an unknown command error because the updateMovement function only exists within the playerShip namespace.

There are actually two versions of the schedule function: one designed for global functions (i.e. function myFunc(){...}) and one designed for class methods (i.e.e function myClass::myFunc(){...}). In your case you have a class (playerShip) and need to call one of its methods (updateMovement) so you'll need to use the second version of schedule, like so:

$pShip.schedule(50, updateMovement);

The above code will call $pShip's schedule() method. After 50ms, $pShip::updateMovement() will be called.
#2
10/22/2011 (6:53 am)
EXCELLENT! Thanks for taking the time to explain it and for the lightning fast response.

- Snackin "N00b" zack