I now have an ammo counter that reads the amount of ammunition and stops the player from shooting if their clip is empty, but the ammo always seems to reset to the starting value whenever I switch out my weapons.
The process goes like this: First, the program chooses what weapon to switch to depending on the button pressed.
#Weapon Switching
if Input.is_action_just_pressed("Weapon1") and weapon != weapons.PISTOLS:
_raise_weapon(weapons.PISTOLS)
player_switchweapon.emit()
if Input.is_action_just_pressed("Weapon2") and weapon != weapons.AUTO:
_raise_weapon(weapons.AUTO)
player_switchweapon.emit()
if Input.is_action_just_pressed("Weapon3") and weapon != weapons.SHOTGUN:
_raise_weapon(weapons.SHOTGUN)
player_switchweapon.emit()
Well… Aren’t you simply setting the ammo count to the starting value in _raise_weapon()? clipammo = rifle.gunclip, gunclip being 6 (or something). If you don’t want that, then you need to not do that. You need to save the ammo somewhere and use the saved ammo value when switching weapons.
Your _raise_weapon(new_weapon) function resets the clipammo and and totalammo variables but doesn’t seem to take account of the current ammo counter.
Can you extend the weapon switching function to e.g.
if Input.is_action_just_pressed("Weapon1") and weapon != weapons.PISTOLS:
_raise_weapon(weapons.PISTOLS, current_ammo_counter)
player_switchweapon.emit()
and then the _raise_weapon function can set the new weapon’s ammo counter accordingly.
I managed to implement this current_ammo_counter variable like you’ve suggested, but how should I go about making it set the ammo count without restarting? If it’s not too much trouble, I’m at a loss when it comes to figuring out how to actually implement it.
I’m not sure how to help your next steps. From the code you’ve supplied I don’t fully understand how clipammo and totalammo are being used. Which counters change when a shot is fired? Which counters change when you reload? Do any counters carry over when you change weapons? Are there different types of ammo for different types of weapon? When you raise a new weapon rather than setting clipammo=shotgun.gunclip should you be using clipammo=shotgun.guntotal-current_ammo_counter and then only set clipammo=shotgun.gunclip when reloading?