Cleanup spawned object when going back to Editor
by Andy Hawkins · in Torque Game Builder · 02/09/2007 (7:23 am) · 8 replies
I have spawned lots of objects using this function.
Only thing is when I stop the game and drop back to the level editor in the console it's generating errors like this constantly, which tells me scheduled task "TestForCollision" is still running.
Because my objects are spawned this way I don't know (and don't care) what they are doing and it allows for many men to be spawned without explicitly tracking them.
So the question is, how can I detect which objects are actively scheduling functions in the scene graph and shut them down when I go back to the level editor? How do I detect when I am going back to the level editor as well?
function MyLevel::CreateMan(%sceneGraph)
{
...
%man[$Total_Men] = new t2dAnimatedSprite() {
sceneGraph = %sceneGraph;
animationName = "Man1_FallingAnimation";
canSaveDynamicFields = "1";
position = "0 0";
size = "16.000 16.000";
mountID = "9";
schedule1 = -1;
};
%man.schedule1 = schedule(300,0,"MyLevel::TestForCollision",%man);
...
}Only thing is when I stop the game and drop back to the level editor in the console it's generating errors like this constantly, which tells me scheduled task "TestForCollision" is still running.
MyGame/gameScripts/MyGame_Level.cs (148): Unable to find object: '6694' attempting to call function 'getPositionY'
Because my objects are spawned this way I don't know (and don't care) what they are doing and it allows for many men to be spawned without explicitly tracking them.
So the question is, how can I detect which objects are actively scheduling functions in the scene graph and shut them down when I go back to the level editor? How do I detect when I am going back to the level editor as well?
#2
02/09/2007 (3:24 pm)
@David - thanks I will give this a go - I see you are rapidly becoming the TorqueScript guru around these GG pages :)
#3
02/09/2007 (3:56 pm)
Couldn't you do the following (if your object is a scriptobject) since the object has the event id and will remove the schedule no matter when the object gets trashed, not just at endgame()?function MyClass::onRemove(%this)
{
cancel(%this.schedule1);
}
#4
02/09/2007 (4:29 pm)
Yep that worked! Cool :)
#5
The 'onRemove' is called when the object is removed from the SceneGraph -- which is called when 'endLevel' is performed -- which I believe is called by the level builders built-in 'esc' or 'stop' prior to setting you back to the Level Builder.
However, the reason I mentioned keeping track of your events in a list was simple -- not all your schedules will be attached to 'sceneObjects' which call 'onRemove' --
or well, not all designs call for this, anyhow ...
@Andy, I'm far from a 'guru', I just try to chime in here and there and help out if I can ... sort of a "treat others as you would expect them to treat you" logic ... I'm bound to have questions at some point ... and it would be nice if someone was helpful ;)
02/10/2007 (5:25 pm)
You could use the 'onRemove' as well -- schedules for objects are implicitly cancelled when the object is deleted, however, the current level builder system does not delete all loaded objects -- which is extremely apparent when your testing GUI's out ... as your GUI Editor will list your GUI multiple times over, with new ID's ... The 'onRemove' is called when the object is removed from the SceneGraph -- which is called when 'endLevel' is performed -- which I believe is called by the level builders built-in 'esc' or 'stop' prior to setting you back to the Level Builder.
However, the reason I mentioned keeping track of your events in a list was simple -- not all your schedules will be attached to 'sceneObjects' which call 'onRemove' --
or well, not all designs call for this, anyhow ...
@Andy, I'm far from a 'guru', I just try to chime in here and there and help out if I can ... sort of a "treat others as you would expect them to treat you" logic ... I'm bound to have questions at some point ... and it would be nice if someone was helpful ;)
#6
02/10/2007 (11:33 pm)
@David, that's a good philosophy to have - I try to do the same thing. You must have read my mind on the fxSprite thread I had because I hadn't looked at that for months, on the day I needed to review the thread and ask more questions you ended up providing the answers i needed - very cool!
#7
02/11/2007 (3:38 am)
@Andy, the fxSprite thread? :)
#8
www.garagegames.com/mg/forums/result.thread.php?qt=55772
02/11/2007 (6:28 am)
@David - this one...www.garagegames.com/mg/forums/result.thread.php?qt=55772
Associate David Higgins
DPHCoders.com
You can do this by keeping a word-list of scheduled events ... the %man.schedule1 field, for example stores the scheduled event id -- add this onto a global '$scheduled_events' word-list -- then in your 'endGame' method, found in the game.cs -- do something like this:
for(%x = 0; %x < getWordCount($scheduled_events); %x++) { cancel(getWord($scheduled_events, %x); }I'm pretty sure you can add the event to the world list using: $scheduled_events = $scheduled_events SPC %eventid and I think there is also an 'addWord' and 'removeWord' method you can call that may simplify things for you (if not, there easy enough to write on your own)
The "endGame" function is called whenever your game is "done" -- and it's always a good idea to clean up any "threads" you create --
Also, it may be noted, that when you exit to the Level Builder, it's the equivalent of "exiting your game", at which point, in a release, the engine would completely shutdown and cancel any running schedules itself ...