Question about ammo and switching weapons

Monkey Forums/Monkey Programming/Question about ammo and switching weapons

DeadFall(Posted 2012) [#1]
So at the moment I have 2 different weapons and when I switch between them it sets the ammo back to a full capacity. So people can just spam the number 1 key to instant reload the gun.

So I'm wondering how I would make it so that it stores the last known ammo amount for the weapon that way if they hit 1 the ammo would stay at the amount it was previously at.

Example right now the ammo would be at 9/9 they shoot 4 times 5/9 then instead of hitting R to reload and have to wait x seconds for it to complete they can just hit 1 and it's back to 9/9 ammo. And I want to make it so that if they hit 1 when the ammo is at 5/9 it doesn't change.


Jesse(Posted 2012) [#2]
if you are using objects for weapons than it's simple. All you have to do is add a field for the number of bullets loaded. just switching weapons back and forth will allow you to keep track of ammo automatically.
otherwise you need to create variables that do the same for each weapon.


c.k.(Posted 2012) [#3]
Each player object has a "weapons_on_hand" array (or object), in which is stored a variety of gun objects which the player has in his possession.

Each hand gun should be a separate "weapon/pistol" object, and each gun should have a field called (for example), "bullets_in_chamber," which gets debited each time the gun is fired. When it's zero, you just hear this annoying "click." Because each gun has its own separate "bullets_in_chamber" field, you don't have to worry about tracking anything. The object tracks it for you.

That's the way I'd do it.

We'd have to see some code to figure out why switching to another weapon automatically fills the chamber/magazine... If they are separate objects, it shouldn't be doing that.


DeadFall(Posted 2012) [#4]
Thanks! Currently the code I'm using it really test code and I know why it was doing that and I'm planning on redoing the way weapons works thanks for the help so far!


Jesse(Posted 2012) [#5]
I would assume the reason that is happening is because you are creating a new object every time the specific weapon is selected. just a guess.


DeadFall(Posted 2012) [#6]
Yeah that is the problem. I kinda realized that a little while ago, I was just wondering on ways to prevent it by other methods of coding it. But I got a general idea on what I need to do to fix the problem.