Game Development Community

Trying to do an ammo counter

by William Goh (Joondalup IT TAFE) · in Torque 3D Professional · 10/17/2009 (12:09 am) · 2 replies

I'm trying to do an ammo counter. Which Gui should I take to display a number containing the amount of ammo I currently have for the Rocket Launcher. Would it be the GuiTextCtrl? And what steps would I require to do in order to have the Gui display my current ammo amount?

I believe the steps to do that would be to create a GuiTextCtrl and then somewhere in the scripts do a CommandToServer creating a function that somehow counts the ammo?

Please help...

#1
10/17/2009 (12:17 am)
If you read through the related *.gui and *.cs scripts you'll find all a working example with the Rocket Launcher.
#2
10/17/2009 (12:37 am)
Like Steve said, reading the files is the best docs! What happend to the Ammo Hud that was there? Here is example stuff to look for!

game/art/gui/playGui.gui
new GuiTextCtrl(AmmoAmount) {
         maxLength = "255";
         Margin = "0 0 0 0";
         Padding = "0 0 0 0";
         AnchorTop = "0";
         AnchorBottom = "0";
         AnchorLeft = "0";
         AnchorRight = "0";
         isContainer = "0";
         Profile = "HudTextItalicProfile";
         HorizSizing = "right";
         VertSizing = "top";
         position = "50 8";
         Extent = "64 16";
         MinExtent = "8 8";
         canSave = "1";
         Visible = "1";
         tooltipprofile = "GuiToolTipProfile";
         hovertime = "1000";
         canSaveDynamicFields = "0";
      };


game/scripts/client/clinet.cs
// Update the Ammo Counter with current ammo, if not any then hide the counter.
function clientCmdSetAmmoAmountHud(%amount)
{
   if (!%amount)
      AmmoAmount.setVisible(false);
   else
   {
      AmmoAmount.setVisible(true);
      AmmoAmount.setText("Ammo: "@%amount);
   }
}

function clientCmdRefreshWeaponHUD(%amount, %preview, %ret)
{
   if (!%amount)
      AmmoAmount.setVisible(false);
   else
   {
      AmmoAmount.setVisible(true);
      AmmoAmount.setText("Ammo: "@ %amount);
   }
}


game/scripts/server/game.cs
// ----------------------------------------------------------------------------
// weapon HUD
// ----------------------------------------------------------------------------
function GameConnection::setAmmoAmountHud(%client, %amount)
{
   commandToClient(%client, 'SetAmmoAmountHud', %amount);
}

function GameConnection::RefreshWeaponHud(%client, %amount, %preview, %ret)
{
   commandToClient(%client, 'RefreshWeaponHud', %amount, %preview, %ret);
}


game/scripts/server/weapon.cs
function Ammo::onInventory(%this, %obj, %amount)
{
   // The ammo inventory state has changed, we need to update any
   // mounted images using this ammo to reflect the new state.
   for (%i = 0; %i < 8; %i++)
   {
      if ((%image = %obj.getMountedImage(%i)) > 0)
         if (isObject(%image.ammo) && %image.ammo.getId() == %this.getId())
         {
            %obj.setImageAmmo(%i, %amount != 0);
            %currentAmmo = %obj.getInventory(%this);
            %obj.client.setAmmoAmountHud(%currentAmmo);
         }
   }
}