Alternate Weapon Fire
by Brett Bowen · in Torque Game Engine · 08/12/2002 (7:01 pm) · 20 replies
This may seem silly, but has anyone done an alternate Fire for a weapon? Someone give me an idea of where to start. Would I need to modify the c++ code or could I do it all in script?
About the author
#2
04/15/2004 (7:07 am)
I did it by adding a second trigger state to the C++ code and then using that state in script.
#3
04/15/2004 (7:40 am)
I did it once in script only, gotta see if I can find it...
#4
in player.cs, replace Armor::onTrigger() with
So you need to at least define another ShapeBaseImageData block (plus the associated definitions for Ammo etc.) in your weapon script...
Then, add this to default.bind.cs:
04/15/2004 (8:07 am)
Ah, found it... pretty easy actually :)in player.cs, replace Armor::onTrigger() with
function Armor::onTrigger(%this, %obj, %triggerNum, %val)
{
// This method is invoked when the player receives a trigger
// move event. The player automatically triggers slot 0 and
// slot one off of triggers # 0 & 1. Trigger # 2 is also used
// as the jump key.
// second fire:
if(%triggerNum == 3)
{
%weapon = %obj.getMountedImage($WeaponSlot);
if(!isObject(%weapon) || (%weapon.getClassName() !$= "ShapeBaseImageData"))
{
return;
}
%name = (%obj.getMountedImage($WeaponSlot).getName());
// get the second image:
%second = %name.second;
switch (%val)
{
case 0:
//echo( "AltFire-false" );
case 1: // second fire!
//echo( "AltFire-true" );
if(isObject(%second))
{
%slot = 0;
%muzzleVector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%muzzleVelocity = VectorAdd(
VectorScale(%muzzleVector, %second.projectile.muzzleVelocity),
VectorScale(%objectVelocity, %second.projectile.velInheritFactor));
%p = new (%second.projectileType)() {
dataBlock = %second.projectile;
initialVelocity = %muzzleVelocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
MissionCleanup.add(%p);
%fireHandle = alxCreateSource("AudioDefault3D", expandFilename(%second.stateSound[3].fileName));
alxPlay(%fireHandle);
}
}
}
}Then, if say you have a weapon called pistol with a shapeBaseImageData named PistolImage, add to yourdatablock ShapeBaseImageData(PistolImage)
{
...
[b]second = LaserBeamImage;[/b]"LaserBeamImage" would be the name of another weapon's ShapeBaseImageData... in my example, I had a second gun, which was a Laser type weapon with its own Ammo etc.So you need to at least define another ShapeBaseImageData block (plus the associated definitions for Ammo etc.) in your weapon script...
Then, add this to default.bind.cs:
function altTrigger(%val)
{
// trigger second fire, if any:
$mvTriggerCount3++;
}as well as this:moveMap.bind(mouse0, "button1", altTrigger);add that bind to client/config.cs, too (if available) and you should be all set... :)
#5
04/15/2004 (10:22 am)
Nice, Beffy. :)
#6
/me slowly begins to see the light with scripting... if only my days
could've been longer :(
04/15/2004 (11:32 pm)
You just made my day... /me slowly begins to see the light with scripting... if only my days
could've been longer :(
#7
06/22/2004 (10:15 pm)
I was just wondering. Do you actually replace that function? Or add to it? Thanks.
#9
I'm thinking you would have to get the actual shape instance, then use setsequence(). Not sure.
07/01/2004 (12:24 pm)
Beffy: Have you figured out a way to play a fire animation from script for the mounted weapon?I'm thinking you would have to get the actual shape instance, then use setsequence(). Not sure.
#10
EDIT: Btw, the comment in Player::onTrigger says the same I just did.
EDIT2: My comment is to Beffy's code where he used 2 separate weapons, the original poster asked for a system to have 2 triggers on the same weapon, and for that you need to add a new trigger to shapeBaseImage.cc copying the current onTriggerDown stuff in there (at least if you want to do it nicely). I believe someone posted a resource about that.
NOTE: Just replied here for further reference for people searching for it. I'm not even sure how it was I came across this post.
07/10/2004 (12:20 pm)
I found this by accident, but I think it would be better if instead of creating the projectile directly there in a hackish attempt to fire you set the fire state in the second weapon slot. Torque supports up to 6 trigger keys and (i think) 8 weapon slots. If you do setImageFireState(0, true); you are setting slot's 0 state to fire, which is what a normal weapon does (player.cc does this in the updateMove). Player.cc also sets automatically the weapon slot 1. So if your weapon is in slot 1 and you use the altTrigger already configured in Torque (mvActionCount1) it will fire without having to do any changes at all.EDIT: Btw, the comment in Player::onTrigger says the same I just did.
EDIT2: My comment is to Beffy's code where he used 2 separate weapons, the original poster asked for a system to have 2 triggers on the same weapon, and for that you need to add a new trigger to shapeBaseImage.cc copying the current onTriggerDown stuff in there (at least if you want to do it nicely). I believe someone posted a resource about that.
NOTE: Just replied here for further reference for people searching for it. I'm not even sure how it was I came across this post.
#11
07/25/2004 (10:17 am)
How would you go about making this zoom in for sniper rifles, and do somethign else for a different type of weapon?
#12
08/26/2004 (9:28 am)
Thanks steffan that helps a ton. There are sniper rifle zooms on the code snipits. you know how "E" zooms in a little (at least in my version of Torque) maybe you could make it zoom a lot, I'll try that!!
#13
08/26/2004 (9:36 am)
OK... Once zoomed in, how would you add the sniper scope and cross hair .bmp to the screen to cover up everything else except the health bars, chat, time, and score?
#14
11/20/2005 (5:57 am)
Even another way of launching the projectile would be to expose setImageState or a part of it to script and then instead of manualyl creating the projectile you would set the weapons imagestate to a preset state that has the alternate fire. In this way you wouldn't need to create a new bit of imagedata, just a new projectile, and it would take full advantage of weapon states for animations, transitions, etc for you. I implemented a similar solution in a project recently and it worked well, the only bit of source modification it required was the consolemethods to allow you to call setImageState from script.
#15
i was wondering if you can have like a scope for the alt fire, like on with veriable zoom,
(think the lightning gun from unreal tournament 2004)
how would i do that?
07/01/2006 (11:05 am)
Hi, i was wondering if you can have like a scope for the alt fire, like on with veriable zoom,
(think the lightning gun from unreal tournament 2004)
how would i do that?
#16
Is there any easy way to solve this without engine mods?
09/21/2006 (6:47 am)
The problem with Beffy's code is the weapon doesnt continue to fire if you hold down the alt.Is there any easy way to solve this without engine mods?
#17
09/21/2006 (7:07 am)
Yes. Check default.binds.cs. %var sets the state.
#18
12/18/2008 (9:14 am)
I have a wwII weapon with a bayonet on the end of the weapon and instead of an alt fire I want to use the bayonet and a stabbing motion with the weapon. I was wondering if this will work for what I need?
#19
12/18/2008 (9:24 am)
Kevin, you might need to implement a melee resource for that, or you can do it by casting a ray and seeing what it hits. Then you can adjust your code for whatever it hit. If it hit the ground: kick up dust, if it hit an enemy - give damage to the enemy, etc...
#20
OP: Alternate trigger state.
Code solutions are always much more direct and elegant than script ones, IMO. And since you're in this forum, you have the code. You paid for it, you may as well use it! ;)
That said... nibbuls: Scopes and binoculars.
12/19/2008 (12:49 am)
J.C.: Manual image state changes.OP: Alternate trigger state.
Code solutions are always much more direct and elegant than script ones, IMO. And since you're in this forum, you have the code. You paid for it, you may as well use it! ;)
That said... nibbuls: Scopes and binoculars.
Torque Owner James Laker (BurNinG)
Can this be done with Scripting?