Game Development Community

dev|Pro Game Development Curriculum

Weapon Specific Scopes and Ironsights

by Joseph Martin · 04/24/2010 (5:28 pm) · 6 comments

Note: I used some of Tim Heldna's scripting which can be found here.
Anyway, what this resource allows you to do is specify, in a weapon datablock, whether or not that weapon can use an ironsight, or scope, without modifying any engine code in T3D, however, it might not be the best way of doing so, as I am pretty new to scripting.

To start off with, create a new script file in "game/scripts/client" I called mine zoom.cs. Inside the file I put:
//function to toggle zoom on and off, by sending the appropriate commands to the server
function fntogglezoom(%val)
{
   if(%val)
   {
      if ( $zoomOn == false )
      {
         commandToServer( 'zoomIn' );
      }
      else if ( $zoomOn == true )
      {
         commandToServer( 'returnZoom' );
      }
   }
}

//function to cycle through zoom levels
function toggleZoomLevel(%val)
{
   if(%val)
   {
      if($zoomOn == true)
	  {
	     commandToServer('toggleZoomLevel');
	  }
	  else
	  {
	     return;
	  }
   }
}

function clientCmdIronsightOn(%client,%curWeapon,%offset,%IronsightFOV)
{
   //Set the FOV and Offset to the values sent by the server
   %curWeapon.eyeOffset = %offset;
   echo("-->Adjusting EyeOffset to " @ %offset);
   setFOV(%IronsightFOV);
   $ZoomOn = true;
   //Ironsight has been activated, no need for a reticle
   reticle.setVisible(false);
}

function clientCmdZoom(%client,%curWeapon,%scopeOffset,%zoomLevel)
{ 
   echo("-->Zooming in to an FOV of " @ %zoomlevel);
   $zoomOn = true;
   //set the offset of the weapon and the FOV
   %curWeapon.eyeOffset = %scopeOffset;
   setFOV(%zoomlevel);
   //Show the zoomReticle and hide the normal reticle
   zoomReticle.setVisible(true);
   reticle.setVisible(false);
}

function clientCmdReturnZoom(%client,%curWeapon,%offset,%canScope)
{
   %curWeapon.eyeOffset = %offset;
   echo("-->Returning zoom to an FOV of 90, & adjusting EyeOffset to " @ %offset);
   //Set the FOV back to the default, 90
   setFOV(90);
   $ZoomOn = false;
   //Show the recticle and hide the zoomReticle
   zoomReticle.setVisible(false);
   reticle.setVisible(true);
   if(%canScope)
   {
      //Resets the zoom level. If you want to remember the zoom level, don't forget to change the appropriate line in the function serverCmdZoomIn.
      %curWeapon.curZoomLevel = 0; 
   }
}

function clientCmdToggleZoomLevel(%client,%curWeapon)
{
   //Set the current zoom level to one if the field is equal to 0
   if(%curWeapon.curZoomLevel == 0)
   {
      %curWeapon.curZoomLevel = 1;
   }
   //get the current and maximum zoom levels from the weapon datablock
   %curZoomlevel = %curWeapon.curZoomLevel;
   %maxZoomLevel = %curWeapon.maxZoomLevel;
   //set the zoom back to level 1 if it is at or exceeds the maximum level specified by the weapon
   if(%curZoomLevel >= %maxZoomLevel)
   {
      setFOV(%curWeapon.ZoomLevel[1]);
	  %curWeapon.curZoomLevel = 1;
	  echo("Zooming in/out to an FOV of: " @ %curWeapon.Zoomlevel[1]);
   }
   //otherwise increase the zoom level by 1
   else
   {
      %curWeapon.curZoomLevel = %curZoomLevel + 1;
      setFOV(%curWeapon.zoomLevel[%curWeapon.curZoomLevel]);
	  echo("Zooming in/out to and FOV of: " @ %curWeapon.zoomlevel[%curWeapon.curZoomLevel]);
   }
}

This file contains all the necessary client functions. Don't forget to execute in init.cs, with the rest of the client scripts, so I put this line there:

exec("./zoom.cs");

Next we need to insert the server side functions, which these functions refer to, these go in "scripts/server/commands.cs" At the end of that file I placed:

