Preventing ammo from resetting upon switching weapons

Godot Version

4.41

Question

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

…Which then leads to the weapon raising function.

func _raise_weapon(new_weapon):
	can_shoot = false
	_lower_weapon()
	await get_tree().create_timer(0.3).timeout
	match new_weapon:
		weapons.AUTO:
				weapon_switching.play_backwards("Rifle_Lower")
				clipammo = rifle.gunclip
				totalammo = rifle.guntotal
		weapons.PISTOLS:
				weapon_switching.play_backwards("Pistol_Lower")
				clipammo = pistol.gunclip
				totalammo = pistol.guntotal
		weapons.SHOTGUN:
				weapon_switching.play_backwards("Shotgun_Lower")
				clipammo = shotgun.gunclip
				totalammo = shotgun.guntotal
	weapon = new_weapon
	player_switchweapon.emit()
	can_shoot = true

The gunclip and guntotal variables draw from ints in the weapon scripts, which are all some variation of this.

@export var gunclip : int = 6
@export var guntotal : int  = 24

Is there something I’m missing in my code? Or is there something actively causing the ammo counts to reset.

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.

Hope this helps,
Martin

Hi Martin, thanks for the suggestion.

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?

I’m assuming when the player shoots you decrease the player’s clipammo meaning the gun’s ammo never changes.

Decrease the gun’s ammo first, then synchronize that with the player’s clipammo.