Game Development Community

dev|Pro Game Development Curriculum

Adding multiple modes to your weapons

by Julian Ridley · 09/13/2004 (9:17 am) · 8 comments

Ok to start, open up the weapon you want to have multiple modes. In the item data for that weapon add:
modes = 2;
where 2 is the number of modes you want the weapon to have. Then for each mode past the first one make a new weapon image(So if you have 2 modes just make one more weapon image) and the stuff to go with it(you can just use the stuff from the first one if you only want to change the fire rate or something). Now add:
image2 = weaponImage2;
to the weapon item, where weaponImage2 is your other weapon image, do this for each extra image you made, increasing the number after image for each one, I.E. image, image2, image3. NOTE: the first image should not have a number after it.
Now, in each image for that weapon, add:
imageNum = 1;
where 1 is the actual image number, if its the second image of the weapon it should be 2, and so on.
Now open default.binds.cs and bind the switch mode button like so:
moveMap.bindCmd(keyboard, "w", "commandToServer('switchMode');", "");
w is the button you want of course. Do the same in config.cs

Last, open weapon.cs and add this to the bottom:

function ShapeBase::switchMode(%obj)
{
   %data = %obj.getMountedImage($WeaponSlot).item;
   if(%obj.getMountedImage($WeaponSlot).imageNum == %data.modes)
      %mount = %data.image;
   else if(%obj.getMountedImage($WeaponSlot) == %data.image.getId())
      %mount = %data.image2;
   else if(%obj.getMountedImage($WeaponSlot) == %data.image2.getId())
      %mount = %data.image3;
   serverPlay3D(WeaponUseSound,%obj.getTransform());
   %obj.mountImage(%mount, $WeaponSlot);
   if (%obj.client)
   {
      if(%data.modes <= 1)
         messageClient(%obj.client, 'MsgWeaponModeNoChange', '\c0Weapon has only one mode');
      else
         messageClient(%obj.client, 'MsgWeaponModeChange', '\c0Weapon mode changed');
   }
}

function serverCmdSwitchMode(%client)
{
   %client.getControlObject().switchMode();
}
What this is doing is mounting the image of that weapon depending on what mode you have switched to. Note that if you have a weapon with more then 3 modes you will need to add anoher if statement. That should be it, Enjoy

About the author

Recent Blogs

• Weapon Cycling in Torque

#1
09/17/2004 (7:27 pm)
I wrote code that was nearly the same as that for Tribes 2 about 2 years ago.
#2
09/19/2004 (7:52 am)
That will save me some time If I plan to put multiple modes in my weapons. :)
#3
10/13/2004 (9:11 pm)
Thank you very much this will help quite a bit
#4
10/14/2005 (1:57 pm)
I was able to get the weapon image to switch, but as soon as it does my weapon refuses to fire in either mode until I get more ammo.

Any clue on why this is happening? I'm thinking that it might have to do with the fact that images assume a false ammo state when theyre loaded, but thats just a guess. Even if it's right, I have no clue how to go about solving this. Im pretty new (2 weeks using Torque), so I'd appreciate a step by step thing, like the example in the article.

Thanks a lot.
#5
10/25/2005 (5:23 am)
I dont know why but i get this error :

Mapping string: SwitchMode to index: 3
serverCmdSwitchMode: Unknown command.

please help :..-(

... finally found it .. just a typo in the datablock for the
secondary firemode

item = Crossbow1;
but i should be
item = Crossbow;


Great resource :-)
#6
02/13/2007 (3:38 pm)
Hihi all ;)
Just figured I'd let y'all know I've implimented this resource into my TGE 1.5 project. I have made a few modifications to how it works, so that it can be used with reload and weapon switching... the modifications I made are pretty extensive, but I will give you the basics:

First of all, I changed the Item data to use an array,
modes[0] = number of modes;
modes[1] = modeImage;
modes[2] = modeImage2;
etc.

then to allow for weapon switching, in order not to default back to the primary ItemData.image object I added the variable
lastMode = 1;

which is used in the weapon switching code, implimentation left up to you in your own weapon switching code, to store the mode when you switch weapons so that when you switch back, it maintains the previous mode.

then I altered the switching function to work with a for loop based off of the number of modes, instead of a static set of "if" statements

if(%data.modes[0]>1){
for(%i=1; %i<=%data.modes[0] ;%i++){
if(%currImage == %data.modes[%i].getId() && %i == %data.modes[0]){
%mount = %data.modes[1];
%data.lastMode = 1;
}
else if( %currImage == %data.modes[%i].getId() ){
%mount = %data.modes[%i+1];
%data.lastMode = %i+1;
break;
}
}
}

I don't know your exact weapon switching code, so I can't troubleshoot too much, but hopefully I haven't left too many holes in the process and such...

admittedly I don't "monitor" posts, but i'll check back periodically to see if anyone has a question about this slight modification.
#7
08/09/2008 (5:14 am)
Looks good, have to try...
#8
02/04/2009 (4:00 pm)
I hope someone is still around to help. I have gotten this system to work as noted in the script, but i'm using the ammo clips system from Michael Schoonbrood (http://www.garagegames.com/community/resources/view/2225) and as you can tell the clips are declared in the image, and for my rifle i have 2 fire modes, semi-auto and 3 shot burst..

now the problem is obvious that when i switch modes it loads a new image, hence a new clip, i somehow need both images to draw from the same "clip" what would be the best approach to this, i've tried adding the clips to the base image to no effect, and i cant figure out a script code to have image2 pull ammo from image.

also on a side note, does anyone know how to setup that system with a reload key aswell not just auto reload?

thanks in advance for any help.
Kevin