Game Development Community

Multiple projectiles

by James Hill · in Torque Game Engine · 10/06/2004 (7:48 pm) · 7 replies

Hey guys, I'm trying to get a "flamethrower" like spell effect. I'm trying the spread shot method, but only one projectile is created instead of multiple as specified with the for loop. Anybody know what I did wrong in my code?

function FrostImage::onFire(%this, %obj, %slot)
{

%projectile = %this.projectile;
%player = %client.player;
if ($firstPerson)
%scanTarg = ContainerRayCast (%cameraPoint, %rangeEnd, %searchMasks, %player);
else
%scanTarg = ContainerRayCast (%cameraPoint, %rangeEnd, %searchMasks);

for(%index = 0; %index < 4; %index++)
{
// Determine initial projectile velocity based on the
// "gun's" muzzle point and the object's current velocity
%muzzleVector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%muzzleVelocity = VectorAdd(
VectorScale(%muzzleVector, %projectile.muzzleVelocity),
VectorScale(%objectVelocity, %projectile.velInheritFactor));

// if(%projectile.projectileSpread) {
%spread = 15/1000;//%projectile.projectileSpread;
%x = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
%y = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
%z = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
// Alter our projectile vector with our spread matrix
%muzzleVelocity = MatrixMulVector(%mat, %muzzleVelocity);
}


// Create the projectile object
%p = new (%this.projectileType)() {
dataBlock = %projectile;
initialVelocity = %muzzleVelocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
echo("Projectile Launched");
MissionCleanup.add(%p);
return %p;
} // end for loop
}

#1
10/06/2004 (9:44 pm)
You are returning before the end of your loop


return %p;


It never gets a second chance
#2
10/07/2004 (4:56 am)
I noticed that this morning, but I moved that and now it locks up when I try to fire. I check the console log and it echoes "Projectile Launched" many times. There's gotta be something else wrong with the code.
#3
10/07/2004 (4:58 am)
Looks to me like the line:

// if(%projectile.projectileSpread) {

is commenting the first brace, so the brace 8 lines lower (which was originally closing the "if") is now closing the for loop. The new datablock is only created for the last values out of the for loop. But Gonzo is right, if you either uncommented the "if" line or commented the additional brace, you would then exit the loop at "return" prematurely.

But maybe I'm missing something...wouldn't the additional brace at the end cause an error, or is it simply ignored? Or is this not the *exact* code that is giving the problems?
#4
10/07/2004 (5:32 am)
I'm not sure what I fixed, but it's fixed. I did some switching around this morning and noticed that I changed the update part of the for loop to $index (typo), then changed it back and it started working. Not sure what was up. Thanks guys.
#5
10/07/2004 (6:19 am)
@ James

Could you show us a screen shot?

Nathan.
#6
10/07/2004 (6:26 am)
Just so you know....

if ($firstPerson)
%scanTarg = ContainerRayCast (%cameraPoint, %rangeEnd, %searchMasks, %player);
else
%scanTarg = ContainerRayCast (%cameraPoint, %rangeEnd, %searchMasks);


As long as you are playing on the server(single player) this will work, if you open this up for LAN or Net(multiplayer) play the 'else' statement is the only one that will work because a server is never in first person mode.
#7
10/07/2004 (3:21 pm)
Thx guys, I got the spread working but haven't finished the effect. It's actually the "frost" spell i'm working on. I just wanted to create a cone like weapon. When I get it completely finished I'll put up a screenshot for you Nathan. But it might be a while, got a lot of things to do at same time.

thx again guys, Eric, I think I did what you said, commented out that brace, but not sure. :)