Game Development Community

Ammo Amount HUD Update on Switching Weapons

by Chris Byars · in Torque Game Engine · 04/10/2005 (5:09 pm) · 18 replies

I've used this resource:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1786

However when you have more than one weapon, and when you switch weapons, the ammo amount doesn't update with the changed weapon's ammo amount, until one fires a bullet.

How would I set it up so that when the weapon is changed, the ammo hud updates as well? I'm sure others have had this issue also. Hm.

#1
04/10/2005 (5:31 pm)
This was actually discussed a few months ago, but no idea in what thread. The short version is that when the client changes weapons, you need to request an ammo update from the server, and have the server respond with the new ammo count for the second weapon so your gui will update. Otherwise, as you noticed, it won't update until the server recognizes a need for an update (when a round is fired).
#2
04/10/2005 (6:08 pm)
Also when a player picks up ammo, even if it isn't for the current weapon, the ammo hud updates with the amount of ammo picked up, until the weapon is fired. If only you had the link to that thread. =/ It seems to be more complicated than it sounded.
#3
04/12/2005 (11:13 pm)
I had the same problem c2 and i fixed it very easily. Just 2 lines of code were needed if i remember correctly. I'm gonna hunt through my scripts now and i'll post the results here as soon as i find it...
#4
04/12/2005 (11:34 pm)
Ok i found the problem. It's just a case of missing a closing bracket "}" in weapon.cs under the weapon image class where it says 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)){
%obj.setImageAmmo(%slot,true);
%currentAmmo = %obj.getInventory(%this.ammo);

} else {
%currentAmmo = 0;
}
[b]}[/b]
%obj.client.setAmmoAmountHud(%currentAmmo);

The bold bracket at the end just above
%obj.client.setAmmoAmountHud(%currentAmmo);
was missing in his resource. As soon as you add it in the hud will reset properly.

This was actually mentioned in a post at the end of his resource. Anyhow, just chuck that closing bracket in and you'll be fine.
#5
04/13/2005 (6:39 am)
For the ammo pickup problem

make your function Ammo:: onInventory look like below

function Ammo::onInventory(%this,%obj,%amount,%shape)
{
   // 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);

        %weapon = %obj.getMountedImage($WeaponSlot).item.getName();
          
	    if(%weapon.image.ammo $= %this.getName())
     {
         %currentAmmo = %obj.getInventory(%this);
		 %obj.client.setAmmoAmountHud(%currentAmmo);
     }

   }
}
#6
04/13/2005 (4:02 pm)
@Tim: Yeah, my code looks exactly like that I think:
// Weapon Image Class
//-----------------------------------------------------------------------------

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

}


//-------------

Hmm.
#7
04/13/2005 (4:20 pm)
If I make the changes as the above posters mentioned, no ammo amount HUD appears, and no weapon will fire, even if I pick up ammo.
#8
04/17/2005 (3:49 pm)
Well I was able to get the weapon switching to update the correct ammo by taking a look at someone else's weapon code, however the ammo pickup problem still exists.

Scenario:
I am holding Weapon 1 which has 60 ammo, and run over a Weapon 2 ammo clip of 40 ammo. The ammo HUD then shows that even though I am holding Weapon 1, it updates as if I have 100 ammo for it, until you fire.

What's the best way to tell the engine not to update the ammo display unless the ammo run over is the ammo for the current weapon?
#9
04/18/2005 (1:28 am)
Have you set all your max inventory levels in your player.cs & game.cs file? Here's an example of my scripts so you can see what i mean:

player.cs (starter.fps/server/scripts)

// Allowable Inventory Items
   maxInv[HealthKit] = 1;
   
   maxInv[Browning] = 1;
   maxInv[BrowningAmmo] = 10;
   maxInv[BrowningAmmoClip] = 3;
   
   maxInv[M16] = 1;
   maxInv[M16Ammo] = 30;
   maxInv[M16AmmoClip] = 10;
   
   maxInv[M16GrenadeLauncher] = 1;
   maxInv[M16GrenadeLauncherAmmo] = 1;
   maxInv[M16GrenadeLauncherAmmoClip] = 6;
   
   maxInv[M60] = 1;
   maxInv[M60Ammo] = 100;
   maxInv[M60AmmoClip] = 5;
   
   maxinv[RedKey] = 1;
   maxinv[BlueKey] = 1;
   maxinv[YellowKey] = 1;

};

game.cs (starter.fps/server/scripts)

