Game Development Community

dev|Pro Game Development Curriculum

Weapon Cycling in Torque

by Julian Ridley · 06/23/2004 (7:45 am) · 41 comments

Ok, for some reason, this isnt in torque to begin with, and there are no resources on it. After some struggling I figured it out, using some tweaked code from the forums. To start:
open Default.Binds.cs in client/scripts. Add this after the bind for fire:
function prevWeapon( %val )
{
   if ( %val )
      commandToServer( 'cycleWeapon', "prev" );
}
function nextWeapon( %val )
{
   if ( %val )
      commandToServer( 'cycleWeapon', "next" );
}
moveMap.bind(keyboard, "ctrl q", prevWeapon);
moveMap.bind(keyboard, q, nextWeapon);
The above simply sets up the default binds and tells them what to do. q and ctrl q can be whatever you want.
next:
open optionsDLG.cs in client/scripts. Put this where all the others like it are:
$RemapName[$RemapCount] = "Previous Weapon";
$RemapCmd[$RemapCount] = "prevWeapon";
$RemapCount++;
$RemapName[$RemapCount] = "Next Weapon";
$RemapCmd[$RemapCount] = "nextWeapon";
$RemapCount++;
That allows the user to bind the weapon cycling to whatever they want.
The rest of this is done in inventory.cs in server/scripts. Open it up and at the top put:
$weaponInSlot[0] = "Crossbow";
$weaponInSlot[1] = "UberCrossbow";
$weaponInSlot[2] = "RocketLauncher";
$weaponInSlot[3] = "FragMine";

$maxWeaponSlot = 3;
However, fill those in with your weapon names instead, and maxWeaponSlot should equal the highest number in your list, NOT the actual number of weapons. Next add this at the bottom of the file:
function ShapeBase::hasInventory(%this, %data)
{
   return (%this.inv[%data] > 0);
}
function serverCmdCycleWeapon( %client, %data )
{
   %client.getControlObject().cycleWeapon( %data );
}
function ShapeBase::cycleWeapon( %this, %data )
{
   %slot = -1;
   if ( %this.getMountedImage($WeaponSlot) != 0 )   {
         %curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
         if(%curWeapon $= "Crossbow")
            %slot = 0;
         else if(%curWeapon $= "UberCrossbow")
            %slot = 1;
         else if(%curWeapon $= "RocketLauncher")
            %slot = 2;
         else if(%curWeapon $= "FragMine")
            %slot = 3;
   }
   if ( %data $= "prev" )   {
         // Previous weapon...
       if ( %slot == 0 || %slot == -1 )      {
           %i = $maxWeaponSlot;
           %slot = 0;
       }
       else
           %i = %slot - 1;
   }
   else   {
   // Next weapon...
      if ( %slot == $maxWeaponSlot || %slot == -1 )      {
         %i = 0;
         %slot = $maxWeaponSlot;
      }
      else
         %i = %slot + 1;
   }
   %newSlot = -1;
   while ( %i != %slot )   {
      if ( $weaponInSlot[%i] !$= ""
              && %this.hasInventory( $weaponInSlot[%i] ))      {
              // player has this weapon and it has ammo or doesn't need ammo
         %newSlot = %i;
         break;
      }
      if ( %data $= "prev" )      {
         if ( %i == 0 )
            %i = $maxWeaponSlot;
         else
            %i--;
      }
      else      {
         if ( %i == $maxWeaponSlot )
            %i = 0;
         else
            %i++;
      }
   }
   if ( %newSlot != -1 )
      %this.use( $weaponInSlot[%newSlot] );
}
Replace the series of if statements(with the weapons), with your weapons and the slots they occupy. The rest is pretty straight forward.
Page «Previous 1 2 3 Last »
#1
06/22/2004 (10:09 pm)
Let me know if I missed something.

Oh, and do remember that ´ = '
#2
06/23/2004 (11:14 am)
This resource works great, thanks!
#3
06/23/2004 (9:00 pm)
Hmmmmm, is there a way to make it so that when you copy and paste from those codes things it doesnt paste as one long line? Or is it just me?
#4
06/24/2004 (5:25 am)
julian you need something to read and write several formats. paste it to wordpad first, then copy it from there to your .cs

btw great resource.
#5
08/28/2004 (7:30 pm)
How do you make it so you can use the scroll button on the mouse?

Cool resource btw!
#6
09/02/2004 (12:06 pm)
Joseph, http://www.garagegames.com/mg/forums/result.thread.php?qt=19356
#7
01/01/2005 (10:29 pm)
this was what I was looking for. I only had 2 problems with it, though easly corrected.
/code

moveMap.bind(keyboard, q, nextWeapon);
// should be
moveMap.bind(keyboard, "q", nextWeapon);

/code

