Game Development Community

Weapon Reloading issues

by Ronald J Nelson · in Torque Game Engine · 06/19/2006 (3:15 pm) · 1 replies

Ok I have no idea what I did wrong here, I trace it all through by the numbers and using echo statements to provide my variable vaules and they match according to what I had worked out on paper. Obviously I have made a mistake in my math in both places and am unable to get my head around it. The problem is that my code decrements ammo and clips perfectly. It even stores a partially used clip's contents for later use. However, regardless on the way that the clips are emptied, the last clip is always empty even though it states there is one left as it should. Here is my code.
function reloadBullets(%image, %obj, %time) 
{
   // The ammo inventory state has changed, we need to update any
   // mounted images using this ammo to reflect the new state.
   %currentAmmo = %obj.getInventory(%image.ammo);
   %currentClip = %obj.getInventory(%image.clip);

   %subMath1 = %image.ammo.maxInventory - %currentAmmo; //The maximum number of bullets a weapon can carry minus the current amount of ammo in the weapon
   %subMath2 = %currentClip * %image.clip.rndsPerClip; //Total bullets in clips

   if (%obj.client.ammoCache == 0)
   {
      %obj.client.ammoCache = %subMath2 + %currentAmmo; //Is the total sum of intial ammo for this weapon in terms of bullets

   }

   if (%currentAmmo == 0) //This means the player shot all of the bullets in the gun
   {
      %obj.client.ammoCache = %obj.client.ammoCache - %image.clip.rndsPerClip; //Deducting one clip worth of bullets from overall ammo for this weapon
      %obj.decInventory(%image.clip,1); //Deducting the actual clip from our inventory
   }
   else if ((%obj.client.ammoSubCache + %subMath1) > %image.clip.rndsPerClip)
   {
      echo("Some bullets in clip have been fired and a clip will be decremented");
      %obj.client.ammoSubCache = (%obj.client.ammoSubCache + %subMath1) - %image.clip.rndsPerClip;//Subtracts the number of bullets fired from the total a clip can carry to give us the numer of bullets in our current clip then stores it as ammo subcache
      %obj.client.ammoCache = %obj.client.ammoCache - %subMath1; 
      %obj.decInventory(%image.clip,1);
   }
   else
   {
      %obj.client.ammoSubCache = %obj.client.ammoSubCache + %subMath1; 
      %obj.client.ammoCache = %obj.client.ammoCache - %subMath1; 
   }
   echo("This is %obj.client.ammoCache post computations: " @ %obj.client.ammoCache);
   echo("This is %obj.client.ammoSubCache: " @ %obj.client.ammoSubCache);

   if (%obj.client.ammoCache >= %image.clip.rndsPerClip)
      %obj.setInventory(%image.ammo,%image.ammo.maxInventory);

   if (%obj.client.ammoCache < %image.clip.rndsPerClip)
      %obj.setInventory(%image.ammo,%obj.client.ammoCache);

   %obj.client.noSwitch = "no";
   schedule(%time, 0, "updateClientAmmoHUD",%obj.client);
}

Help!! My math stinks apparently.

#1
06/19/2006 (8:01 pm)
Hi Ron, I only had a quick glimpse so I might be wrong (don't have much time atm) but I know for a fact that Torque does not recognize an inventory value of '0'. This might be the problem.

I get around the problem like this:
if(%currentAmmo < 1)