// Starting equipment

   %player.mountImage(BrowningImage,0);
   %player.setInventory(Browning,1);
   %player.setInventory(BrowningAmmo,10);
   %player.setInventory(BrowningAmmoClip,3);
   %player.setInventory(M16,1);
   %player.setInventory(M16Ammo,30);
   %player.setInventory(M16AmmoClip,10);
   %player.setInventory(M16grenadelauncher,1);
   %player.setInventory(M16grenadelauncherAmmo,1);
   %player.setInventory(M16grenadelauncherAmmoClip,6);
   %player.setInventory(M60,1);
   %player.setInventory(M60Ammo,100);
   %player.setInventory(M60AmmoClip,5);

Let me know if this helps.
#10
04/18/2005 (3:41 pm)
Yes, of course, those variables are set. Problem is when ammo is picked up, it updates the "setAmmoAmountHUD" even if the ammo isn't for the currently mounted weapon. Once you fire your current weapon, it decrements the ammo according to the current weapon's actual ammo.
#11
04/18/2005 (11:09 pm)
Oh ok, sorry. I misunderstood your problem before. Well i have no idea what's going on then. All i can suggest is checking the console for errors or warnings but i guess you've already been doing that. It sounds like something is going wrong in you're weapon.cs script. It's hard to track down the problem without seeing your script files.
If you want send me an email tjh@rascal.com.au of your weapon.cs script and your crossbow.cs script (*replace 'crossbow with whatever weapon script your using*) and i'll be happy to take a look.
#12
04/19/2005 (3:57 am)
Thanks.
#13
04/19/2005 (7:06 am)
Ok c2 i fixed your problem. I feel like kicking myself up the butt cos i should of picked up your problem straight away when you posted your weapon image class section of code (inside weapon.cs). To jog your memory you posted this section of code on the 14th april.
// Weapon Image Class
//-----------------------------------------------------------------------------

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

}


//-------------

Your problem was being caused by the 2nd closing bracket (eg "}") under the line %currentAmmo = 0;. Remove this bracket so the whole section of code 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)){
%obj.setImageAmmo(%slot,true);
%currentAmmo = %obj.getInventory(%this.ammo);
} else {
%currentAmmo = 0;
}

%obj.client.setAmmoAmountHud(%currentAmmo);

}

To explain; By having that extra closing bracket ("}") under the line %currentAmmo = 0; the last line of the function (%obj.client.setAmmoAmountHud(%currentAmmo); was not included in the function and was doing nothing at all. By taking out the unneccesary closing bracket the setAmmoAmountHud line of code is included within the whole function.

I use a script editor called context, which is a free download, which highlights all your opening and closing brackets and makes it easier to pick up on errors like this.

I tested this and it worked fine once that extra closing bracket was removed.
Hopefully we have finally resolved this issue, if not let me know and i'll help you in any way i can

-Tim
#14
04/19/2005 (3:13 pm)
If I do that, no ammo HUD number shows up, and I get the error in the console:

fps/server/scripts/game.cs (228): Unknown command setAmmoAmountHud.

And it says this each time a bullet is fired:

Unknown command setAmmoAmountHud.

=/
#15
04/20/2005 (4:57 am)
Ok c2, i know you have probably lost all faith in me by now but i reckon i got it this time.

At the very bottom of your weapon.cs script you have this:

//-----------------------------------------------------------------------------
//Ammo Display
//-----------------------------------------------------------------------------
function setAmmoAmountHud(%client, %amount)
{    
    commandToClient(%client, 'SetAmmoAmountHud', %amount);
}

Change it to this:

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

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

Now (fingers crossed) your ammo hud will display and update accordingly. Notice that in your script you are missing the word GameConnection in the first line of code posted above. That's what's been causing the problem.

Assuming you followed my past script changes we should be in business now. If not you may take consolation in the fact that i'm not giving up on this till it's fixed.

-Tim

P.S. Just a small note regarding console errors.

I have this resource working in my game perfectly but i still get console warnings (the one's that appear in light grey, not red) but everything still works as it should. I may be wrong here but i don't believe these warnings are happening cos of errors in scripting, i think it's just Torque being anul and reporting (false) warnings unnecessarily.
So what's my point here? Well if you get this working, and i'm quite confident with the above script changes it will work, then just ignore any warnings you get in the console.
#16
04/20/2005 (2:56 pm)
It's fixed! :O
#17
04/20/2005 (7:52 pm)
Happy to hear it fixed. :) :) :)

Glad i was able to help, it was my pleasure!

You may find this resource of interest www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2221

It works in conjuncion with the ammo display, what it does is display a picture of the weapon you have selected.

Happy Gaming
#18
04/21/2005 (3:48 am)
That one is a bugged resource too, doesn't work correctly. I figured the WeaponNameHUD is enough, lol. =)