Initialize behaviors
by PeterB · in Torque Game Builder · 06/18/2008 (4:23 pm) · 4 replies
I've decided to start using Torsion fully. I've always enjoyed using it, but now I'm going to launch my games with it by clicking the Play button. This is so that I can make use of its full debugging capabilities (watch list, breakpoints, etc).
Separately, back when I always launched my games from the TGB level editor, I tended to initialize my behaviors with the functions 'onBehaviorAdd' and 'onAddToScene'. I noticed that 'onBehaviorAdd' is called twice - when you start the game, but also while using the level editor. This can makes it a bad place to do anything other than initialize variables.
But now that I'm (properly) launching my games through Torsion, I notice 'onAddToScene' is never called. So perhaps I should rely on 'onLevelLoaded'. That seems to be called with the Torsion launching method.
So can I simply depend on 'onLevelLoaded' for all initialization needs (for behaviors)?
Please give me any feedback including what you guys do. Thanks.
Separately, back when I always launched my games from the TGB level editor, I tended to initialize my behaviors with the functions 'onBehaviorAdd' and 'onAddToScene'. I noticed that 'onBehaviorAdd' is called twice - when you start the game, but also while using the level editor. This can makes it a bad place to do anything other than initialize variables.
But now that I'm (properly) launching my games through Torsion, I notice 'onAddToScene' is never called. So perhaps I should rely on 'onLevelLoaded'. That seems to be called with the Torsion launching method.
So can I simply depend on 'onLevelLoaded' for all initialization needs (for behaviors)?
Please give me any feedback including what you guys do. Thanks.
About the author
#2
I have behaviors that I sometimes attach to sprite in the level editor, but other times I attach those same behaviors "on the fly" with:
...so there is not one function that fits both situations, I guess.
And then there's cloning, which may be a third situation.
06/21/2008 (3:24 pm)
Oh, but if you instantiate sprites and their behaviors during gameplay (instead of setting them up in the level editor), 'onLevelLoaded' doesn't work. So 'onAddToScene' would be an obvious choice, except we know that function doesn't get called when launching your games with the Torsion/debugging method mentioned above.I have behaviors that I sometimes attach to sprite in the level editor, but other times I attach those same behaviors "on the fly" with:
%newBehave = ChaserBehavior.createInstance(); // make an instance of our behavior %newGuy.addBehavior(%newBehave);
...so there is not one function that fits both situations, I guess.
And then there's cloning, which may be a third situation.
#3
06/21/2008 (5:12 pm)
You could put your initialization code in 'onLevelLoaded', then for behaviors that you add in script, just make sure you call 'onLevelLoaded' yourself after creating/attaching it.
#4
///////////////// -- below is code that creates and attached ChaserBehavior.
06/21/2008 (5:29 pm)
Right, so you can call 'onLevelLoaded' yourself. Or at least both 'onLevelLoaded' and the line after you create/attach the behavior would call your own function.function ChaserBehavior::onLevelLoaded(%this)
{
%this.initMe();
}
function ChaserBehavior::initMe(%this)
{
/// INIT code here...
}///////////////// -- below is code that creates and attached ChaserBehavior.
%newBehave = ChaserBehavior.createInstance(); // make an instance of our behavior %newGuy.addBehavior(%newBehave); %newBehave.initMe();
Torque Owner Kevin James