Game Development Community

Any Trick to using TorqueScript packages?

by Chris Groot · in Torque 3D Professional · 04/26/2011 (2:02 pm) · 2 replies

I'm trying to use packages to swap between two different implementations for the same function but I am having an issue swapping between packages and wondering if anyone might be able to help me. I have defined the function the same for both packages, check to make sure both packages are valid packages before attempting to activate/deactivate them, and deactivate the first package before activating the second package. The result I get is that the first activated package definition gets used every time regardless of which package is activated. Wondering what I have done that may have caused that?
Package declarations:
package MainMenuPackage
{
   function InitializeSUITPage()
   {
      echo("Mainmenu suit");
      ChangePackage(MainMenuPackage2);
   }
};

package MainMenuPackage2
{
   function InitializeSUITPage()
   {
      echo("Mainmenu2 suit");
   }
};
Helper Function definition:

function ChangePackage(%PackageToChangeTo)
{
   if(!isPackage(%PackageToChangeTo))
      return;
   %OldPackage = $CurrentPackage;
   if(%OldPackage !$= "")
      deactivatePackage(%OldPackage);
   activatePackage(%PackageToChangeTo);
   $CurrentPackage = %PackageToChangeTo;
   InitializeSUITPage();
}

If I start by changing to MainMenuPackage the output will be recursive:
"Mainmenu suit"

Any ideas as to what is causing this oddness?

#1
04/26/2011 (2:25 pm)
If you call activatePackage()/deactivatePackage() from inside a packaged function, the change will only take place after the execution leaves the package. Your ChangePackage() function recurses further into the packaged function and will never hit the function in the newly activated package.

Try this:
function ChangePackage(%PackageToChangeTo)
{
   if(!isPackage(%PackageToChangeTo))
      return;
   %OldPackage = $CurrentPackage;
   if(%OldPackage !$= "")
      deactivatePackage(%OldPackage);
   activatePackage(%PackageToChangeTo);
   $CurrentPackage = %PackageToChangeTo;
   schedule(0,0, InitializeSUITPage); //schedules the execution, instead of recursing
}
#2
04/26/2011 (2:36 pm)
Ah, thank you that fixed my issue.