Alternating fire.
Moderator: Moderators
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Alternating fire.
Not really sure what to call this, but I've been putting this off for months now and I think it's about time someone showed me how to do it.
How do I animate my weapons to fire in "alternating fire". Example: two guns on a turret. Instead of them both firing at once, I want to make them fire like left, right, left, right, ect.
At the moment all of the units in CT fire all their weapons at once (or when they can)
An example of the script would be best.
Thanks.
How do I animate my weapons to fire in "alternating fire". Example: two guns on a turret. Instead of them both firing at once, I want to make them fire like left, right, left, right, ect.
At the moment all of the units in CT fire all their weapons at once (or when they can)
An example of the script would be best.
Thanks.
Re: Alternating fire.
It's pretty easy, lets say you have two flare pieces, flare1, flare2; one for each of the barrels. You have one weapon which alternates firepoints via the unit script.
** snip, this code was wrong **
As usual, no guarantees of plug-and-play.
Also, this would probably make a good question for Q&A.
** snip, this code was wrong **
As usual, no guarantees of plug-and-play.

Also, this would probably make a good question for Q&A.
Last edited by FLOZi on 19 Nov 2010, 18:24, edited 1 time in total.
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Re: Alternating fire.
Neither of those were plug and play, and I don't really understand what's going on in either one to try and alter it so it would work. No worries though, I appreciate the quick response.
http://code.google.com/p/conflictterra/ ... ruiser.lua
There's an example of a unit I'm thinking of putting this in. I'd like weapons 1 and 2 to be the alternating ones. (Same for 3 and 4, but if I can get it figured out for 1 and 2 I can do the rest)
I thought about putting it on the Q&A page, but when I was scolded for not posting in the form of a question I figured I did not understand the purpose of the site, and should therefore stay away.
http://code.google.com/p/conflictterra/ ... ruiser.lua
There's an example of a unit I'm thinking of putting this in. I'd like weapons 1 and 2 to be the alternating ones. (Same for 3 and 4, but if I can get it figured out for 1 and 2 I can do the rest)
I thought about putting it on the Q&A page, but when I was scolded for not posting in the form of a question I figured I did not understand the purpose of the site, and should therefore stay away.
Re: Alternating fire.
Swapping actual weapons is a bit trickier. Looking at the unitdef, weapon 1 and 2 are using the same weapondef. Do you want them to be able to target independently of one another? If the answer is no, then the best solution is to half the reload time and use just 1 weapon. (i.e. don't reinvent the wheel)
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Re: Alternating fire.
No, i'd prefer them to target the same thing.
The unit is set up the way it is because I didn't know how to do the alternating fire. This was how I got two missiles to come out of a tank months and months ago, and so I've used this janky method.
I'll toy with the unit def. I guess I should say when it didn't work before this is the error it gave me:
[ 304] QueryWeapon: bad return value, expected number
Sorry, it's been a while.
The unit is set up the way it is because I didn't know how to do the alternating fire. This was how I got two missiles to come out of a tank months and months ago, and so I've used this janky method.
I'll toy with the unit def. I guess I should say when it didn't work before this is the error it gave me:
[ 304] QueryWeapon: bad return value, expected number
Sorry, it's been a while.
- Forboding Angel
- Evolution RTS Developer
- Posts: 14673
- Joined: 17 Nov 2005, 02:43
Re: Alternating fire.
Yes, it is, good thing for him, someone has already done the hard workFLOZi wrote:Swapping actual weapons is a bit trickier.

Code: Select all
RestoreAfterDelay() // restore function to turn the turret and so forth back to start
{
sleep 3000;
turn turret to y-axis <0> speed <60>;
turn barrel1 to x-axis <0> speed <30>;
}
AimWeapon1(heading, pitch) // single weapon with 2 fires this tell sit what to do while aiming
{
if ( get IN_WATER(0) == 1 )
{
return (0);
}
signal SIG_AIM;
set-signal-mask SIG_AIM;
turn turret to y-axis heading speed <500>;
turn barrel1 to x-axis <0> - pitch speed <300>;
wait-for-turn turret around y-axis;
wait-for-turn barrel1 around x-axis;
start-script RestoreAfterDelay();
return (1);
}
FireWeapon1() // what do while firing, fires 1 barrel then the next , and resets
{
emit-sfx shot from firepoint1;
}
AimFromWeapon1(piecenum) // where it aims the weapon from
{
piecenum = barrel1;
}
QueryWeapon1(piecenum) // where the shot is called from
{
piecenum = firepoint1;
}
AimWeapon2(heading, pitch) // single weapon with 2 fires this tell sit what to do while aiming
{
if ( get IN_WATER(0) == 0 )
{
return (0);
}
signal SIG_AIM_2;
set-signal-mask SIG_AIM_2;
turn turret to y-axis heading speed <500>;
turn barrel1 to x-axis <0> - pitch speed <300>;
wait-for-turn turret around y-axis;
wait-for-turn barrel1 around x-axis;
start-script RestoreAfterDelay();
return (1);
}
FireWeapon2() // what do while firing, fires 1 barrel then the next , and resets
{
emit-sfx shot from firepoint1;
}
AimFromWeapon2(piecenum) // where it aims the weapon from
{
piecenum = barrel1;
}
QueryWeapon2(piecenum) // where the shot is called from
{
piecenum = firepoint1;
}
Edit... OH, you want to alternate fire between two barrels? That's easy. Thank smoth for this clever trickery (this method is far superior to the other ways that are commonly used around here).
Code: Select all
Create() // tells it what to do on creation
{
shooting_num=1;
}
RestoreAfterDelay()
{
sleep 3000;
turn turret to y-axis <0.000000> speed <35.005495>;
turn barrel1 to x-axis <0.000000> speed <15.000000>;
}
AimWeapon1(heading, pitch) // single weapon with 2 fires this tell sit what to do while aiming
{
signal SIG_AIM;
set-signal-mask SIG_AIM;
turn turret to y-axis heading speed <255>;
turn barrel1 to x-axis <0> - pitch speed <150>;
wait-for-turn turret around y-axis;
wait-for-turn barrel1 around x-axis;
start-script RestoreAfterDelay();
return (1);
}
FireWeapon1() // what do while firing, fires 1 barrel then the next , and resets
{
if(shooting_num==1)
{
emit-sfx 1024 from firepoint2;
}
if(shooting_num==2)
{
emit-sfx 1024 from firepoint1;
}
shooting_num=shooting_num+1;
if( shooting_num == 3)
{
shooting_num=1;
}
}
AimFromWeapon1(piecenum) // where it aims the weapon from
{
piecenum = barrel1;
}
QueryWeapon1(piecenum) // where the shot is called from
{
if (shooting_num==1)
{
piecenum=firepoint1;
}
if (shooting_num==2)
{
piecenum=firepoint2;
}
}
Re: Alternating fire.
S44 tanks do something similar but I thought I'd find out if that's what he really wanted before pasting a huge script in here...
As for your second script, that is entirely the standard normal way of doing it. I was just pretty tired last night and screwed up my examples as I was thinking in COB (forgot that lua returns the piece rather than assigning them
).
Theoretically it should work doing it all in QueryWeapon as I did before (Query being called just before / as the weapon is fired, and Fire afterwards) but off the top of my head I'm not 100% sure there won't be any issues doing it that way.

As for your second script, that is entirely the standard normal way of doing it. I was just pretty tired last night and screwed up my examples as I was thinking in COB (forgot that lua returns the piece rather than assigning them

Code: Select all
local flare1, flare2 = piece("flare1", "flare2")
local currBarrel = 1
function script.QueryWeapon1()
if (currBarrel == 1) then
return flare1
else
return flare2
end
end
function script.FireWeapon1()
-- do recoil movements and fx here first
currBarrel = currBarrel + 1
if currBarrel == 3 then currBarrel = 1 end
end
Code: Select all
local flares = {piece("flare1"), piece("flare2")}
local currBarrel = 1
function script.QueryWeapon1()
return flares[currBarrel]
end
function script.FireWeapon1()
-- fx here again, but you can avoid if blocks by using the table again
currBarrel = currBarrel + 1
if currBarrel == 3 then currBarrel = 1 end
end
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Re: Alternating fire.
The first example worked. Thanks a ton
So what is currBarrel exactly? Is that nothing more than just a tag, or is it something the engine reads?
I ask because now that one "set" of weapons is working, I'm trying to get the other "set" to work. currBarrel = 1 didn't work, and currBarrel2 = 2 didn't work. Clearly I don't understand what is going on here, haha.

So what is currBarrel exactly? Is that nothing more than just a tag, or is it something the engine reads?
I ask because now that one "set" of weapons is working, I'm trying to get the other "set" to work. currBarrel = 1 didn't work, and currBarrel2 = 2 didn't work. Clearly I don't understand what is going on here, haha.
Re: Alternating fire.
currBarrel is just a variable, like any other variable in any other lua script. We use the value of that variable to count our way through the barrels. Say you had a ship with 6 barrels, you'd initialize currBarrel to 1 as before, and add one on every time the weapon fires. So then currBarrel is 2, so we use barrel2 / flare2 the next time it fires. Once we hit 7 (in your script, 3) we know we are on a barrel/flare which does not exist, so we should go back to barrel1 / flare1.SanadaUjiosan wrote:The first example worked. Thanks a ton![]()
So what is currBarrel exactly? Is that nothing more than just a tag, or is it something the engine reads?
I ask because now that one "set" of weapons is working, I'm trying to get the other "set" to work. currBarrel = 1 didn't work, and currBarrel2 = 2 didn't work. Clearly I don't understand what is going on here, haha.
To do it for multiple weapons with their own pairs (or larger sets) of flares, we need a different variable to keep track of each set of weapon's current barrel. So say currBarrel1 and currBarrel2 (they can be named anything you want that makes sense to you), we increment currBarrelX only when weapon X fires.
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Re: Alternating fire.
Aaaalright, I'm beginning to understand this. Cool.
*Evidence I should not and could never be a programmer haha
So I came up with something that works (!) to give me muzzle flares for the alternating shots.
Am I doing this horribly inefficiently, or will this be fine? It works, but I know that doesn't always mean its the best.
Thanks a ton for helping me with this seemingly easy task.
*Evidence I should not and could never be a programmer haha
So I came up with something that works (!) to give me muzzle flares for the alternating shots.
Code: Select all
function script.FireWeapon1()
if currBarrel == 1 then
EmitSfx(flare2, orc_machinegun_flash)
EmitSfx(flare2, orc_machinegun_muzzle)
end
if currBarrel == 2 then
EmitSfx(flare1, orc_machinegun_flash)
EmitSfx(flare1, orc_machinegun_muzzle)
end
currBarrel = currBarrel + 1
if currBarrel == 3 then currBarrel = 1 end
Sleep(30)
end
Thanks a ton for helping me with this seemingly easy task.
Re: Alternating fire.
It'll work fine but the sleep is superfluous, and for crying out loud, rename the orc_machinegun_flash variable 
If you were using the table method however you could just have:

If you were using the table method however you could just have:
Code: Select all
function script.FireWeapon1()
EmitSfx(flares[currBarrel], orc_machinegun_flash)
EmitSfx(flares[currBarrel], orc_machinegun_muzzle)
currBarrel = currBarrel + 1
if currBarrel == 3 then currBarrel = 1 end
end
Last edited by FLOZi on 20 Nov 2010, 16:59, edited 1 time in total.
Re: Alternating fire.
the NKG only uses orcamatic guns.
- SanadaUjiosan
- Conflict Terra Developer
- Posts: 907
- Joined: 21 Jan 2010, 06:21
Re: Alternating fire.
It's true.oksnoop2 wrote:the NKG only uses orcamatic guns.
But yeah, you're right. Can't even remember why the sleeps are in there. Copying and pasting scripts is fun!
I'll clean it up a little, thanks a ton for the help Flozi.
- Forboding Angel
- Evolution RTS Developer
- Posts: 14673
- Joined: 17 Nov 2005, 02:43
Re: Alternating fire.
Yep, that method should work just fine sanada
And yeah, silly sleep needs to fly away and come back some other day 


Re: Alternating fire.
Note that burst weapons will only call FireWeapon once per burst, to cycle through barrels during a burst the Shot function is a good option.