Author Topic: Two bullets being fired instead of one  (Read 1273 times)

Alright so I encounter this issue a couple times and I don't actually know why. This especially occurs on weapons that have spread in them. It probably has something to do with the parent

Code: [Select]
function SWA280CImage::onFire(%this,%obj,%slot)
{
%projectile = SWA280CProjectile;
%spread = 0.0015;
%shellcount = 1;


%obj.playThread(2, plant);
%shellcount = 1;
//%obj.spawnExplosion(QuakeLittleRecoilProjectile,"1 1 1");
           
for(%shell=0; %shell<%shellcount; %shell++)
{
%vector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
%velocity = VectorAdd(%vector1,%vector2);
%x = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%y = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%z = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
%velocity = MatrixMulVector(%mat, %velocity);

%p = new (%this.projectileType)()
{
dataBlock = %projectile;
scale = "1 1 1";
initialVelocity = %velocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
MissionCleanup.add(%p);
}
    Parent::onFire(%this,%obj,%slot);
    return %p;
}
If i remove the Parent::onFire at the bottom, the gun will fire properly with one bullet but all WeaponImage::onFire scripts will not work for it anymore. If I leave it, there will be two bullets coming out. Anyone know whats up?

its because parent::onfire by default spawns a projectile with no spread heading directly where the cursor is aiming. the only way you can fix this and also allow for the other standard onfire scripts to be called is to not assign a projectile datablock to the projectile var in the image. you'll have to fix the spread code to make sure its getting the right projectile datablock though, so it spawns a projectile.

the code snippet you have will work if you set the image projectile to "" since you hardcoded %projectile to the SWA280Projectile.

You may be able to make use of the weapon image's MinShotTime field. Set it to your firing delay in miliseconds, and set %obj.lastFireTime in your onFire to getSimTime(), and that should prevent the default system from firing any projectiles. What other WeaponImage::onFire functionalities did you plan to take advantage of, though? I can't remember whether packaging that function affects child methods or not without having to check, but I don't recall many people wrestling with this in the past so it seems like it'd be a non-issue either way in your case.

You may be able to make use of the weapon image's MinShotTime field. Set it to your firing delay in miliseconds, and set %obj.lastFireTime in your onFire to getSimTime(), and that should prevent the default system from firing any projectiles. What other WeaponImage::onFire functionalities did you plan to take advantage of, though? I can't remember whether packaging that function affects child methods or not without having to check, but I don't recall many people wrestling with this in the past so it seems like it'd be a non-issue either way in your case.
Overheat script i made. It's packaged into a WeaponImage::onFire which is called after the gun scripts are actually initialized.

Anyways, I found a solution. just packaged all the firing scripts, including projectile adding, into the same package and run a check on the image values

Overheat script i made. It's packaged into a WeaponImage::onFire which is called after the gun scripts are actually initialized.

Anyways, I found a solution. just packaged all the firing scripts, including projectile adding, into the same package and run a check on the image values
Wait, are you saying you took your weapon's individual onFire code and moved it into a package around the WeaponImage one? Please tell me that's not what you mean.

Wait, are you saying you took your weapon's individual onFire code and moved it into a package around the WeaponImage one? Please tell me that's not what you mean.
Yup. It actually works fine, using WeaponImage::onFire instead of ExampleWeaponImage::onFire. I'm making a weapon pack so it helps to assign a boolean to all the guns in the pack and just run a packaged weaponImage::onFire to check if its part of the pack and apply the stuff.

Only problem is that re-executing the server.cs file causes all the weapon pack projectile tracers to not exist anymore. The bullet still travels but it no longer has a model. Oh well.

Yup. It actually works fine, using WeaponImage::onFire instead of ExampleWeaponImage::onFire. I'm making a weapon pack so it helps to assign a boolean to all the guns in the pack and just run a packaged weaponImage::onFire to check if its part of the pack and apply the stuff.
While the default onFire should have offered support for spread and multiple projectiles, it doesn't, and now the standard is if you have to modify that process to add those things, add it in onFire and do not use the parent. You should either move your overheat code into the individual onFire methods, or into its own function(s) and call it from the onFires of the weapons that need it. Packaging WeaponImage::onFire is usually inadvisable because of situations like this. It should only be used when a weapon doesn't require any complexity to its projectile creation.