Game Development Community

dev|Pro Game Development Curriculum

Certain Weapon Zoom with Individual Scope HUDs

by Chris Byars · 08/07/2005 (1:05 am) · 46 comments

Today you get to learn how to make only certain weapons be able to zoom, and to cause a unique scope HUD to be visible for each. This resource was made possible by the contributions of Matt Sanders, Harold "LabRat" Brown, and hours of banging heads on desks getting the code to function exactly as we wished.

To begin, create a new script file called zoom.cs and place it in yourgame/server/scripts/. Put the following code in it:

function serverCmdCallZoomFunction( %client, %val )
{
   %zoomer = %client.getControlObject();
   %curWeapon = %zoomer.getMountedImage($WeaponSlot).item.getName();
   commandToClient(%client,'ZoomInDaBiatch',%val,%curWeapon);
}

Open up /server/scripts/game.cs and add this in with the rest of the execute script commands:

exec("./zoom.cs");

We're done with the server side part of things. Now open up /client/scripts/default.bind.cs and under "Zoom and FOV functions", replace everything there with the following.

if($Pref::player::CurrentFOV $= "")
   $Pref::player::CurrentFOV = 45;

function clientCmdZoomInDaBiatch( %val, %curWeapon )
{
   if ( %val && %curWeapon $= "XM8" )
   {
      $ZoomOn = true;
      setFov( $Pref::player::CurrentFOV );
      ScopeGuiGroup1.Visible = 1; // Show XM8 Scope HUD
      Crosshair.Visible = 0; // Hide Crosshair
   }
   else if ( %val && %curWeapon $= "sniperrifle" )
   {
      $ZoomOn = true;
      setFov( $Pref::player::CurrentFOV );
      ScopeGuiGroup2.Visible = 1; // Show Sniper Rifle Scope HUD
      Crosshair.Visible = 0; // Hide Crosshair
   }
   else
   {
      $ZoomOn = false;
      setFov( $Pref::player::DefaultFov );
      ScopeGuiGroup1.Visible = 0; // Make XM8 Scope HUD Invisible
      ScopeGuiGroup2.Visible = 0; //Make Sniper Rifle Scope HUD Invisible
      Crosshair.Visible = 1; // Show crosshair
   }
}

function toggleZoom(%val)
{
   if ($firstPerson == 1) // No Zoom When in 3rd Person View
   {
   commandToServer( 'CallZoomFunction', %val );
   }
}

moveMap.bind(mouse, mouse2, toggleZoom);

Be sure to change "XM8" and "sniperrifle" to the weapon names you want to zoom. What the above script does, is when you press the right-mouse button (mouse2, which you can change to whatever you wish in your options GUI), it calls the zoom function, which then makes sure you are in 1st person, checks what the client's current weapon is, and performs the zoom function and set's the scope HUD group visible that is specified for the current weapon. If you only want to have one weapon be able to zoom, simply remove this part of the script:

else if ( %val && %curWeapon $= "sniperrifle" )
   {
      $ZoomOn = true;
      setFov( $Pref::player::CurrentFOV );
      ScopeGuiGroup2.Visible = 1; // Show Sniper Rifle Scope HUD
      Crosshair.Visible = 0; // Hide Crosshair
   }

Likewise, if you want more than two weapons to zoom, add another of the above code block, and change the %curWeapon $= "" to the weapon of addition. If you want it to use another HUD, change the ScopeGuiGroup1 to your new GUI group's name. Where are all these GUI groups, you say? We'll get to that in a few moments.

You will want to now go into /client/prefs.cs and change the CurrentFOV preference to the zoom distance you want, it is the zoom in power. I happen to like 8x, so mine is set to 11.25.

$Pref::player::CurrentFOV = 11.25;

2x = 45
3x = 30
4x = 22.5
5x = 18
6x = 15
7x = 12.86
8x = 11.25

Now on to the GUI groups, the Scope HUDs. First, download the Sniper Scope image and place it in /client/ui/.