And second the first code snipplet would work in my Default.Binds.cs .
So I put the in ~/client/config.cs, then it worked great.

Thank you

Edit: ok how do you get the neat code box?
#8
02/21/2005 (9:03 pm)
How would you assign the mouse wheel to cycle weapons?
#9
03/02/2005 (6:36 pm)
EDIT: nevermind, got it to work, THANKS!
#10
03/22/2005 (5:42 pm)
For those of you who want to use the mouse wheel to scroll weapons here is the code you'll need...

In Client/config.cs at the bottom add the following line:

moveMap.bind(mouse0, "zaxis", handleMouseWheel);

Then in Client/Scripts/default.bind.cs find this:

function prevWeapon( %val ){   if ( %val )
      commandToServer( 'cycleWeapon', "prev" );
      }
      function nextWeapon( %val )
      {
      if ( %val )
      commandToServer( 'cycleWeapon', "next" );
      }

Then directly after it add the following:

moveMap.bind(mouse0, "zaxis", handleMouseWheel);

function handleMouseWheel(%val)
{
      if ( %val < 0 )
      commandToServer('cycleWeapon', "next");
      else if ( %val > 0 )
      commandToServer('cycleWeapon', "prev");
}

Don't forget to remove the following lines in default.bind.cs:

moveMap.bind(keyboard, "ctrl q", prevWeapon);
moveMap.bind(keyboard, q, nextWeapon);

That's it, you should now be able to scroll through weapons with the mouse wheel!
#11
04/25/2005 (2:19 am)
thanks alot man i just added this to my game :) only thing i need after this is mouse scroll support then this option will realy be worth having around
#12
04/25/2005 (2:25 am)
ah thanks Tim Heldna i didnt read your statment at first :)
#13
04/25/2005 (4:46 am)
@ Ignius Greatday

Yeah i read that first post of yours and you had me scraching my head. Glad you took another look and read my post.

What's the point of having a weapons scroll feature if you can't use the mouse wheel? I found some forums threads regarding use of the mouse wheel, not relating to this resource in specific, and managed to work it out from there. I figured i'd lay it out in a step by step manner so people who use this resource can easily chuck in mouse wheel support without the headaches.

Anyhow, glad i managed to help someone with this.
#14
05/06/2005 (10:36 pm)
the one thing that i have been trying to figure out is how to make is so people can have the option to set cycle weapons to their scroll incase they want to either use it or not
#15
06/03/2005 (11:02 pm)
Thank you! This helped a ton. I got it to display weapon names when you switch, just change the IFs to coincide with your weapon selection. The only problem is, whenever you swap weapons while holding down fire the messages get behind. For example, I have three weapons: pistol, mp5, and m4ss. When I'm shooting the m4, and I simultaneously hit the next weapon button, it goes to mp5, but the message hud brings up a new message that says: "M4ss Selected".

The code to get it to display weapon names is in weapon.cs, just replace the whole Weapon::onUse function.

function Weapon::onUse(%data,%obj)
{
   // Default behavoir for all weapons is to mount it into the
   // this object's weapon slot, which is currently assumed
   // to be slot 0
   if (%obj.getMountedImage($WeaponSlot) != %data.image.getId()) {
      %obj.mountImage(%data.image, $WeaponSlot);
	%curwpn = %obj.getMountedImage($WeaponSlot).item.getName();
      if (%obj.client)
      {
	if(%curwpn $= "Pistol")
		messageClient(%obj.client, 'MsgWeaponUsed', '\c0Pistol selected');
	if(%curwpn $= "M4ss")
		messageClient(%obj.client, 'MsgWeaponUsed', '\c0M4ss selected');
	if(%curwpn $= "Mp5")
		messageClient(%obj.client, 'MsgWeaponUsed', '\c0Mp5 selected');
}
   }
}
#16
06/25/2006 (4:11 pm)
This resource was perfect, went in smooth and easy and worked on first try, thank you so much!
#17
08/08/2006 (6:59 pm)
for some reason i can't even get my new gun to work at all....getting very very annoying...can't think of what i'm missing :*(............i'm so stupid...i forgot to add it to be inventoriable by the player lol. Long days and nights are driving me crazy...btw i almost had this on my own. All i forgot about was adding a few lines. TY guys
#18
08/12/2006 (6:14 am)
Great resource!
Thank you.
#19
08/19/2006 (12:41 pm)
great resource, however something wierd occured. i have 8 weapons, maxslot = 7, thats right, right?

ok, next weapon cycles nicely, exept that it skips the weapon in weapon slot[5]

then in previous, it cycles backward exept that it gets stuck on weapon slot [6], then it always goes to 6, the next previous again brings to 6 .

any ideas y?
#20
08/29/2006 (10:10 am)
Excellent resource! Thanks, Julian.
Page «Previous 1 2 3 Last »