// ----------------------------------------------------------------------------
// Server-Side Zoom Functions
// ----------------------------------------------------------------------------
function serverCmdZoomIn(%client,%val)
{
   //gather the necessary information, like the current weapon, weather or not it can use
   //ironsights or scopes, the offset to use for ironsights or scopes, and the zoom level.
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %offset = %curWeapon.ironsightEyeOffset;
   %scopeOffset = (%curWeapon.scopeEyeOffset = "") ? %curWeapon.normalEyeOffset : %curWeapon.scopeEyeOffset;
   %zoomLevel = %curWeapon.zoomlevel[%curWeapon.curZoomLevel]; //If you want to make the weapon always start on a certain zoom level,
                                                               //replace "%curWeapon.curZoomLevel" with the desired zoom level. Also 
															   //remember to comment out the line in the function clientCmdReturnZoom
															   //which resets the zoom level.
   %canIronsight = %curWeapon.ironsight;
   %canScope = %curWeapon.scope;
   %IronsightFOV = %curWeapon.IronsightFOV;
   
   if(%canIronsight)
   {
      //tell the client to activate ironsights
      commandToClient(%client,'IronsightOn',%client,%curWeapon,%offset,%IronsightFOV);
	  %weaponName = %curWeapon.item.getName();
      echo("-->Current Weapon is " @ %weaponName);
   }
   else if(%canScope)
   {
      //tell the client to activate the scope
      commandToClient(%client,'zoom',%client,%curWeapon,%scopeOffset,%zoomLevel);
   }
   else
   {
      //Cant use scopes or ironsights
      echo("-->Weapon does not have scope or ironsights");
      return;
   }
}

function serverCmdReturnZoom(%client,%val)
{
   //gathering necessary information to send to client
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %offset = %curWeapon.normalEyeOffset;
   %canIronsight = %curWeapon.ironsight;
   %canScope = %curWeapon.scope;
   //tell the client to return to normal zoom
   commandToClient(%client,'ReturnZoom',%client,%curWeapon,%offset,%canScope);
}

function serverCmdToggleZoomLevel(%client,%val)
{
   //get the current weapon, and if it can use a scope
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %canScope = %curWeapon.scope;
   
   if(%canScope)
   {
      //then tell the client to increase the zoom level
      commandToClient(%client,'ToggleZoomLevel',%client,%curWeapon);
   }
   else
   {
   return;
   }
}

These functions get the necessary information and send it back to the client. Finally, we need to add the needed information to the weapons datablock, so the functions know that the weapon can use ironsights/scopes.

To allow a weapon to use ironsights, add this information to its image datablock:

ironsight = true;
   IronsightFOV = 45;
   Scope = false;
   normalEyeOffset   = "0.25 0.6 -0.27"; //This should always match eyeOffset value listed above
   ironsightEyeOffset = "0 0.7 -0.29"; //This is the eyeOffet value we switch to on zoom

The ironsightFOV setting lets you specify a new FOV when using ironsights, right now, I'm using it to zoom in a little.

and for scopes:

ironsight = false;
   Scope = true;
   normalEyeOffset   = "0.2 1.0 -0.17"; //This should always match eyeOffset value listed above
   scopeEyeOffset = "0 0.0 -0.29"; //This is the eyeOffet value we switch to on zoom
   curZoomlevel = 0;
   maxZoomLevel = 3;
   zoomlevel[1] = 45;
   zoomlevel[2] = 22.5;
   zoomlevel[3] = 11.25;

You specify zoom reticles in the item datablock

You should be able to have as many or as few zoom levels as you want, just remember to set the maxZoomLevel to the highest zoom level you want the players to have access to with that weapon.

The only thing left to do is add the key bindings, I set mine to "q" for fntogglezoom and "=" for ToggleZoom Level.

That's it!

So far I've only been able to test this in single player, so I have no idea if this would work in multiplayer.

About the author

Recent Blogs


#1
04/25/2010 (9:55 am)
hey this is pretty cool and yea it should work in multiplayer since it uses client and server commands. Very nice thanks for this!
#2
05/16/2010 (6:51 pm)
cool!


#3
07/21/2011 (6:48 pm)
I am having trouble with this one. Not only do I need to set the offset, I also need to change rotation and when I first installed this it worked great on offset but after a few reloads it quits altogether. The console says it changes in script and my cross hair will disappear but the weapon never moves.

What version of Torque3D are you using for this and have you had any of these troubles?

Thanks
#4
08/07/2011 (5:49 pm)
Well figured a few things out. If eyeoffset is set to 0 0 0 then the iron sights do not work at all. You got to set it somewhere close to 0 0 0 and it will work but the weapon will not move with the player model when running so forth.
#5
07/08/2012 (7:09 pm)
Any idea why after installing this into T3D 1.2 you can do iron sights or zoom on the weapon then after that you can not switch weapons. Number keys or mouse scroll do not work.

Thanks
#6
06/12/2015 (7:15 pm)
Is anyone able to get this to work in T3D 1.2.. I used it in the FPS Example and nothing changed it still just zooms like before..

I Used this keybind so something should happen

function mouseButtonZoom(%val)
{
toggleZoom(%val);
}

moveMap.bind(keyboard, f, setZoomFOV); // f for field of view
moveMap.bind(keyboard, z, toggleZoom); // z for zoom
moveMap.bind( mouse, button1, fntogglezoom );