← Pixel Dreamer Studio

Reload without losing or making up rounds

Load the smaller of what the magazine is missing and what the reserve holds.

Setting the magazine to full and taking a whole magazine from the reserve throws away whatever was still loaded. In our Godot 4.7 test, reloading with 12 rounds left and 90 in reserve went from 102 rounds to 90, and reloading a full magazine lost 30. Taking only the missing rounds is right until the reserve runs low: with 10 in reserve it left the magazine at 30 and the reserve at -8. Load mini(mag_size - in_mag, reserve): 102 stayed 102, and 12 + 10 became a magazine of 22.

The code

Check can_reload() before starting the reload animation, so the player cannot reload a full magazine or with nothing left, and call reload() when the animation reaches the point where the new magazine goes in. It returns how many rounds were loaded.

extends Node
## Magazine and reserve ammo that never creates or loses rounds when reloading.

signal reloaded(loaded: int)

@export var mag_size := 30

var in_mag := 30
var reserve := 90


func can_reload() -> bool:
	return in_mag < mag_size and reserve > 0


func reload() -> int:
	var loaded := mini(mag_size - in_mag, reserve)  # only what is missing, only what you carry
	in_mag += loaded
	reserve -= loaded
	reloaded.emit(loaded)
	return loaded

What we measured

A 30-round magazine in three situations. Two runs with the same numbers.

Why it happens

A reload moves rounds from the reserve into the magazine, so the total should never change. Setting the magazine to full forgets what was in it, and taking what is missing forgets that the reserve may not have that many. Loading the smaller of the two covers both.

What to change

Shots that hit what the crosshair is on: raycast from the camera, not the muzzle. A muzzle flash that lights the room: muzzle flash with a light.

A ready-made shooter base: FPS Combat Kit for Godot 4 has hitscan shooting, impacts by surface, a muzzle flash with a real light, shell ejection and barrel smoke when the barrel gets hot.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: FPS Combat Kit for Godot 4 has hitscan shooting, impacts by surface, a muzzle flash with a real light, shell ejection and barrel smoke when the barrel gets hot.

More how-tos for FPS Combat Kit for Godot 4: Muzzle flash with a real light · Flash a 3D enemy white when it takes a hit

More how-tos: Projectiles and aiming