Dealing with machine gun in a low framerate

Godot Version

4.3

Question

How do you guys deal with this?

Suppose a machine gun shoots 30 bullets per second, we spawn a bullet in _process function at every 0.033 sec delta. All is well, but if the framerate goes less than 30 fps, the machine gun will no longer shoots 30 bullets. This will also result in damage per second being lowered.

In this case, how would you deal with this fairly?

determine how much time has passed and do a division + round operation to figure out how many bullets would have spawned in say ‘0.066’ seconds. then deal appropriate damage. make bullets cosmetic only, or spawn in batches similar to how the damage now works

1 Like

Bullets are physics objects so, save yourself some frames and spawn/move them in _phyiscs_process.

Like SpikeTrapBoomStudios said, the time passed also allows you to calculate how many bullets should spawn. Example:

var spawn_interval: float = 0.06 # 1000 RPM
var time_elapsed: float

func _physics_process(delta: float):
    time_elapsed += delta
    if time_elapsed >= spawn_interval:
        # Spawn bullets
        var spawn_count = int(floorf(time_elapsed / spawn_interval))
        time_elapsed -= float(spawn_count) * spawn_interval
        for i in range(spawn_count, -1, 0):
            var bullet = spawn_bullet()
            # Move the bullet according to how much time
            # passed since it was supposed to be spawned.
            bullet.move(spawn_interval * i) # spawn_interval used as a delta
1 Like