Game Development Community

dev|Pro Game Development Curriculum

Ammo Hud Tutorial

by Tim Newell · 12/13/2001 (11:19 pm) · 58 comments

To add an ammo hud display do the following.

In C:\GarageGames\torque\example\fps\server\scripts\weapon.cs:

Add at the end of the file:


//-----------------------------------------------------------------------------
//Ammo Display
//-----------------------------------------------------------------------------

function GameConnection::setAmmoAmountHud(%client, %amount)
{
    commandToClient(%client, 'SetAmmoAmountHud', %amount);
}

then change the body of function WeaponImage::onMount(%this,%obj,%slot) to look like this:

// Images assume a false ammo state on load.  We need to
// set the state according to the current inventory.
if (%obj.getInventory(%this.ammo)) {

  %obj.setImageAmmo(%slot,true);
  %currentAmmo = %obj.getInventory(%this.ammo);

} else {
  %currentAmmo = 0;
}

%obj.client.setAmmoAmountHud(%currentAmmo);

Next add this to C:\GarageGames\torque\example\fps\server\scripts\rifle.cs in the
RifleImage::onFire(%this, %obj, %slot) function after %obj.decInventory(%this.ammo,1);:


%currentAmmo = %obj.getInventory(%this.ammo);
%obj.client.setAmmoAmountHud(%currentAmmo);

Next add this to C:\GarageGames\torque\example\fps\client\scripts\PlayGui.cs:

function clientCmdSetAmmoAmountHud(%value) // time is specified in seconds
{
     AmmoAmount.setText("Ammo: " @ %value);
}

Next add this to C:\GarageGames\torque\example\fps\client\ui\defaultGameProfiles.cs

//-----------------------------------------------------------------------------
// Ammo hud profile

new GuiControlProfile ("AmmoPrintProfile")
{
   opaque = false;
   fillColor = "128 128 128";
   fontColor = "255 255 255";
   border = true;
   borderColor = "0 255 0";
};

***UPDATED***
Forgot about updating the Hud on Ammo pick up...do the following: BUG IS FIXED.

In C:\GarageGames\torque\example\fps\server\scripts\weapon.cs change the function Ammo::onInventory(%this,%obj,%amount) to this:

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);
         }
   }
}

***END UPDATE***

***UPDATED AGAIN***
I just realized 2 things that were missing. reseting the HUD to 0 on Spawning in the map (curently if you leave the game without leaving torque and come back in game it will be set to what you had when you left) and on Dying.

both changes are made in fps/server/scripts/game.cs

First in function GameConnection::onClientEnterGame(%this) Add this to the end:
//reset AmmoAmountHud
%this.setAmmoAmountHud("0");

then in function GameConnection::onDeath( add to the beginning:

//reset AmmoAmountHud
%this.setAmmoAmountHud("0");

***END UPDATE***

Last thing to do is add the GuiTextCtrl to the game.
Go into a map and press F10 for the GUI editor. (this should automatically bring up the playGui.gui)

Add a new GuiTextCtrl and then set its name to AmmoAmount, then set the profile to AmmoPrintProfile

and dont forget to save the PLayGui.gui.


Thanks to EdGardner and Stereo('s code :)) for helping me out.
Page «Previous 1 2 3 Last »
#1
12/09/2001 (7:23 pm)
oooo now its sexy...
Ryan
#2
12/17/2001 (5:54 pm)
I must be the lone idiot with the questions ;)

I have the latest HEAD version and no real bug issues.

I can get the green box on my screen and get it to save in the playgui so when i restart it is still there. There is nothing in it though.

I think i'm doing something wrong with the gui editor.

BTW Is "guitextctrl" and "guimltextcontrol" the same thing?

I'm stumped......

Damn newbies, anyway.

Scott
#3
08/12/2002 (9:17 pm)
One thing I found in this code and don't get me wrong I am not an accomplished scripter but I found no need to decriment the ammo amount. this line of code is not needed in the Rifle.cs file.
%currentAmmo = %obj.getInventory(%this.ammo);
%obj.client.setAmmoAmountHud(%currentAmmo);

The ammo amount will decrease with or with out it. Just my experience though.

Also If you have already done Tim Newells tutorial on weapon reload tutorial you can use this tutorial to add a count for your clips. Just change every instance of "ammo" (note the lowercase letters) to "clip" and "Ammo"(uppercase) "Clip". Then make sure to merge your functions. Somthing like this:
WeaponImage::onMount(%this,%obj,%slot)
function WeaponImage::onMount(%this,%obj,%slot)
{
   // Images assume a false ammo state on load.  We need to
// set the state according to the current inventory.
if (%obj.getInventory(%this.ammo)) {

  %obj.setImageAmmo(%slot,true);
  %currentAmmo = %obj.getInventory(%this.ammo);

} else {
  %currentAmmo = 0;
}

%obj.client.setAmmoAmountHud(%currentAmmo);

      // Images assume a false ammo state on load.  We need to
// set the state according to the current inventory.
// Watch this half of the function
if (%obj.getInventory(%this.clip)) {

  %obj.setImageClip(%slot,true);
  %currentClip = %obj.getInventory(%this.clip);

} else {
  %currentClip = 0;
}

%obj.client.setClipAmountHud(%currentClip);
}

Also a little further down the line you will have to make another merge in your weapons.csfunction Ammo::onInventory(%this,%obj,%amount)
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);
         }
   }
   // The clip inventory state has changed, we need to update any
   // mounted images using this clip to reflect the new state.
   for (%i = 0; %i < 8; %i++) {
      if ((%image = %obj.getMountedImage(%i)) > 0)
         if (isObject(%image.clip) && %image.clip.getId() == %this.getId()) {
            %obj.setImageClip(%i,%amount != 0);
            %currentClip = %obj.getInventory(%this);
            %obj.client.setClipAmountHud(%currentClip);
         }
   }
}

Other than that I don't think it will be much more than duplicating the other stuff and calling the CLIP instead of AMMO

It worked for me and might help some other people. Thanks Tim for your solid stuff!

Matt
#4
11/27/2002 (10:05 pm)
This work's great if you got one weapon but when you got two weapons and you change weapon the ammo counter doesnt change and neither weapon will work :/
gonna try work out how to fix it but im not that good at scripting myself... has anyone else fixed this problem?
#5
07/22/2003 (3:54 pm)
commandToClient(%client, ´SetAmmoAmountHud´, %amount);

this line of code is giving me syntax errors in the console, I have the latest head, a little help would be much appriciated
#6
08/12/2003 (3:29 pm)
Eric,

This might just be an artifact of your message posting, but make sure that the ´ parts are changed to single quotes throughout the source code.

The line show read:

commandToClient(%client, 'SetAmmoAmountHud', %amount);

~ Scott
#7
09/12/2003 (9:10 am)
I had a problem. In my profile drop down box I did not see a AmmoPrintProfile. Is there a way to add that?

charvey1
#8
10/08/2003 (11:56 am)
I went through your tutorial with the new head and when i try to set the profile of the new gui to I cant find the AmmoPrintProfile. I have the script in the defaultgui file but it doesnt show up
#9
10/30/2003 (11:59 am)
ive added all the scripting to the script files and added the new control to the Gui, but it does not display the Ammo Amount. What could be wrong? anyone else have this problem?
#10
11/09/2003 (11:14 pm)
I've got the same problem as Louis.
#11
11/10/2003 (6:32 pm)
well, I finally figured out my problem. I was coding way too tired. the problem that I had was the single quote one previously mentioned in this thread but I missed it from being tired.
#12
11/25/2003 (9:26 pm)
Curtis and Seth,

The reason you don't see the profile listed is because you have it scripted in the wrong file. Go into the customProfiles.cs file instead and remake the AmmoPrintProfile. You will now be able to see it in the profile drop-down menu.
#13
01/14/2004 (6:45 pm)
Will this tutorial work with a multiplayer based game? Or is strictly for single player with one weapon?
#14
01/19/2004 (1:32 am)
should work fine with multiplayer, the switching prob that could be just because both weapons use the same ammo type, i donno his setup, so hey
#15
09/09/2004 (2:05 pm)
in this example\starter.fps\client\ui\defaultGameProfiles.cs
should define the AmmoPrintProfile

// Ammo hud profile

new GuiControlProfile ("AmmoPrintProfile")
{
opaque = false;
fillColor = "128 128 128";
fontColor = "255 255 255";
border = true;
borderColor = "0 255 0";
};


try to find it in the consol log and see if you compile it rigth ?
#16
12/17/2004 (4:05 pm)
I've added all the code above (except the clip stuff) and I don't see anything on the screen much the same as Louis and John had above.
#17
12/18/2004 (5:08 pm)
I've followed the tutorial, and also tried messing around a bit with it myself, but no matter what i keep getting the message Unknown command setAmmoAmountHud whenever i fire any of my weapons (they're all set up with the code to update the ammo on fire). i checked and this function is in fact there, i even tried renaming it without the cmdClient and that didn't do anything. Deleting DSOs did nothing. Everything else works fine (eg. AmmoPrintProfile is there). Advice?
#18
12/24/2004 (4:39 am)
Interesting...I had the same problem as Seth (where the AmmoPrintProfile won't show up)

So I checked my console.log, and here it reads...

Compiling starter.fps/client/ui/PlayGui.gui...
Loading compiled script starter.fps/client/ui/PlayGui.gui.
Warning: (gui/guiTypes.cc @ 293) GuiControlProfile: requested gui profile (AmmoPrintProfile) does not exist.

eh?

[edit]

AH, found out why it wasn't showing up - the ';' was needed x.x

anyways, so afer i did that, i have AmmoPrintProfile in my customProfiles.cs, and I was able to set the GuiTextCtrl to AmmoPrintProfile. I saved my playGui.gui, reloaded torque, but the HUD still did not display. So i ventured again into console.log, and here's what it says:

starter.fps/server/scripts/weapon.cs (98): Unable to find object: '' attempting to call function 'setAmmoAmountHud'
starter.fps/server/scripts/weapon.cs (123): Unable to find object: '' attempting to call function 'setAmmoAmountHud'

what now? x.x

thanks!

ps: i did the same thing as Leo Altmann, renaming it without cmdClient, but to no avail.
#19
12/27/2004 (9:02 pm)
ah, i fixed it! (or at least the problem i was having). there were a few {}s missing in the WeaponImage::OnMount function in weapon.cs (those darn if statements w/o brackets...). anyways mine now looks like this:
function WeaponImage::onMount(%this,%obj,%slot)
{
   // Images assume a false ammo state on load.  We need to
   // set the state according to the current inventory.
   if (%obj.getInventory(%this.ammo))[b]{[/b]
      %obj.setImageAmmo(%slot,true);
%currentAmmo = %obj.getInventory(%this.ammo);

} else {
  %currentAmmo = 0;
[b]}[/b]
}

%obj.client.setAmmoAmountHud(%currentAmmo);

now i'm off to mod this to have a different counter for each weapon :D
Page «Previous 1 2 3 Last »