I wanted to write a system that lets projectiles spawn other projectiles when they explode.
Here's what I got:
//part of a package with tons of other stuff in it too.
function projectileData::onExplode(%this, %obj, %pos) {
if(isObject(%this.subProjectile)) {
%inheritVel = vectorScale(%obj.getVelocity(), %this.subProjectile.velInheritFactor);
if(%this.subProjectileCount $= "")
%this.subProjectileCount = 1;
for(%i = 0; %i < %this.subProjectileCount; %i++) {
%vel = mCos(mDegToRad(getRandom(0, 359))) SPC mSin(mDegToRad(getRandom(0, 359))) SPC mCos(mDegToRad(getRandom(0, 179)));
%vel = vectorNormalize(%vel);
%vel = vectorScale(%vel, %this.subProjectile.muzzleVelocity);
%vel = vectorAdd(%vel, %inheritVel);
%vel = vectorClampF(%vel, 0, 200);
%p = new projectile() {
dataBlock = %this.subProjectile.getID();
initialPosition = %pos;
initialVelocity = %vel;
sourceObject = %obj.sourceObject;
sourceSlot = %obj.sourceSlot;
client = %obj.client;
};
missionCleanup.add(%p);
}
%obj.delete();
return;
}
parent::onExplode(%this, %obj, %pos);
}
You can add projectileData.subProjectileC ount and projectileData.subProjectile and it works quite well.
When I have projectile1 explode to projectile2, and projectile2 explode to projectile1, the engine crashes. As far as I can tell, it's not just some simple infinite loop grabbing a ton of memory problem as it might seem at first.
So far it hasn't crashed the first time a projectile in the loop explodes though.
Traces show nothing odd, dumping the projectiles shows nothing odd, I really don't have any clue why the engine crashes.
Oh, and here's the vectorClampF() I wrote for limiting the projectile velocity to 200.
function vectorClampF(%vector, %min, %max) {
return vectorScale(vectorNormalize(%vector), mClampF(vectorLen(%vector), %min, %max));
}