Game Development Community

Ammo bug

by Aleksander Elvemo · in Torque Game Engine · 10/22/2007 (7:53 am) · 8 replies

Hi there!
This is a really excellent community and i'm really happy to be with such helpful and resourceful peers. My question is about ammo and weapons. I created a new fully working weapon wich fires correctly and behaves the way i want it too. The problem is that when i reach zero ammo it still fires. Anyone got a clue?

regards
kinepoo

#1
10/22/2007 (8:21 am)
What does your state setup look like?

Do you have something similar to this in your weapon? (Ripped from crossbow.cs)
// Ready to fire, just waiting for the trigger
   stateName[2]                     = "Ready";
[b]   stateTransitionOnNoAmmo[2]       = "NoAmmo";[/b]
   stateTransitionOnTriggerDown[2]  = "Fire";

[b]   // No ammo in the weapon, just idle until something
   // shows up. Play the dry fire sound if the trigger is
   // pulled.
   stateName[5]                     = "NoAmmo";
   stateTransitionOnAmmo[5]         = "Reload";
   stateSequence[5]                 = "NoAmmo";
   stateTransitionOnTriggerDown[5]  = "DryFire";                        // VERY IMPORTANT

   // No ammo dry fire
   stateName[6]                     = "DryFire";
   stateTimeoutValue[6]             = 1.0;
   stateTransitionOnTimeout[6]      = "NoAmmo";
   stateSound[6]                    = CrossbowFireEmptySound;[/b]
#2
10/22/2007 (8:34 am)
// Ready to fire, just waiting for the trigger
stateName[2] = "Ready";
stateTransitionOnNoAmmo[2] = "NoAmmo";
stateTransitionOnTriggerDown[2] = "Fire";

// Fire the weapon. Calls the fire script which does
// the actual work.
stateName[3] = "Fire";
stateTransitionOnTimeout[3] = "Reload";
stateTimeoutValue[3] = 0.02;
stateFire[3] = true;
stateRecoil[3] = LightRecoil;
stateAllowImageChange[3] = false;
stateSequence[3] = "Fire";
stateScript[3] = "onFire";
stateSound[3] = ar15FireSound;

// Play the reload animation, and transition into
stateName[4] = "Reload";
stateTransitionOnNoAmmo[4] = "NoAmmo";
stateTransitionOnTimeout[4] = "Ready";
stateTimeoutValue[4] = 2.00;
stateAllowImageChange[4] = false;
stateSequence[4] = "Reload";
stateEjectShell[4] = true;
stateScript[4] = "onReload";
//stateSound[4] = ar15ReloadSound;

// No ammo in the weapon, just idle until something
// shows up. Play the dry fire sound if the trigger is
// pulled.
stateName[5] = "NoAmmo";
stateTransitionOnAmmo[5] = "Reload";
stateSequence[5] = "NoAmmo";
stateTransitionOnTriggerDown[5] = "DryFire";

// No ammo dry fire
stateName[6] = "DryFire";
stateTimeoutValue[6] = 1.0;
stateTransitionOnTimeout[6] = "NoAmmo";
stateSound[6] = ar15FireEmptySound;

that was quick!
This is my states, where ar15 is the gun, i can't spot a difference
#3
10/22/2007 (4:22 pm)
In your AR15::onFire() method do you decrement your ammo with something like this?
// Decrement inventory ammo. The image's ammo state is update
   // automatically by the ammo inventory hooks.
   %obj.decInventory(%this.ammo,1);

And finally, try displaying your how much ammo you have. Does it count down every time you fire? Does it count below zero? Does it stop at zero and still let you fire?

It almost sounds like you are referencing the wrong type of ammo somewhere... Not sure though.
#4
10/22/2007 (6:05 pm)
It's probably because you aren't changing the ammo state of the weapon. I'm assuming you are mounting a shapebaseimage? So let's say you have a shapebaseimage called gun. Somewhere in your script there should be gun.SetAmmo(0, true). 0 is the slot number and true says true it has ammo. Set that to false and the gun won't fire anymore.
#5
10/22/2007 (6:11 pm)
Actually what you could do is in the OnFire function keep a global variable for the ammo. Decrease that by one everytime the function gets executed, once it reaches zero change the above state to false.
#6
10/23/2007 (4:42 am)
But this functionality is already provided by the weapon system. It works just fine for the example crossbow. Why the heck would you go and (gasp) make a global variable to keep track of something like this when it is clearly already supported in the code base?

Lets find the real problem not create more down the road (when you go multiplayer).
#7
10/23/2007 (5:59 am)
There are multiple coding paths to a solution and I was merely explaining one possibility.
#8
10/23/2007 (9:36 am)
I got it to work =)
it was a simple ammo definition! gah! programming errors is always some silly typo!
thanks everyone!!