Game Development Community

Digital Speedometer (code inside)

by Tim Heldna · in Torque Game Engine · 02/11/2007 (10:54 pm) · 0 replies

Here's a free digital speedometer for anyone who wants it. It will display speed in km/h if you are mounted in a vehicle. It works across a network.

- First download the bitmaps required by the display:
speedHud.zip (31 KB)

- Once downloaded, extract to "starter.fps/client/ui" (keep bitmaps inside the speedHUD folder, the full path to the bitmaps should be "starter.fps/client/ui/speedHUD/")

- Add this to the Armor::onAdd method inside of player.cs
// Digital Speedometer GUI
   %obj.getSpeed(%obj);
   // Digital Speedometer GUI

- Add this to the Armor::onDisabled method inside of player.cs
// Digital Speedometer GUI
   cancel(%obj.speedHUDSchedule);
   // Digital Speedometer GUI

- Add this to server/scripts/commands.cs
//-----------------------------------------------------------------------------
// Digital Speedometer (Server)
//-----------------------------------------------------------------------------
function Player::getSpeed(%player)
{
   %player.speedHUDSchedule = %player.schedule(100, getSpeed, %player);

   if(%player.getControlObject())
   {
      commandToClient(%player.client, 'displaySpeedHUD', true);
      commandToClient(%player.client, 'getSpeed');
   }

   else
      commandToClient(%player.client, 'displaySpeedHUD', false);
}
//-----------------------------------------------------------------------------

- Add this to client/scripts/client.cs
//-----------------------------------------------------------------------------
// Digital Speedometer (Client)
//-----------------------------------------------------------------------------
function clientCmdGetSpeed()
{
   %texPath = "starter.fps/client/ui/speedHUD/";

   %vel = getControlObjectSpeed();
   // convert from m/s to km/h
   %cVel = mFloor(%vel * 3.6); // m/s * (3600/1000) = km/h

   if(%cVel <= 9)
   {
      digit_1.setBitmap(%texPath @ "0");
      digit_2.setBitmap(%texPath @ "0");
      digit_3.setBitmap(%texPath @ %cVel);
   }

   if(%cVel >= 10)
   {
      %speed1 = getSubStr(%cVel, 0, 1);
      %speed2 = getSubStr(%cVel, 1, 1);

      digit_1.setBitmap(%texPath @ "0");
      digit_2.setBitmap(%texPath @ %speed1);
      digit_3.setBitmap(%texPath @ %speed2);
   }

   if(%cVel >= 100)
   {
      %speed3 = getSubStr(%cVel, 2, 1);

      digit_1.setBitmap(%texPath @ %speed1);
      digit_2.setBitmap(%texPath @ %speed2);
      digit_3.setBitmap(%texPath @ %speed3);
   }
}

function clientCmdDisplaySpeedHUD(%bool)
{
   digitalSpeedHUD.setVisible(%bool);
}
//-----------------------------------------------------------------------------

- Add this to the bottom of your playGui.gui file, before the last closing brace
new GuiControl(digitalSpeedHUD) {
      Profile = "GuiDefaultProfile";
      HorizSizing = "right";
      VertSizing = "top";
      position = "60 676";
      Extent = "56 36";
      MinExtent = "8 2";
      Visible = "1";

      new GuiClockHud(background) {
         Profile = "GuiDefaultProfile";
         HorizSizing = "right";
         VertSizing = "bottom";
         position = "0 0";
         Extent = "56 36";
         MinExtent = "8 2";
         Visible = "1";
         showFill = "1";
         showFrame = "1";
         fillColor = "0 0 0 1";
         frameColor = "1 1 1 1";
         textColor = "0 0 0 0";
      };
      new GuiBitmapCtrl(digit_1) {
         Profile = "GuiDefaultProfile";
         HorizSizing = "right";
         VertSizing = "bottom";
         position = "2 2";
         Extent = "16 32";
         MinExtent = "8 2";
         Visible = "1";
         bitmap = "./speedHUD/0";
         wrap = "0";
      };
      new GuiBitmapCtrl(digit_2) {
         Profile = "GuiDefaultProfile";
         HorizSizing = "right";
         VertSizing = "bottom";
         position = "20 2";
         Extent = "16 32";
         MinExtent = "8 2";
         Visible = "1";
         bitmap = "./speedHUD/0";
         wrap = "0";
      };
      new GuiBitmapCtrl(digit_3) {
         Profile = "GuiDefaultProfile";
         HorizSizing = "right";
         VertSizing = "bottom";
         position = "38 2";
         Extent = "16 32";
         MinExtent = "8 2";
         Visible = "1";
         bitmap = "./speedHUD/0";
         wrap = "0";
      };
   };

That's it. Run your game, mount your vehicle and test it out.

Here's a video of it in action (heavily compressed, sorry about the poor quality)

speedHudVideo.zip (2.8 MB)

i5.photobucket.com/albums/y189/fjs/speedHudGui.jpg