Resolved-Template" and "Self-destruct" behavior conflict
by Christian Rademan · in Torque Game Builder · 10/24/2007 (11:50 pm) · 8 replies
I have a problem with behaviors. Other than the fact that i sometimes spell it as "behaviours".
I have a turret that has the "Timer shoots" behaviours set. This works. It fires my projectile.
My projectile has the "template" behavior set. Then I wanted my projectile to explode after a few seconds.
So I added the "Self destruct" behavior. Even though I have the "template" behavior set on my projectile it explodes when the level is loaded and my turret fires blanks.
Anyone else have this problem?
I have a turret that has the "Timer shoots" behaviours set. This works. It fires my projectile.
My projectile has the "template" behavior set. Then I wanted my projectile to explode after a few seconds.
So I added the "Self destruct" behavior. Even though I have the "template" behavior set on my projectile it explodes when the level is loaded and my turret fires blanks.
Anyone else have this problem?
#2
As I understand it "Timer shoots" clones the projectile you specifiy.
The problem is my projectile template object explodes and removes itself even with the "template" behaviour attached.
10/25/2007 (12:56 am)
No. The turret fires a few of them. As I understand it "Timer shoots" clones the projectile you specifiy.
The problem is my projectile template object explodes and removes itself even with the "template" behaviour attached.
#3
You can change the behavior to check if it's the original. Give the original one a dynamic field "Original" with the value true.
Then in the Self Destruct include and if ( !%this.owner.Original ) bla bla bla. I'm not familiar with these behaviors, but that should do the trick.
10/25/2007 (7:05 am)
Ah! Now I understood.You can change the behavior to check if it's the original. Give the original one a dynamic field "Original" with the value true.
Then in the Self Destruct include and if ( !%this.owner.Original ) bla bla bla. I'm not familiar with these behaviors, but that should do the trick.
#4
Here is what is happening.
The self destruct behavior starts it countdown as soon as the behavior instance is loaded. It has no awareness of a "template". I thought I could work around it but the template behavior sets the enabled field of the parent object to false. but it does not do it until the onlevelloaded callback. The problem is the timing. Your template object is loaded and the selfdestruct behavior is activated before the template behavior gets called to disable the object. So your template selfdestructs.
Ricardo's method wont work because when the template is cloned it copies all the dynamic fields so your clone says its original too.
10/25/2007 (7:17 pm)
I played around with this a bit and it is a chicken and egg scenario. Here is what is happening.
The self destruct behavior starts it countdown as soon as the behavior instance is loaded. It has no awareness of a "template". I thought I could work around it but the template behavior sets the enabled field of the parent object to false. but it does not do it until the onlevelloaded callback. The problem is the timing. Your template object is loaded and the selfdestruct behavior is activated before the template behavior gets called to disable the object. So your template selfdestructs.
Ricardo's method wont work because when the template is cloned it copies all the dynamic fields so your clone says its original too.
#5
10/26/2007 (3:25 am)
You can clone and then change the dynamic field in the clone. If there's a time issue, schedule the change.
#6
Ok I know exactly what you mean. I had this issue when I was playing around with some behaviours a while ago. You can work around this in a lot of ways but some just make your brain hurt. You guys are thinking about this too much ;)
The quickest solution for me was to add the life time of the projectile to timer shoots. eg.
This is basic house keeping that timer shoots needs IMHO... if you don't kill off those bullets somehow ie world limit hit or time etc TGB bombs out pretty hard eventually.
The fire function then needs to be modified slightly so it resembles the following:
So, basically we only set the lifetime on newly cloned created projectiles. Probably a good rule here is don't let a template object delete itself as it will only give you a headache ;)
Sometimes the easiest solution is to extend the original behaviours especially if the extension is useful and reusable.
Anyway, hope that helps.
10/26/2007 (1:09 pm)
Hi Christian,Ok I know exactly what you mean. I had this issue when I was playing around with some behaviours a while ago. You can work around this in a lot of ways but some just make your brain hurt. You guys are thinking about this too much ;)
The quickest solution for me was to add the life time of the projectile to timer shoots. eg.
%template.addBehaviorField(projectileLifeTime, "The lifetime of the projectile so we don't get too many of them", float, "2.0");
This is basic house keeping that timer shoots needs IMHO... if you don't kill off those bullets somehow ie world limit hit or time etc TGB bombs out pretty hard eventually.
The fire function then needs to be modified slightly so it resembles the following:
function TimerShootsBehavior::fire(%this)
{
if (%this.owner.enabled)
{
if (!isObject(%this.projectile))
return;
%projectile = %this.projectile.cloneWithBehaviors();
// your code here
%projectile.setLifeTime(%this.projectileLifeTime);
}
// your code here
}So, basically we only set the lifetime on newly cloned created projectiles. Probably a good rule here is don't let a template object delete itself as it will only give you a headache ;)
Sometimes the easiest solution is to extend the original behaviours especially if the extension is useful and reusable.
Anyway, hope that helps.
#7
10/26/2007 (7:13 pm)
Just change the selfdestruct behavior to not start the countdown immediately and instead add a new method like "startCountdown". Then after you clone a new projectile in the shoots behavior call start() !
#8
10/26/2007 (11:17 pm)
Thanks for all the help. I think this is pretty much resovled.
Ricardo Vladimiro
Default Studio Name