Game Development Community

Bug? StaticShape continuing functions on new level

by Steve Acaster · in Torque 3D Professional · 08/20/2009 (1:27 pm) · 1 replies

If you have a StaticShape which plays a function, especially a scheduled function, and you load a new mission, the scheduled functions keep calling - but can't find the object because they are not in this newly loaded level.

The obvious way to prevent this is for the functions to check that the object exists, and then not do anything if it doesn't. Problem solved.

function dofunction1(%this,%obj)
{
	if(isObject(%obj))
		{
                %obj.dosomething1();
		%this.schedule(3000,dofunction2, %obj);
		}
	else
		{
		//chill
		}

function dofunction2(%this,%obj)
{
	if(isObject(%obj))
		{
                %obj.dosomething2();
		%this.schedule(3000,dofunction1, %obj);
		}
	else
		{
		//chill
		}
}

I was wondering if there was a better way of stopping functions? I noticed that StaticShape doesn't use MissionCleaup.

As I use an onAdd function to start the process, should I be using an onRemove function to end it? Is there an onRemove function and would it be called when loading a new level?






Just on a clean up.
moved to resolved.

#1
08/20/2009 (1:43 pm)
There is an onRemove that would be called when you load a different level. And there is a ConsoleFunction "cancel( %schId )". But I would try to do it this way...

function MyStaticShapeData::onAdd( %this, %obj )
{
   %obj.schedule( 3000, update );
}

function StaticShape::update( %this )
{
   // do stuff.
   %this.schedule( 3000, update );
}

Since the schedule is associated with the object, it will be canceled automatically if that object is deleted.

If you need different instances of your StaticShape to update differently, you might need to give them names, and put the update callback within the object-name namespace ( and then probably test isMethod on "update" ) before making the initial schedule ).