To get this to appear when you zoom in, we will be opening /client/ui/playGui.gui for modification. Add the following scripts somewhere under the new GameTSCtrl(PlayGui) { block of code, and be sure they are indented correctly, to reflect the GUI tree.

new GuiControl(ScopeGuiGroup1) {
      profile = "GuiDefaultProfile";
      horizSizing = "width";
      vertSizing = "height";
      position = "0 0";
      extent = "1024 768";
      minExtent = "8 8";
      visible = "0";

      new GuiBitmapCtrl() {
         profile = "GuiDefaultProfile";
         horizSizing = "relative";
         vertSizing = "relative";
         position = "0 0";
         extent = "1024 768";
         minExtent = "8 8";
         visible = "1";
         bitmap = "./sniperscope";
         wrap = "0";
            helpTag = "0";
      };
   };
   new GuiControl(ScopeGuiGroup2) {
      profile = "GuiDefaultProfile";
      horizSizing = "width";
      vertSizing = "height";
      position = "0 0";
      extent = "1024 768";
      minExtent = "8 8";
      visible = "0";

      new GuiBitmapCtrl() {
         profile = "GuiDefaultProfile";
         horizSizing = "relative";
         vertSizing = "relative";
         position = "0 0";
         extent = "1024 768";
         minExtent = "8 8";
         visible = "1";
         bitmap = "./sniperscope";
         wrap = "0";
            helpTag = "0";
      };
   };

Those two GuiBitmapCtrls control the bitmap that will be displayed for each GUI Group. Both are set to the sniperscope.png image currently, but if you will probably want to change one of them; after all, you're supposed to be able to have each weapon have unique scoping HUDs, with this resource. After doing the above additions/changes, the weapons you set to zoom, should now zoom correctly.

Now you don't want a player to be able to switch to 3rd person while zooming, so open up /client/scripts/default.bind.cs again and look for the toggleFirstPerson(%val) function, and replace it with the following.

function toggleFirstPerson(%val)
{
   if (%val && $ZoomOn != true)
   {
      $firstPerson = !$firstPerson;
   }
}

For some more exploit protection, you don't want a player to be able to switch weapons while they are zooming, otherwise they'll be able to use the zoom and scope for weapons unspecified. So if you are using the Weapon Cycling resource and/or have weapons set to be used when you hit a number on the keyboard, you'll want to do the following.

Open up /server/scripts/inventory.cs and replace the function ShapeBase::use(%this,%data) with the code below:

function ShapeBase::use(%this,%data)
{
   if ( $ZoomOn $= false )
   {
   // Use an object in the inventory.
      if (%this.getInventory(%data) > 0)
         return %data.onUse(%this);
      return false;
   }
}

And now, if you are using the Weapon Cycling resource (which I highly suggest using), you will want to also want to find your serverCmdCycleWeapon function in inventory.cs, and replace it with the following:

function serverCmdCycleWeapon( %client, %data )
{
   if ( $ZoomOn $= false )
      {
         %client.getControlObject().cycleWeapon( %data );
      }
}

There. So now you have a way to make certain weapons able to use a zoom function, and each weapon can have its own HUD plastered on the screen while you zoom. And it cannot be exploited by using items/weapons or cycling weapons while the zoom is on. If you have any problems (hope not), go ahead and post a comment.

Enjoy the power of zoom.
Page«First 1 2 3 Next»
#41
03/21/2009 (5:19 pm)
It doesn't matter... it's just a fullscreen overlay. Make the background of the image transparent and then draw whatever you want.

#42
06/23/2009 (7:43 am)
Chris could you maby send me the sniperscope.png again?

EDIT... sorry here's my E-mail: wouterharingsma@hotmail.com
#43
08/10/2009 (4:11 pm)
i'm sorry but i haven't been able to download the sniper scope image (sniperscope.png), so i was wondering if you could send me the image through e-mail: maris_62@hotmail.com, thanks
#44
11/02/2009 (5:37 pm)
hey i cant get my sniper gui to be center aligned with the crosshairs..also the default crosshairs are still visible in zoom mode? any suggestions??
#45
05/31/2010 (3:55 pm)
I found a solution to the problem of not being able to swap weapons until after you use the zoom at least once. The problem is the ZoomOn variable is NULL when the game launches. Since it is NULL and not false then you can't swap weapons until it is set to NULL. Quickest way to do this is to add
$ZoomOn = false;
to the game.cs script right before
// Give the client control of the player
   %this.player = %player;
   %this.setControlObject(%player);
}
#46
05/05/2012 (9:14 am)
has anyone this working under torque3d v1.2 i did everything and there where no errors but can not getting to work
Page«First 1 2 3 Next»