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.
- 12 loaded, 90 in reserve (102 rounds): full magazine and 30 off the reserve left 90 rounds. Taking only the 18 missing, and the script, kept 102: 30 + 72.
- 12 loaded, 10 in reserve (22 rounds): full magazine and 30 off left the reserve at -20. Taking the 18 missing left it at -8, with 30 in the magazine from 22 real rounds. The script loaded 10: 22 in the magazine, 0 in reserve.
- Full magazine, 90 in reserve: full magazine and 30 off lost 30 rounds. The script said it could not reload and loaded 0.
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
mag_size: rounds per magazine.- For a shotgun that loads one shell at a time, move 1 round (if the reserve has one) per shell animation until the magazine is full, with the same check.
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
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- A fire rate that holds at any frame rate
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims